CodeView

PHPUnit test harness for Komodo

KoCommandTest.php

<?php
 
    /**
     * @author  Michael Heim
     * @link    http://www.zeilenwechsel.de/
     * @version 1.0.0
     * @license http://www.zeilenwechsel.de/it/code/browse/komodo-phpunit-harness/prod/license.txt
     */
 
    require_once( dirname( dirname( dirname( __file__ ) ) ) . '/prod/Utils/Loader.php' );
    PHPUnitXt_Utils_Loader::load( 'PHPUnitXt_Ko_Command' );
 
 
    class PHPUnitXt_Ko_CommandTest extends PHPUnit_Framework_TestCase {
 
        /* @var string    the full path to Komodo's drive_testrunner.php */
        protected $ko_testrunner_path;
 
        /* @var PHPUnit_TextUI_ResultPrinter */
        protected $printer_stub;
 
 
        /**
         * @dataProvider provideSwitches
         */
        public function testConfigureAndRun_TestDirFromKomodo_IsPassedToPHPUnit ( $switches ) {
 
            $ko_testdir = '/home/me/myapp/tests';
 
            $args     = $this->build_args( $ko_testdir, $switches );
            $expected = $this->build_phpunit_expected_args( $switches, $ko_testdir );
 
            $ko_command = $this->getMock( 'PHPUnitXt_Ko_Command', array( 'run' ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'run' )
                       ->with( $this->equalTo( $expected ) );
 
            PHPUnitXt_Ko_Command::configure_and_run( $ko_command, $args, $this->printer_stub );
 
        }
 
        /**
         * @dataProvider provideSwitches
         */
        public function testConfigureAndRun_TestDirFromUserArgs_IsPassedToPHPUnit ( $switches ) {
 
            $ko_testdir          = '/home/me/myapp/tests';
            $testdir_in_userargs = '/home/me/otherapp/unittests';
 
            $args     = $this->build_args( $ko_testdir, $this->concat_arg_string( $switches, $testdir_in_userargs ) );
            $expected = $this->build_phpunit_expected_args( $switches, $testdir_in_userargs );
 
            $ko_command = $this->getMock( 'PHPUnitXt_Ko_Command', array( 'run', 'set_working_dir' ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'run' )
                       ->with( $this->equalTo( $expected ) );
 
            PHPUnitXt_Ko_Command::configure_and_run( $ko_command, $args, $this->printer_stub );
 
        }
 
        /**
         * @dataProvider provideSwitches
         */
        public function testConfigureAndRun_TestDirFromUserArgs_IsDot_IsPassedToPHPUnit ( $switches ) {
 
            $ko_testdir          = '/home/me/myapp/tests';
            $testdir_in_userargs = '.';
 
            $args     = $this->build_args( $ko_testdir, $this->concat_arg_string( $switches, $testdir_in_userargs ) );
            $expected = $this->build_phpunit_expected_args( $switches, $testdir_in_userargs );
 
            $ko_command = $this->getMock( 'PHPUnitXt_Ko_Command', array( 'run', 'set_working_dir' ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'run' )
                       ->with( $this->equalTo( $expected ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'set_working_dir' )
                       ->with( $this->equalTo( $ko_testdir ) );
 
            PHPUnitXt_Ko_Command::configure_and_run( $ko_command, $args, $this->printer_stub );
 
        }
 
        /**
         * @dataProvider provideSwitches
         */
        public function testConfigureAndRun_TestClassFromUserArgs_IsPassedToPHPUnit_WorkDirSetToKomodoDir ( $switches ) {
 
            $ko_testdir            = '/home/me/myapp/tests';
            $testclass_in_userargs = 'SomeClassTest';
 
            $args     = $this->build_args( $ko_testdir, $this->concat_arg_string( $switches, $testclass_in_userargs ) );
            $expected = $this->build_phpunit_expected_args( $switches, null, $testclass_in_userargs );
 
            $ko_command = $this->getMock( 'PHPUnitXt_Ko_Command', array( 'run', 'set_working_dir' ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'run' )
                       ->with( $this->equalTo( $expected ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'set_working_dir' )
                       ->with( $this->equalTo( $ko_testdir ) );
 
            PHPUnitXt_Ko_Command::configure_and_run( $ko_command, $args, $this->printer_stub );
 
        }
 
        /**
         * @dataProvider provideSwitches
         */
        public function testConfigureAndRun_TestFileFromUserArgs_IsPassedToPHPUnit_WorkDirSetToKomodoDir ( $switches ) {
 
            $ko_testdir           = '/home/me/myapp/tests';
            $testfile_in_userargs = 'SomeClassTest.php';
 
            $args     = $this->build_args( $ko_testdir, $this->concat_arg_string( $switches, $testfile_in_userargs ) );
            $expected = $this->build_phpunit_expected_args( $switches, null, null, $testfile_in_userargs );
 
            $ko_command = $this->getMock( 'PHPUnitXt_Ko_Command', array( 'run', 'set_working_dir' ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'run' )
                       ->with( $this->equalTo( $expected ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'set_working_dir' )
                       ->with( $this->equalTo( $ko_testdir ) );
 
            PHPUnitXt_Ko_Command::configure_and_run( $ko_command, $args, $this->printer_stub );
 
        }
 
        /**
         * @dataProvider provideSwitches
         */
        public function testConfigureAndRun_TestClassAndTestFileFromUserArgs_IsPassedToPHPUnit_WorkDirSetToKomodoDir ( $switches ) {
 
            $ko_testdir            = '/home/me/myapp/tests';
            $testclass_in_userargs = 'SomeClassTest';
            $testfile_in_userargs  = 'SomeClassTest.php';
 
            $args     = $this->build_args( $ko_testdir, $this->concat_arg_string( $switches, "$testclass_in_userargs $testfile_in_userargs" ) );
            $expected = $this->build_phpunit_expected_args( $switches, null, $testclass_in_userargs, $testfile_in_userargs );
 
            $ko_command = $this->getMock( 'PHPUnitXt_Ko_Command', array( 'run', 'set_working_dir' ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'run' )
                       ->with( $this->equalTo( $expected ) );
 
            $ko_command->expects( $this->once() )
                       ->method( 'set_working_dir' )
                       ->with( $this->equalTo( $ko_testdir ) );
 
            PHPUnitXt_Ko_Command::configure_and_run( $ko_command, $args, $this->printer_stub );
 
        }
 
 
        public function provideSwitches () {
 
            return array(
                array( '' ),                                                    /* data set #0: no switches used */
                array( '--in-sequence' ),                                       /* data set #1: a single switch */
                array( '--in-sequence --as-name' ),                             /* data set #2: multiple switches */
                array( '-d magic_quotes_runtime=On' ),                          /* data set #3: switch with value */
                array( '--in-sequence --verbose -d magic_quotes_runtime=On' )   /* data set #4: multiple switches, one with value */
            );
 
        }
 
        /**
         * Builds the $_Server arguments array as provided by Komodo.
         * 
         * @param string $ko_testdir    the testdir defined in the Komodo user interface
         * @param string $user_args     the arg string defined in the Komodo user interface
         * 
         * @return array                the arguments array
         */
        public function build_args ( $ko_testdir, $user_args ) {
 
            $args[] = $this->ko_testrunner_path;
            $args[] = $ko_testdir;
            if ( $user_args !== '' ) $args = array_merge( $args, explode( ' ', $user_args ) );
 
            return $args;
 
        }
 
        /**
         * Builds the arguments array as expected by PHPUnit.
         * 
         * @param string $switches          switches passed to PHPUnit
         * @param string $test_directory    the test directory passed to PHPUnit, if used. Must not be used in conjunction
         *                                  with $test_classname or $test_filename.
         * @param string $test_classname    the test classname passed to PHPUnit, if used. May be used alone or together
         *                                  with $test_filename, but never with $test_directory.
         * @param string $test_filename     the test filename passed to PHPUnit, if used.
         * 
         * @return array                    the arguments array
         */
        public function build_phpunit_expected_args ( $switches, $test_directory = null, $test_classname = null, $test_filename = null ) {
 
            // Make sure the optional arguments are used correctly.
            if ( $test_directory and ( $test_classname or $test_filename ) )
                throw new PHPUnit_Framework_Exception( 'Invalid test setup: a test directory can\'t be defined along with a test classname or a test filename' );
 
            if ( ! ( $test_directory or $test_classname or $test_filename ) )
                throw new PHPUnit_Framework_Exception( 'Invalid test setup: a test directory, classname or filename must be defined' );
 
 
            $args[] = $this->ko_testrunner_path;
            if ( $switches !== '' ) $args = array_merge( $args, explode( ' ', $switches ) );
 
            if ( $test_directory ) $args[] = $test_directory;
            if ( $test_classname ) $args[] = $test_classname;
            if ( $test_filename )  $args[] = $test_filename;
 
            return $args;
 
        }
 
        /**
         * Concatenates partial argument strings.
         * 
         * @param string $args1
         * @param string $args2
         * 
         * @return string    the concatenated arguments string
         */
        public function concat_arg_string ( $args1, $args2 ) {
 
            if ( $args1 === '' or $args2 === '') {
                $concat = $args1 . $args2;
            } else {
                $concat = "$args1 $args2";
            }
 
            return $concat;
 
        }
 
        public function setUp () {
 
            putenv( 'PHPUNIT_COMMAND_NOEXIT=true' );
 
            $this->ko_testrunner_path = PHPUnitXt_Utils_Loader::root() . DIRECTORY_SEPARATOR . 'drive_testrunner.php';
 
            $this->printer_stub = $this->getMock( 'PHPUnitXt_Base_Printer', array( 'get_switch_names' ) );
            $this->printer_stub->expects( $this->any() )
                               ->method( 'get_switch_names' )
                               ->will( $this->returnValue( array( 'in-sequence', 'as-name' ) ) );
 
        }
 
    }
 
?>