2011-02-12 20:51:00 +00:00
|
|
|
<?php
|
2011-02-14 03:19:49 +00:00
|
|
|
/**
|
|
|
|
* TestLoader for CakePHP Test suite.
|
|
|
|
*
|
|
|
|
* Turns partial paths used on the testsuite console and web UI into full file paths.
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2011-02-14 03:19:49 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-02-14 03:19:49 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-02-14 03:19:49 +00:00
|
|
|
* @since CakePHP(tm) v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2011-09-30 17:47:12 +00:00
|
|
|
* @package Cake.TestSuite
|
2011-02-14 03:19:49 +00:00
|
|
|
*/
|
2011-12-08 15:35:02 +00:00
|
|
|
|
2021-12-21 22:29:35 +00:00
|
|
|
use PHPUnit\Runner\StandardTestSuiteLoader;
|
|
|
|
use PHPUnit\Runner\TestSuiteLoader;
|
|
|
|
|
2011-12-08 15:35:02 +00:00
|
|
|
/**
|
|
|
|
* TestLoader for CakePHP Test suite.
|
|
|
|
*
|
|
|
|
* Turns partial paths used on the testsuite console and web UI into full file paths.
|
|
|
|
*
|
|
|
|
* @package Cake.TestSuite
|
|
|
|
*/
|
2021-12-21 22:29:35 +00:00
|
|
|
class CakeTestLoader implements TestSuiteLoader {
|
2011-02-12 20:51:00 +00:00
|
|
|
|
2021-12-21 22:29:35 +00:00
|
|
|
|
|
|
|
private TestSuiteLoader $testSuiteLoader;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->testSuiteLoader = new StandardTestSuiteLoader();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-13 00:11:47 +00:00
|
|
|
* Load a file and find the first test case / suite in that file.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $filePath The file path to load
|
|
|
|
* @param string $params Additional parameters
|
2011-02-13 00:11:47 +00:00
|
|
|
* @return ReflectionClass
|
|
|
|
*/
|
2021-12-21 22:29:35 +00:00
|
|
|
public function load(string $filePath, string $params = '') : ReflectionClass {
|
2011-02-12 20:51:00 +00:00
|
|
|
$file = $this->_resolveTestFile($filePath, $params);
|
2021-12-21 22:29:35 +00:00
|
|
|
return $this->testSuiteLoader->load('', $file);
|
2011-02-12 20:51:00 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 22:29:35 +00:00
|
|
|
public function reload(ReflectionClass $aClass) : ReflectionClass
|
|
|
|
{
|
|
|
|
return $this->testSuiteLoader->reload($aClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-27 17:36:43 +00:00
|
|
|
* Convert path fragments used by CakePHP's test runner to absolute paths that can be fed to PHPUnit.
|
2011-02-12 20:51:00 +00:00
|
|
|
*
|
2015-09-25 15:22:00 +00:00
|
|
|
* @param string $filePath The file path to load.
|
|
|
|
* @param string $params Additional parameters.
|
|
|
|
* @return string Converted path fragments.
|
2011-02-12 20:51:00 +00:00
|
|
|
*/
|
|
|
|
protected function _resolveTestFile($filePath, $params) {
|
2011-02-13 00:11:47 +00:00
|
|
|
$basePath = $this->_basePath($params) . DS . $filePath;
|
2011-04-11 00:38:24 +00:00
|
|
|
$ending = 'Test.php';
|
2011-02-13 00:11:47 +00:00
|
|
|
return (strpos($basePath, $ending) === (strlen($basePath) - strlen($ending))) ? $basePath : $basePath . $ending;
|
2011-02-12 20:51:00 +00:00
|
|
|
}
|
2011-02-13 00:11:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the base path to a set of tests based on the parameters.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param array $params The path parameters.
|
2011-02-13 00:11:47 +00:00
|
|
|
* @return string The base path.
|
|
|
|
*/
|
2011-02-13 03:49:15 +00:00
|
|
|
protected static function _basePath($params) {
|
2011-02-12 20:51:00 +00:00
|
|
|
$result = null;
|
|
|
|
if (!empty($params['core'])) {
|
|
|
|
$result = CORE_TEST_CASES;
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (!empty($params['plugin'])) {
|
2011-05-18 15:15:39 +00:00
|
|
|
if (!CakePlugin::loaded($params['plugin'])) {
|
2011-05-20 02:05:51 +00:00
|
|
|
try {
|
|
|
|
CakePlugin::load($params['plugin']);
|
2011-05-23 03:28:52 +00:00
|
|
|
$result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
|
2012-03-05 02:51:44 +00:00
|
|
|
} catch (MissingPluginException $e) {
|
|
|
|
}
|
2011-05-23 03:28:52 +00:00
|
|
|
} else {
|
|
|
|
$result = CakePlugin::path($params['plugin']) . 'Test' . DS . 'Case';
|
2011-05-18 15:15:39 +00:00
|
|
|
}
|
2011-12-20 04:13:46 +00:00
|
|
|
} elseif (!empty($params['app'])) {
|
|
|
|
$result = APP_TEST_CASES;
|
2011-02-12 20:51:00 +00:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
2011-02-13 00:11:47 +00:00
|
|
|
|
2011-02-13 03:49:15 +00:00
|
|
|
/**
|
|
|
|
* Get the list of files for the test listing.
|
|
|
|
*
|
2014-06-05 04:19:27 +00:00
|
|
|
* @param string $params Path parameters
|
2012-11-28 22:30:47 +00:00
|
|
|
* @return array
|
2011-02-13 03:49:15 +00:00
|
|
|
*/
|
|
|
|
public static function generateTestList($params) {
|
2015-07-21 08:22:53 +00:00
|
|
|
$directory = static::_basePath($params);
|
|
|
|
$fileList = static::_getRecursiveFileList($directory);
|
2011-02-13 03:49:15 +00:00
|
|
|
|
|
|
|
$testCases = array();
|
|
|
|
foreach ($fileList as $testCaseFile) {
|
2011-04-11 00:38:24 +00:00
|
|
|
$case = str_replace($directory . DS, '', $testCaseFile);
|
|
|
|
$case = str_replace('Test.php', '', $case);
|
|
|
|
$testCases[$testCaseFile] = $case;
|
2011-02-13 03:49:15 +00:00
|
|
|
}
|
2011-04-22 20:29:02 +00:00
|
|
|
sort($testCases);
|
2011-02-13 03:49:15 +00:00
|
|
|
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.
|
2012-11-28 22:30:47 +00:00
|
|
|
* @return array
|
2011-02-13 03:49:15 +00:00
|
|
|
*/
|
|
|
|
protected static function _getRecursiveFileList($directory = '.') {
|
|
|
|
$fileList = array();
|
|
|
|
if (!is_dir($directory)) {
|
|
|
|
return $fileList;
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = new RegexIterator(
|
|
|
|
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)),
|
2011-04-11 00:38:24 +00:00
|
|
|
'/.*Test.php$/'
|
2011-02-13 03:49:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$fileList[] = $file->getPathname();
|
|
|
|
}
|
|
|
|
return $fileList;
|
|
|
|
}
|
|
|
|
|
2011-02-12 20:51:00 +00:00
|
|
|
}
|