Moving methods into the loader class.

Getting rid of lame functions and using Spl more effectively.
This commit is contained in:
mark_story 2011-02-12 22:49:15 -05:00
parent 0c09d08585
commit e5c898a4d2
2 changed files with 42 additions and 117 deletions

View file

@ -85,7 +85,7 @@ class CakeTestLoader implements PHPUnit_Runner_TestSuiteLoader {
* @param array $params
* @return string The base path.
*/
protected function _basePath($params) {
protected static function _basePath($params) {
$result = null;
if (!empty($params['core'])) {
$result = CORE_TEST_CASES;
@ -98,4 +98,44 @@ class CakeTestLoader implements PHPUnit_Runner_TestSuiteLoader {
return $result;
}
/**
* Get the list of files for the test listing.
*
* @return void
*/
public static function generateTestList($params) {
$directory = self::_basePath($params);
$fileList = self::_getRecursiveFileList($directory);
$testCases = array();
foreach ($fileList as $testCaseFile) {
$testCases[$testCaseFile] = str_replace($directory . DS, '', $testCaseFile);
}
return $testCases;
}
/**
* Gets a recursive list of files from a given directory and matches then against
* a given fileTestFunction, like isTestCaseFile()
*
* @param string $directory The directory to scan for files.
* @param mixed $fileTestFunction
*/
protected static function _getRecursiveFileList($directory = '.') {
$fileList = array();
if (!is_dir($directory)) {
return $fileList;
}
$files = new RegexIterator(
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
'/.*\.test.php$/'
);
foreach ($files as $file) {
$fileList[] = $file->getPathname();
}
return $fileList;
}
}

View file

@ -101,125 +101,10 @@ class CakeBaseReporter extends PHPUnit_TextUI_ResultPrinter {
* @return mixed
*/
public function testCaseList() {
$testList = $this->_generateTestList($this->params);
$testList = CakeTestLoader::generateTestList($this->params);
return $testList;
}
/**
* Get the list of files for the test listing.
*
* @return void
*/
protected function _generateTestList($params) {
$directory = self::_getTestsPath($params);
$fileList = self::_getTestFileList($directory);
$testCases = array();
foreach ($fileList as $testCaseFile) {
$testCases[$testCaseFile] = str_replace($directory . DS, '', $testCaseFile);
}
return $testCases;
}
/**
* Returns a list of test files from a given directory
*
* @param string $directory Directory to get test case files from.
* @static
*/
protected static function &_getTestFileList($directory = '.') {
$return = self::_getRecursiveFileList($directory, array('self', '_isTestCaseFile'));
return $return;
}
/**
* Gets a recursive list of files from a given directory and matches then against
* a given fileTestFunction, like isTestCaseFile()
*
* @param string $directory The directory to scan for files.
* @param mixed $fileTestFunction
* @static
*/
protected static function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
$fileList = array();
if (!is_dir($directory)) {
return $fileList;
}
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($files as $file) {
if (!$file->isFile()) {
continue;
}
$file = $file->getRealPath();
if (call_user_func_array($fileTestFunction, array($file))) {
$fileList[] = $file;
}
}
return $fileList;
}
/**
* Extension suffix for test case files.
*
* @var string
*/
protected static $_testExtension = '.test.php';
/**
* Tests if a file has the correct test case extension
*
* @param string $file
* @return boolean Whether $file is a test case.
* @static
*/
protected static function _isTestCaseFile($file) {
return self::_hasExpectedExtension($file, self::$_testExtension);
}
/**
* Check if a file has a specific extension
*
* @param string $file
* @param string $extension
* @return void
* @static
*/
protected static function _hasExpectedExtension($file, $extension) {
return $extension == strtolower(substr($file, (0 - strlen($extension))));
}
/**
* Returns the given path to the test files depending on a given type of tests (core, app, plugin)
*
* @param array $params Array of parameters for getting test paths.
* Can contain app, type, and plugin params.
* @return string The path tests are located on
* @static
*/
protected static function _getTestsPath($params) {
$result = null;
if (!empty($params['app'])) {
$result = APP_TEST_CASES;
} else if (!empty($params['plugin'])) {
$pluginPath = App::pluginPath($params['plugin']);
$result = $pluginPath . 'tests' . DS . 'cases';
} else {
$result = CORE_TEST_CASES;
}
return $result;
}
/**
* Get the extension for either 'group' or 'test' types.
*
* @param string $type Type of test to get, either 'test' or 'group'
* @return string Extension suffix for test.
*/
public static function getExtension($type = 'test') {
return self::$_testExtension;
}
/**
* Paints the start of the response from the test suite.
* Used to paint things like head elements in an html page.