From 60590de6bcd8be662b3cc59f1abbdfc8a5cb34cb Mon Sep 17 00:00:00 2001 From: mark_story Date: Sat, 12 Feb 2011 15:51:00 -0500 Subject: [PATCH] Starting to banish TestManager, and replace it with a more normal PHPUnit test loader. --- cake/console/shells/testsuite.php | 9 ++-- cake/tests/lib/cake_test_loader.php | 80 +++++++++++++++++++++++++++++ cake/tests/lib/test_manager.php | 1 + cake/tests/lib/test_runner.php | 18 +++++-- 4 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 cake/tests/lib/cake_test_loader.php diff --git a/cake/console/shells/testsuite.php b/cake/console/shells/testsuite.php index b8eb079b8..f74c53e36 100644 --- a/cake/console/shells/testsuite.php +++ b/cake/console/shells/testsuite.php @@ -165,7 +165,6 @@ class TestSuiteShell extends Shell { $this->_dispatcher = new CakeTestSuiteDispatcher(); $this->_dispatcher->loadTestFramework(); - require_once CAKE . 'tests' . DS . 'lib' . DS . 'test_manager.php'; } /** @@ -178,6 +177,7 @@ class TestSuiteShell extends Shell { return; } $params = array( + 'core' => false, 'app' => false, 'plugin' => null, 'output' => 'text', @@ -185,7 +185,9 @@ class TestSuiteShell extends Shell { $category = $this->args[0]; - if ($category == 'app') { + if ($category == 'core') { + $params['core'] = true; + } elseif ($category == 'app') { $params['app'] = true; } elseif ($category != 'core') { $params['plugin'] = $category; @@ -252,11 +254,12 @@ class TestSuiteShell extends Shell { */ 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'; restore_error_handler(); restore_error_handler(); - $testCli = new TestRunner($runnerArgs); + $testCli = new TestRunner('CakeTestLoader', $runnerArgs); $testCli->run($options); } diff --git a/cake/tests/lib/cake_test_loader.php b/cake/tests/lib/cake_test_loader.php new file mode 100644 index 000000000..c388a410e --- /dev/null +++ b/cake/tests/lib/cake_test_loader.php @@ -0,0 +1,80 @@ +_resolveTestFile($filePath, $params); + + PHPUnit_Util_Class::collectStart(); + PHPUnit_Util_Fileloader::checkAndLoad($file, false); + $loadedClasses = PHPUnit_Util_Class::collectEnd(); + + if (!empty($loadedClasses)) { + $testCaseClass = 'PHPUnit_Framework_TestCase'; + + foreach ($loadedClasses as $loadedClass) { + $class = new ReflectionClass($loadedClass); + $classFile = $class->getFileName(); + + if ($class->isSubclassOf($testCaseClass) && + !$class->isAbstract()) { + $suiteClassName = $loadedClass; + $testCaseClass = $loadedClass; + + if ($classFile == realpath($file)) { + break; + } + } + + if ($class->hasMethod('suite')) { + $method = $class->getMethod('suite'); + + if (!$method->isAbstract() && + $method->isPublic() && + $method->isStatic()) { + $suiteClassName = $loadedClass; + + if ($classFile == realpath($file)) { + break; + } + } + } + } + } + + if (class_exists($suiteClassName, FALSE)) { + $class = new ReflectionClass($suiteClassName); + + if ($class->getFileName() == realpath($file)) { + return $class; + } + } + } + + public function reload(ReflectionClass $aClass) { + return $aClass; + } + +/** + * Convert path fragments used by Cake's test runner to absolute paths that can be fed to PHPUnit. + * + * @return void + */ + protected function _resolveTestFile($filePath, $params) { + $basePath = $this->_basePath($params); + return $basePath . DS . $filePath . '.test.php'; + } + + protected function _basePath($params) { + $result = null; + if (!empty($params['core'])) { + $result = CORE_TEST_CASES; + } elseif (!empty($params['app'])) { + $result = APP_TEST_CASES; + } else if (!empty($params['plugin'])) { + $pluginPath = App::pluginPath($params['plugin']); + $result = $pluginPath . 'tests' . DS . 'cases'; + } + return $result; + } +} diff --git a/cake/tests/lib/test_manager.php b/cake/tests/lib/test_manager.php index 7cb6a3fdd..ea7c6f5eb 100644 --- a/cake/tests/lib/test_manager.php +++ b/cake/tests/lib/test_manager.php @@ -16,6 +16,7 @@ * @since CakePHP(tm) v 1.2.0.4433 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ +var_dump(debug_backtrace()); define('CORE_TEST_CASES', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'cases'); define('CORE_TEST_GROUPS', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'groups'); define('APP_TEST_CASES', TESTS . 'cases'); diff --git a/cake/tests/lib/test_runner.php b/cake/tests/lib/test_runner.php index 22def2a23..ec99cf93a 100644 --- a/cake/tests/lib/test_runner.php +++ b/cake/tests/lib/test_runner.php @@ -16,10 +16,15 @@ * @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 'test_manager.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'); + PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT'); @@ -35,7 +40,10 @@ class TestRunner extends PHPUnit_TextUI_Command { * * @param array $params list of options to be used for this run */ - public function __construct($params = array()) { + public function __construct($loader, $params = array()) { + $this->arguments['loader'] = $loader; + $this->arguments['test'] = $params['case']; + $this->arguments['testFile'] = $params; $this->_params = $params; } @@ -46,12 +54,12 @@ class TestRunner extends PHPUnit_TextUI_Command { * @return void */ protected function handleCustomTestSuite() { - $manager = new TestManager($this->_params); + /*$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