2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2010-01-10 02:24:27 +00:00
|
|
|
* TestManager for CakePHP Test suite.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2010-01-26 19:18:20 +00:00
|
|
|
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.lib
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4433
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2008-11-08 02:58:37 +00:00
|
|
|
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');
|
|
|
|
define('APP_TEST_GROUPS', TESTS . 'groups');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-09-28 03:07:23 +00:00
|
|
|
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
|
2010-05-08 20:25:16 +00:00
|
|
|
require_once CAKE_TESTS_LIB . 'cake_test_suite.php';
|
2010-05-08 20:18:45 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2010-01-26 14:14:47 +00:00
|
|
|
* TestManager is the base class that handles loading and initiating the running
|
2010-01-04 03:48:03 +00:00
|
|
|
* of TestCase and TestSuite classes that the user has selected.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-12-24 18:57:20 +00:00
|
|
|
* @package cake.tests.lib
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
|
|
|
class TestManager {
|
2010-01-04 03:48:03 +00:00
|
|
|
/**
|
|
|
|
* Extension suffix for test case files.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-05-05 01:21:58 +00:00
|
|
|
protected static $_testExtension = '.test.php';
|
2010-01-04 03:48:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this test an AppTest?
|
|
|
|
*
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $appTest = false;
|
2010-01-04 03:48:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this test a plugin test?
|
|
|
|
*
|
|
|
|
* @var mixed boolean false or string name of the plugin being used.
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $pluginTest = false;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-27 02:21:34 +00:00
|
|
|
/**
|
|
|
|
* String to filter test case method names by.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $filter = false;
|
|
|
|
|
2010-05-04 22:55:15 +00:00
|
|
|
/**
|
|
|
|
* TestSuite container for single or grouped test files
|
|
|
|
*
|
2010-05-08 20:25:16 +00:00
|
|
|
* @var PHPUnit_Framework_TestSuite
|
2010-05-04 22:55:15 +00:00
|
|
|
*/
|
2010-06-09 22:37:24 +00:00
|
|
|
protected $_testSuite = null;
|
2010-05-04 22:55:15 +00:00
|
|
|
|
2010-05-08 20:25:16 +00:00
|
|
|
/**
|
|
|
|
* Object instance responsible for managing the test fixtures
|
|
|
|
*
|
|
|
|
* @var CakeFixtureManager
|
|
|
|
*/
|
|
|
|
protected $_fixtureManager = null;
|
|
|
|
|
2010-07-07 03:49:37 +00:00
|
|
|
/**
|
|
|
|
* Params to configure test runner
|
|
|
|
*
|
|
|
|
* @var CakeFixtureManager
|
|
|
|
*/
|
|
|
|
public $params = array();
|
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* Constructor for the TestManager class
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-06-11 02:29:03 +00:00
|
|
|
public function __construct($params = array()) {
|
2010-05-04 18:19:10 +00:00
|
|
|
require_once(CAKE_TESTS_LIB . 'cake_test_case.php');
|
2010-12-15 03:58:27 +00:00
|
|
|
require_once(CAKE_TESTS_LIB . 'controller_test_case.php');
|
2010-07-07 03:49:37 +00:00
|
|
|
|
|
|
|
$this->params = $params;
|
2010-05-27 02:21:34 +00:00
|
|
|
if (isset($params['app'])) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->appTest = true;
|
|
|
|
}
|
2010-05-27 02:21:34 +00:00
|
|
|
if (isset($params['plugin'])) {
|
|
|
|
$this->pluginTest = htmlentities($params['plugin']);
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
isset($params['filter']) &&
|
|
|
|
$params['filter'] !== false &&
|
|
|
|
preg_match('/^[a-zA-Z0-9_]/', $params['filter'])
|
|
|
|
) {
|
|
|
|
$this->filter = '/' . $params['filter'] . '/';
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* Runs a specific test case file
|
|
|
|
*
|
2010-01-04 03:48:03 +00:00
|
|
|
* @param string $testCaseFile Filename of the test to be run.
|
2010-05-04 18:19:10 +00:00
|
|
|
* @param PHPUnit_Framework_TestListener $reporter Reporter instance to attach to the test case.
|
2010-05-04 22:55:15 +00:00
|
|
|
* @throws InvalidArgumentException if the supplied $testCaseFile does not exists
|
2010-01-10 02:22:41 +00:00
|
|
|
* @return mixed Result of test case being run.
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-05-08 20:18:45 +00:00
|
|
|
public function runTestCase($testCaseFile, PHPUnit_Framework_TestListener $reporter, $codeCoverage = false) {
|
2010-07-07 03:49:37 +00:00
|
|
|
$this->loadCase($testCaseFile);
|
2010-05-08 20:18:45 +00:00
|
|
|
return $this->run($reporter, $codeCoverage);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-05-05 03:41:27 +00:00
|
|
|
/**
|
|
|
|
* Runs the main testSuite and attaches to it a reporter
|
|
|
|
*
|
|
|
|
* @param PHPUnit_Framework_TestListener $reporter Reporter instance to use with the group test being run.
|
2010-06-26 17:48:55 +00:00
|
|
|
* @return PHPUnit_Framework_TestResult Result object of the test run.
|
2010-05-05 03:41:27 +00:00
|
|
|
*/
|
2010-05-08 20:18:45 +00:00
|
|
|
protected function run($reporter, $codeCoverage = false) {
|
2010-06-22 02:54:41 +00:00
|
|
|
restore_error_handler();
|
|
|
|
restore_error_handler();
|
|
|
|
|
2010-05-05 03:41:27 +00:00
|
|
|
$result = new PHPUnit_Framework_TestResult;
|
2010-05-08 20:18:45 +00:00
|
|
|
$result->collectCodeCoverageInformation($codeCoverage);
|
2010-05-05 03:41:27 +00:00
|
|
|
$result->addListener($reporter);
|
|
|
|
$reporter->paintHeader();
|
2010-05-08 20:25:16 +00:00
|
|
|
$testSuite = $this->getTestSuite();
|
|
|
|
$testSuite->setFixtureManager($this->getFixtureManager());
|
2010-05-27 02:21:34 +00:00
|
|
|
$testSuite->run($result, $this->filter);
|
2010-05-05 03:41:27 +00:00
|
|
|
$reporter->paintResult($result);
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-07-07 04:01:21 +00:00
|
|
|
/**
|
|
|
|
* Loads a test case in a test suite, if the test suite is null it will create it
|
|
|
|
*
|
|
|
|
* @param string Test file path
|
|
|
|
* @param PHPUnit_Framework_TestSuite $suite the test suite to load the case in
|
|
|
|
* @throws InvalidArgumentException if test case file is not found
|
|
|
|
* @return PHPUnit_Framework_TestSuite the suite with the test case loaded
|
|
|
|
*/
|
|
|
|
public function loadCase($testCaseFile, PHPUnit_Framework_TestSuite $suite = null) {
|
|
|
|
$testCaseFileWithPath = $this->_getTestsPath($this->params) . DS . $testCaseFile;
|
|
|
|
|
|
|
|
if (!file_exists($testCaseFileWithPath) || strpos($testCaseFileWithPath, '..')) {
|
2010-12-05 01:37:13 +00:00
|
|
|
throw new InvalidArgumentException(__('Unable to load test file %s', htmlentities($testCaseFile)));
|
2010-07-07 04:01:21 +00:00
|
|
|
}
|
|
|
|
if (!$suite) {
|
2010-12-05 01:37:13 +00:00
|
|
|
$suite = $this->getTestSuite(__('Individual test case: %s', $testCaseFile));
|
2010-07-07 04:01:21 +00:00
|
|
|
}
|
|
|
|
$suite->addTestFile($testCaseFileWithPath);
|
|
|
|
|
|
|
|
return $suite;
|
|
|
|
}
|
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* Returns a list of test cases found in the current valid test case path
|
|
|
|
*
|
|
|
|
* @access public
|
2010-01-10 02:22:41 +00:00
|
|
|
* @static
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-06-09 22:37:24 +00:00
|
|
|
public static function getTestCaseList($params) {
|
|
|
|
$directory = self::_getTestsPath($params);
|
2010-05-05 01:21:58 +00:00
|
|
|
$fileList = self::_getTestFileList($directory);
|
2010-06-09 22:37:24 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
$testCases = array();
|
|
|
|
foreach ($fileList as $testCaseFile) {
|
|
|
|
$testCases[$testCaseFile] = str_replace($directory . DS, '', $testCaseFile);
|
|
|
|
}
|
|
|
|
return $testCases;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
2008-09-07 14:21:21 +00:00
|
|
|
* Returns a list of test files from a given directory
|
2008-07-30 13:37:06 +00:00
|
|
|
*
|
2010-01-10 00:52:52 +00:00
|
|
|
* @param string $directory Directory to get test case files from.
|
2010-05-05 01:21:58 +00:00
|
|
|
* @static
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-05-05 01:21:58 +00:00
|
|
|
protected static function &_getTestFileList($directory = '.') {
|
2010-05-13 21:48:34 +00:00
|
|
|
$return = self::_getRecursiveFileList($directory, array('self', '_isTestCaseFile'));
|
2008-05-30 11:40:08 +00:00
|
|
|
return $return;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* Gets a recursive list of files from a given directory and matches then against
|
|
|
|
* a given fileTestFunction, like isTestCaseFile()
|
|
|
|
*
|
2010-01-04 03:48:03 +00:00
|
|
|
* @param string $directory The directory to scan for files.
|
|
|
|
* @param mixed $fileTestFunction
|
2010-05-05 01:21:58 +00:00
|
|
|
* @static
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-05-05 01:21:58 +00:00
|
|
|
protected static function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$fileList = array();
|
|
|
|
if (!is_dir($directory)) {
|
|
|
|
return $fileList;
|
|
|
|
}
|
|
|
|
|
2010-05-04 22:55:15 +00:00
|
|
|
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
|
2009-02-16 00:45:50 +00:00
|
|
|
|
2008-11-06 23:35:08 +00:00
|
|
|
foreach ($files as $file) {
|
2010-05-04 22:55:15 +00:00
|
|
|
if (!$file->isFile()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$file = $file->getRealPath();
|
2010-05-05 01:21:58 +00:00
|
|
|
|
|
|
|
if (call_user_func_array($fileTestFunction, array($file))) {
|
2008-11-06 23:35:08 +00:00
|
|
|
$fileList[] = $file;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fileList;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* Tests if a file has the correct test case extension
|
|
|
|
*
|
2008-09-07 14:21:21 +00:00
|
|
|
* @param string $file
|
2010-01-10 02:22:41 +00:00
|
|
|
* @return boolean Whether $file is a test case.
|
2010-05-05 01:21:58 +00:00
|
|
|
* @static
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-05-05 01:21:58 +00:00
|
|
|
protected static function _isTestCaseFile($file) {
|
|
|
|
return self::_hasExpectedExtension($file, self::$_testExtension);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
|
|
|
* Check if a file has a specific extension
|
|
|
|
*
|
2008-09-07 14:21:21 +00:00
|
|
|
* @param string $file
|
|
|
|
* @param string $extension
|
2008-07-30 13:37:06 +00:00
|
|
|
* @return void
|
2010-05-05 01:21:58 +00:00
|
|
|
* @static
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-05-05 01:21:58 +00:00
|
|
|
protected static function _hasExpectedExtension($file, $extension) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $extension == strtolower(substr($file, (0 - strlen($extension))));
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-07-30 13:37:06 +00:00
|
|
|
/**
|
2010-06-26 16:58:03 +00:00
|
|
|
* Returns the given path to the test files depending on a given type of tests (core, app, plugin)
|
2008-07-30 13:37:06 +00:00
|
|
|
*
|
2010-06-09 22:37:24 +00:00
|
|
|
* @param array $params Array of parameters for getting test paths.
|
|
|
|
* Can contain app, type, and plugin params.
|
2010-01-04 03:48:03 +00:00
|
|
|
* @return string The path tests are located on
|
2010-05-05 01:34:18 +00:00
|
|
|
* @static
|
2008-07-30 13:37:06 +00:00
|
|
|
*/
|
2010-06-09 22:37:24 +00:00
|
|
|
protected static function _getTestsPath($params) {
|
2010-06-11 02:29:03 +00:00
|
|
|
$result = null;
|
2010-06-09 22:37:24 +00:00
|
|
|
if (!empty($params['app'])) {
|
2010-06-26 16:58:03 +00:00
|
|
|
$result = APP_TEST_CASES;
|
2010-06-09 22:37:24 +00:00
|
|
|
} else if (!empty($params['plugin'])) {
|
|
|
|
$pluginPath = App::pluginPath($params['plugin']);
|
2010-06-26 16:58:03 +00:00
|
|
|
$result = $pluginPath . 'tests' . DS . 'cases';
|
|
|
|
} else {
|
|
|
|
$result = CORE_TEST_CASES;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-07 11:33:13 +00:00
|
|
|
/**
|
2010-01-04 03:48:03 +00:00
|
|
|
* Get the extension for either 'group' or 'test' types.
|
2008-11-07 11:33:13 +00:00
|
|
|
*
|
2010-01-04 03:48:03 +00:00
|
|
|
* @param string $type Type of test to get, either 'test' or 'group'
|
|
|
|
* @return string Extension suffix for test.
|
2008-11-07 11:33:13 +00:00
|
|
|
*/
|
2010-05-05 01:21:58 +00:00
|
|
|
public static function getExtension($type = 'test') {
|
2010-06-29 02:44:13 +00:00
|
|
|
return self::$_testExtension;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-04 18:19:10 +00:00
|
|
|
|
2010-05-04 22:55:15 +00:00
|
|
|
/**
|
|
|
|
* Get the container testSuite instance for this runner or creates a new one
|
|
|
|
*
|
|
|
|
* @param string $name The name for the container test suite
|
|
|
|
* @return PHPUnit_Framework_TestSuite container test suite
|
|
|
|
*/
|
2010-07-07 04:01:21 +00:00
|
|
|
public function getTestSuite($name = '') {
|
2010-05-04 22:55:15 +00:00
|
|
|
if (!empty($this->_testSuite)) {
|
|
|
|
return $this->_testSuite;
|
|
|
|
}
|
2010-05-08 20:25:16 +00:00
|
|
|
return $this->_testSuite = new CakeTestSuite($name);
|
2010-05-04 22:55:15 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 21:59:33 +00:00
|
|
|
/**
|
|
|
|
* Get an instance of a Fixture manager to be used by the test cases
|
|
|
|
*
|
|
|
|
* @return CakeFixtureManager fixture manager
|
|
|
|
*/
|
2010-07-07 04:01:21 +00:00
|
|
|
public function getFixtureManager() {
|
2010-05-08 20:25:16 +00:00
|
|
|
if (!empty($this->_fixtureManager)) {
|
|
|
|
return $this->_fixtureManager;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2010-05-08 20:25:16 +00:00
|
|
|
return $this->_fixtureManager = new CakeFixtureManager;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
}
|