CodeView

PHPUnit test harness for Komodo

DebugOptionSet.php

<?php
 
    /**
     * @author  Michael Heim
     * @link    http://www.zeilenwechsel.de/
     * @version 1.2.0
     * @license http://www.zeilenwechsel.de/it/code/browse/komodo-phpunit-harness/prod/license.txt
     */
 
    /**
     * Object representing multiple sets of switches for a test harness. Provides
     * filenames for debug output along with the switches.
     */
    abstract class DebugOptionSet {
 
        /* @var boolean   whether to return arguments as a string or an array (default: array) */
        protected $return_args_as_string = false;
 
        /* @var string    the path to prepend to filenames (including trailing slash); empty string if unused */
        protected $dirpath = '';
 
        /* @var string    optional prefix to prepend to filenames */
        protected $filename_prefix = '';
 
        /* @var string    optional default filename to use instead of an empty filename */
        protected $empty_filename_default = '';
 
        /* for caching the generated argument strings, argument arrays, and filenames: */
        protected $argstrings = null;
        protected $arg_arrays = null;
        protected $filenames  = null;
 
        /**
         * If called, arguments will be concatenated and returned as a string. By
         * default, arguments are returned in an array (as if read from the command
         * line).
         * 
         * @return DebugOptionSet
         */
        public function format_args_as_string () {
 
            $this->return_args_as_string = true;
            return $this;
 
        }
 
        /**
         * Returns an array of test configurations covering all available sets of
         * options/switches. Also provides a filename for the output of each set.
         *
         * Each configuration is again an array: 'args' containing an arg string or an
         * arg array for an options combination, 'output_file' the corresponding
         * filename for output.
         * 
         * @return array
         */
        public function get_test_configurations () {
 
            $args = $this->get_argument_sets();
 
            $filenames  = $this->get_filename_sets();
 
            for ( $i = 0; $i < count( $args ); $i++ ) {
 
                $configurations[] = array(
                    'args'        => $args[$i],
                    'output_file' => $filenames[$i]
                );
 
            }
 
            return $configurations;
 
        }
 
        /**
         * Returns an array of switch configurations.
         *
         * Each individual switch configuration is represented as an array by default,
         * or as a string if format_args_as_string() has been called.
         *
         * @return string|array
         */
        public function get_argument_sets () {
 
            $args = (
                $this->return_args_as_string ?
                $this->get_argstring_sets() :
                $this->get_arg_array_sets()
            );
 
            return $args;
 
        }
 
        abstract protected function get_argstring_sets ();
 
        /**
         * Returns an array of argument arrays covering all possible combinations of options.
         *
         * @return array
         */
        protected function get_arg_array_sets () {
 
            if ( is_null( $this->arg_arrays ) ) {
 
                $argstrings = $this->get_argstring_sets();
                foreach ( $argstrings as $argstring ) $this->arg_arrays[] = explode( ' ', $argstring );
 
            }
 
            return $this->arg_arrays;
 
        }
 
        /**
         * Sets the path to prepend to filenames which are returned by
         * get_filename_sets().
         *
         * @param  string $path
         * @return DebugOptionSet
         */
        public function prepend_path_to_filenames ( $path ) {
 
            $this->dirpath = Tools::add_trailing_slash( $path );
            return $this;
 
        }
 
        /**
         * Sets an optional prefix for filenames which are returned by
         * get_filename_sets().
         *
         * @param  string $prefix
         * @return DebugOptionSet
         */
        public function use_filename_prefix( $prefix ) {
 
            $this->filename_prefix = $prefix;
            return $this;
 
        }
 
        /**
         * Sets an optional default for filenames which would otherwise be returned as
         * an empty string by get_filename_sets().
         *
         * NB: The replacement will happen before a prefix is added (cf.
         * use_filename_prefix()).
         *
         * @param  string $name
         * @return DebugOptionSet
         */
        public function set_empty_filename_default ( $name ) {
 
            $this->empty_filename_default = $name;
            return $this;
 
        }
 
        /**
         * Returns an array of output filenames corresponding to the generated arg string combinations.
         *
         * @return array
         */
        protected function get_filename_sets () {
 
            if ( is_null( $this->filenames ) ) {
 
                $argstrings = $this->get_argstring_sets();
                foreach ( $argstrings as $argstring ) {
 
                    $filename = ( $argstring === '' ? $this->empty_filename_default : $argstring );
                    $filename = $this->filename_prefix . $filename;
                    $filename =
                        str_replace( array( '-c ', '--', ' ', 'display-', '/', '\\' ), array( 'c ', '', '_', '', '~', '~' ), $filename )
                        . '.txt';
 
                    $this->filenames[] = $filename;
 
                }
 
            }
 
            if ( $this->dirpath != '' ) {
 
                foreach ( $this->filenames as $filename ) $files[] = $this->dirpath . $filename;
 
            } else {
 
                $files = $this->filenames;
 
            }
 
            return $files;
 
        }
 
    }
 
?>