From 5a631a6c74b6aba084adbfc5b7e67b17f5124cb1 Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 12 Feb 2011 16:08:48 -0500 Subject: [PATCH] Renaming the runner. Making the runner extend PHPUnit_TextUi_TestRunner so we can customize the TestSuite classes it uses. Adding CakeTestSuiteCommand so we can customize the TextUI_Command output. --- cake/console/shells/testsuite.php | 5 +- .../{test_runner.php => cake_test_runner.php} | 31 +--- cake/tests/lib/cake_test_suite_command.php | 139 ++++++++++++++++++ 3 files changed, 144 insertions(+), 31 deletions(-) rename cake/tests/lib/{test_runner.php => cake_test_runner.php} (50%) create mode 100644 cake/tests/lib/cake_test_suite_command.php diff --git a/cake/console/shells/testsuite.php b/cake/console/shells/testsuite.php index f74c53e36..b0c31831f 100644 --- a/cake/console/shells/testsuite.php +++ b/cake/console/shells/testsuite.php @@ -253,13 +253,12 @@ class TestSuiteShell extends Shell { * @return void */ protected function run($runnerArgs, $options = array()) { - require_once CAKE . 'tests' . DS . 'lib' . DS . 'test_runner.php'; - require_once CAKE . 'tests' . DS . 'lib' . DS . 'cake_test_loader.php'; + require_once CAKE . 'tests' . DS . 'lib' . DS . 'cake_test_suite_command.php'; restore_error_handler(); restore_error_handler(); - $testCli = new TestRunner('CakeTestLoader', $runnerArgs); + $testCli = new CakeTestSuiteCommand('CakeTestLoader', $runnerArgs); $testCli->run($options); } diff --git a/cake/tests/lib/test_runner.php b/cake/tests/lib/cake_test_runner.php similarity index 50% rename from cake/tests/lib/test_runner.php rename to cake/tests/lib/cake_test_runner.php index ec99cf93a..632f88fcb 100644 --- a/cake/tests/lib/test_runner.php +++ b/cake/tests/lib/cake_test_runner.php @@ -16,15 +16,7 @@ * @since CakePHP(tm) v 2.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -define('CORE_TEST_CASES', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'cases'); -define('APP_TEST_CASES', TESTS . 'cases'); - -require 'PHPUnit/TextUI/Command.php'; - -require_once CAKE_TESTS_LIB . 'cake_test_suite.php'; -require_once(CAKE_TESTS_LIB . 'cake_test_case.php'); -require_once(CAKE_TESTS_LIB . 'controller_test_case.php'); - +require 'PHPUnit/TextUI/TestRunner.php'; PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT'); @@ -33,19 +25,8 @@ PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT'); * * @package cake.tests.lib */ -class TestRunner extends PHPUnit_TextUI_Command { +class CakeTestRunner extends PHPUnit_TextUI_TestRunner { -/** - * Construct method - * - * @param array $params list of options to be used for this run - */ - public function __construct($loader, $params = array()) { - $this->arguments['loader'] = $loader; - $this->arguments['test'] = $params['case']; - $this->arguments['testFile'] = $params; - $this->_params = $params; - } /** * Sets the proper test suite to use and loads the test file in it. @@ -54,12 +35,6 @@ class TestRunner extends PHPUnit_TextUI_Command { * @return void */ protected function handleCustomTestSuite() { - /*$manager = new TestManager($this->_params); - - if (!empty($this->_params['case'])) { - $this->arguments['test'] = $manager->getTestSuite(); - $this->arguments['test']->setFixtureManager($manager->getFixtureManager()); - $manager->loadCase($this->_params['case'] . '.test.php', $this->arguments['test']); - }*/ + } } \ No newline at end of file diff --git a/cake/tests/lib/cake_test_suite_command.php b/cake/tests/lib/cake_test_suite_command.php new file mode 100644 index 000000000..779edad22 --- /dev/null +++ b/cake/tests/lib/cake_test_suite_command.php @@ -0,0 +1,139 @@ +addFileToBlacklist(__FILE__, 'DEFAULT'); + +/** + * Class to customize loading of test suites from CLI + * + * @package cake.tests.lib + */ +class CakeTestSuiteCommand extends PHPUnit_TextUI_Command { + +/** + * Construct method + * + * @param array $params list of options to be used for this run + */ + public function __construct($loader, $params = array()) { + $this->arguments['loader'] = $loader; + $this->arguments['test'] = $params['case']; + $this->arguments['testFile'] = $params; + $this->_params = $params; + } + + /** + * @param array $argv + * @param boolean $exit + */ + public function run(array $argv, $exit = TRUE) + { + $this->handleArguments($argv); + + $runner = new CakeTestRunner($this->arguments['loader']); + + if (is_object($this->arguments['test']) && + $this->arguments['test'] instanceof PHPUnit_Framework_Test) { + $suite = $this->arguments['test']; + } else { + $suite = $runner->getTest( + $this->arguments['test'], + $this->arguments['testFile'], + $this->arguments['syntaxCheck'] + ); + } + + if (count($suite) == 0) { + $skeleton = new PHPUnit_Util_Skeleton_Test( + $suite->getName(), + $this->arguments['testFile'] + ); + + $result = $skeleton->generate(TRUE); + + if (!$result['incomplete']) { + eval(str_replace(array(''), '', $result['code'])); + $suite = new PHPUnit_Framework_TestSuite( + $this->arguments['test'] . 'Test' + ); + } + } + + if ($this->arguments['listGroups']) { + PHPUnit_TextUI_TestRunner::printVersionString(); + + print "Available test group(s):\n"; + + $groups = $suite->getGroups(); + sort($groups); + + foreach ($groups as $group) { + print " - $group\n"; + } + + exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT); + } + + unset($this->arguments['test']); + unset($this->arguments['testFile']); + + try { + $result = $runner->doRun($suite, $this->arguments); + } + + catch (PHPUnit_Framework_Exception $e) { + print $e->getMessage() . "\n"; + } + + if ($exit) { + if (isset($result) && $result->wasSuccessful()) { + exit(PHPUnit_TextUI_TestRunner::SUCCESS_EXIT); + } + + else if (!isset($result) || $result->errorCount() > 0) { + exit(PHPUnit_TextUI_TestRunner::EXCEPTION_EXIT); + } + + else { + exit(PHPUnit_TextUI_TestRunner::FAILURE_EXIT); + } + } + } + +/** + * Sets the proper test suite to use and loads the test file in it. + * this method gets called as a callback from the parent class + * + * @return void + */ + protected function handleCustomTestSuite() { + + } +} \ No newline at end of file