CodeView

PHPUnit test harness for Komodo

BulkOutputGenerator.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
     */
 
    /**
     * Generates all available combinations of options for a given printer. Also creates testrunner output for
     * all those combinations, using a dummy PHPUnit test or XML config.
     */
    class BulkOutputGenerator {
 
        /* @var DebugRun */
        protected $debug_run;
 
        /* @var DebugOptionSet */
        protected $option_set;
 
        /* @var string */
        protected $output_dir;
        protected $dummy_test_path;
 
        /* @var boolean */
        protected $normalize = false;
        protected $create_output_dir = false;
        protected $show_progress = false;
 
        /**
         * @param DebugRun       $debug_run    manager for debug runs
         * @param DebugOptionSet $option_set   the option set to generate output files for
         */
        public function __construct( DebugRun $debug_run, DebugOptionSet $option_set ) {
 
            $this->debug_run = $debug_run;
            $this->option_set = $option_set;
 
        }
 
        /**
         * If called, the output will be normalized. By default, it is returned raw, ie
         * exactly as generated by the test harness.
         * 
         * @see DebugOutputTools::normalize_output
         *
         * @return BulkOutputGenerator
         */
        public function normalize_output () {
 
            $this->normalize = true;
            return $this;
 
        }
 
        /**
         * @param  string  $path    acceptable with and without trailing slash
         * @param  boolean $create  create the dir if it doesn't exist (default: false)
         * 
         * @return BulkOutputGenerator
         */
        public function set_output_dir ( $path ) {
 
            $this->output_dir = $path;
            return $this;
 
        }
 
        /**
         * If called, the output dir is created if it doesn't already exist.
         * 
         * @return BulkOutputGenerator
         */
        public function create_missing_output_dir () {
 
            $this->create_output_dir = true;
            return $this;
 
        }
 
        /**
         * Sets the path to the test file, or to the directory containing the XML
         * configuration.
         * 
         * @param string $path  to a test file, or to a directory if an XML config is used
         * @return BulkOutputGenerator
         */
        public function set_dummy_test ( $path ) {
 
            $this->dummy_test_path = $path;
            return $this;
 
        }
 
        /**
         * Prints status messages during output generation.
         * 
         * @return BulkOutputGenerator
         */
        public function log_progress () {
 
            $this->show_progress = true;
            return $this;
 
        }
 
        /**
         * Writes the captured output to a set of files or prints it (depending on
         * write_to_files()).
         *
         * @return BulkOutputGenerator
         */
        public function create_output () {
 
            if ( ! isset( $this->output_dir ) ) throw new BadMethodCallException( 'Output dir must be defined in order to generate files' );
            if ( ! is_dir( $this->output_dir ) ) {
 
                if ( $this->create_output_dir ) {
 
                    mkdir( $this->output_dir, 0777, true );
 
                } else {
 
                    throw new BadMethodCallException( 'Output dir does not exist. Use the $create option in set_output_dir() to create it automatically' );
 
                }
 
            }
 
            $this->option_set->prepend_path_to_filenames( $this->output_dir );
            $test_configurations = $this->option_set->get_test_configurations();
 
            if ( $this->show_progress ) {
 
                print "Using configuration:\n";
                print "- testrunner: " . realpath( $this->debug_run->get_testrunner_path() ) . "\n";
                print "- option set: " . get_class( $this->option_set ) . "\n";
                print "- dummy test: " . realpath( $this->dummy_test_path ) . "\n";
                print "- output dir: " . realpath( $this->output_dir ) . "\n";
                print "Creating files ...\n";
 
            }
 
            $this->debug_run->do_not_print_output();
 
            foreach ( $test_configurations as $configuration ) {
 
                $output = $this->debug_run->go( $this->dummy_test_path, $configuration[ 'args' ] );
 
                if ( $this->normalize ) $output = DebugOutputTools::normalize_output( $output );
 
                file_put_contents( $configuration[ 'output_file' ], $output );
 
            }
 
            if ( $this->show_progress ) print "Done.\n\n";
 
            return $this;
 
        }
 
        /**
         * Utility function, returning an array of all XML configuration test dirs
         * currently in use.
         *
         * @return array
         */
        public static function get_xml_config_dirs () {
 
            $config_test_dirs = array();
 
            // Get all non-empty directories in the XML config tree.
            $top_xml_dir = INTEGRATION_TEST_XML_RESOURCE_DIR;
 
            /* @var $file DirectoryIterator */
            foreach( new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $top_xml_dir ) ) as $file ) {
 
                // Getting rid of dot "files"  ('.' and '..')
                if ( $file->isDir() ) continue;
                $config_test_dirs[] = $file->getPath();
 
            }
 
            // Add empty directories manually if they are relevant for a test
            // configuration (currently unused)
            // $config_test_dirs[] = INTEGRATION_TEST_RESOURCE_DIR;
 
            $config_test_dirs = array_unique( $config_test_dirs );
 
            return $config_test_dirs;
 
        }
 
    }
 
?>