CodeView

PHPUnit test harness for Komodo

DebugFactory.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
     */
 
    class DebugFactory {
 
        /* @var string    'Base' or 'Ko' */
        protected $type;
 
        /* @var string     '' or 'Ko' */
        protected $prefix;
 
        /**
         * @param string $type  'Base', 'Ko'
         */
        public function __construct ( $type ) {
 
            $this->type = $type;
 
            switch ( $type ) {
 
                case 'Base':
                    $this->prefix = '';
                    break;
 
                case 'Ko':
                    $this->prefix = 'Ko';
                    break;
 
                default:
                    throw new InvalidArgumentException( "Unrecognized type '$type'" );
 
            }
 
        }
 
        /**
         * @param  string $type  'Base', 'Ko'
         * @return DebugFactory
         */
        static public function init ( $type ) {
 
            return new self( $type );
 
        }
 
        /**
         * Create a CustomOptionSet, testing custom XML file switches.
         * Test dir must be 'resources/xml/custom-file'.
         *
         * @return CustomOptionSet
         */
        protected function create_xml_switches_for_custom_xml_dir () {
 
            $switches = array(
 
                /* testing a custom config */
 
                /*     (a) as only switch      */
                '-c dummy_config1.xml',
                '--configuration dummy_config2.xml',
                /*     (b) following another switch */
                '--as-name -c dummy_config3.xml',
                '--as-name --configuration dummy_config4.xml',
                /*     (b) preceding another switch */
                '-c dummy_config5.xml --as-name',
                '--configuration dummy_config6.xml --as-name'
 
            );
 
            $option_set = new CustomOptionSet( $switches );
            $option_set->set_empty_filename_default( 'noargs' )
                       ->use_filename_prefix( 'xml-custom-' );
 
            return $option_set;
 
        }
 
        /**
         * Create a CustomOptionSet, testing switches for the default phpunit.xml.
         * Test dir must be a subdirectory of 'resources/xml/default-file'.
         *
         * @param string|integer $subdir   subdirs are numbered, so integers will do as well
         * @return CustomOptionSet
         */
        protected function create_xml_switches_for_default_xml_dir ( $subdir ) {
 
            switch ( (int) $subdir ) {
 
                case 1:
                    $switch = '';               // no test file, no switch => using phpunit.xml
                    break;
 
                case 2:
                    $switch = '--as-name';    // using phpunit.xml with another switch
                    break;
 
                default:
                    throw new InvalidArgumentException( "Subdir '$subdir' is not defined" );
 
            }
 
            $option_set = new CustomOptionSet( array( $switch ) );
            $option_set->set_empty_filename_default( 'noargs' )
                       ->use_filename_prefix( "xml-default-$subdir-");
 
            return $option_set;
 
        }
 
        /**
         * Create a CustomOptionSet, testing phpunit.xml.dist switches. Test dir must
         * be a subdirectory of 'resources/xml/dist-file'.
         *
         * @param string|integer $subdir   subdirs are numbered, so integers will do as well
         * @return CustomOptionSet
         */
        protected function create_xml_switches_for_dist_dir ( $subdir ) {
 
            switch ( (int) $subdir ) {
 
                case 1:
                    $switch = '';               // no test file, no switch => using phpunit.xml.dist
                    break;
 
                case 2:
                    $switch = '--as-name';    // using phpunit.xml.dist with another switch
                    break;
 
                default:
                    throw new InvalidArgumentException( "Subdir '$subdir' is not defined" );
 
            }
 
            $option_set = new CustomOptionSet( array( $switch ) );
            $option_set->set_empty_filename_default( 'noargs' )
                       ->use_filename_prefix( "xml-dist-$subdir-");
 
            return $option_set;
 
        }
 
        /**
         * Create a CustomOptionSet, testing XML configurations based on the selected
         * directory. The directory path will determine what precisely is tested.
         *
         * The $config_dir_path should be an absolute path to one of these directories:
         *
         * - 'resources/xml/custom-file'
         * - a subdirectory of 'resources/xml/default-file'
         * - a subdirectory of 'resources/xml/dist-file'
         *
         * @param  string $config_dir_path  will be normalized for the comparison
         * @return CustomOptionSet
         */
        public function create_xml_switches_from_dirpath ( $config_dir_path ) {
 
            $config_dir_path = realpath( Tools::remove_trailing_slash( $config_dir_path ) );
 
            $is_default_xml_config = ( strpos( $config_dir_path, INTEGRATION_TEST_XML_DEFAULT_RESOURCE_DIR ) === 0 );
            $is_dist_xml_config    = ( strpos( $config_dir_path, INTEGRATION_TEST_XML_DIST_RESOURCE_DIR ) === 0 );
 
            if ( $is_default_xml_config or $is_dist_xml_config ) $subdir = basename( $config_dir_path );
 
            switch ( $config_dir_path ) {
 
                case INTEGRATION_TEST_XML_CUSTOM_RESOURCE_DIR:
                    $option_set = self::create_xml_switches_for_custom_xml_dir();
                    break;
 
                case INTEGRATION_TEST_XML_DEFAULT_RESOURCE_DIR . DIRECTORY_SEPARATOR . $subdir:
                    $option_set = self::create_xml_switches_for_default_xml_dir( $subdir );
                    break;
 
                case INTEGRATION_TEST_XML_DIST_RESOURCE_DIR . DIRECTORY_SEPARATOR . $subdir:
                    $option_set = self::create_xml_switches_for_dist_dir( $subdir );
                    break;
 
                default:
                    throw new InvalidArgumentException( "Can't handle the config dir path '$config_dir_path' automatically" );
 
            }
 
            return $option_set;
 
        }
 
        /**
         * Returns the default printer options.
         *
         * @return PHPUnitXt_Base_PrinterOptions|PHPUnitXt_Ko_PrinterOptions
         */
        public function create_default_printer_options () {
 
            require_once( HARNESS_LOADER );
            $printer_class = "PHPUnitXt_{$this->type}_PrinterOptions";
            PHPUnitXt_Utils_Loader::load( $printer_class );
 
            return new PrinterOptionSet( new $printer_class );
 
        }
 
        /**
         * @return BulkOutputGenerator
         */
        public function create_bulk_output_generator ( DebugOptionSet $option_set ) {
 
            $debug_run_class = "{$this->prefix}DebugRun";
 
            return new BulkOutputGenerator( new $debug_run_class(), $option_set );
 
        }
 
        /**
         * Creates a BulkOutputGenerator for excercising all available printer options.
         * Output is based on the default dummy test.
         *
         * Missing output dirs are created, progress is logged.
         * 
         * @return BulkOutputGenerator
         */
        public function create_bulk_output_generator_for_printer_options () {
 
            $generator = $this->create_bulk_output_generator( $this->create_default_printer_options() );
 
            $generator->set_dummy_test( INTEGRATION_TEST_RESOURCE_DIR . '/DummyClassTst.php' )
                      ->create_missing_output_dir()
                      ->log_progress();
 
            return $generator;
 
        }
 
        /**
         * Creates a BulkOutputGenerator for excercising an XML configuration scenario.
         * 
         * Output is based on the selected directory. The $config_dir_path should be an
         * absolute path to one of these directories:
         *
         * - 'resources'
         * - 'resources/xml'
         * - 'resources/xml/dist-file'
         *
         * Missing output dirs are created, progress is logged.
         * 
         * @param  string $config_dir_path  will be normalized for the comparison
         * @return BulkOutputGenerator
         */
        public function create_bulk_output_generator_for_xml_config ( $config_dir_path ) {
 
            $option_set = $this->create_xml_switches_from_dirpath( $config_dir_path );
            $generator  = $this->create_bulk_output_generator( $option_set );
 
            $generator->set_dummy_test( $config_dir_path )
                      ->create_missing_output_dir()
                      ->log_progress();
 
            return $generator;
 
        }
 
 
 
    }
 
?>