Replace hard coded test directory paths with constants, fixes #12636

The `TESTS` constant definition is moved from bootstrap.php in order
to make it possible to set the constant in test.php or in the project's
bootstrap file so that CakePHP would detect tests in a different folder.
This commit is contained in:
bancer 2018-10-26 13:49:53 +02:00
parent 2acad7e94d
commit 53e3df20d0
3 changed files with 24 additions and 20 deletions

View file

@ -360,23 +360,19 @@ class TestShell extends Shell {
} }
$testFile = $testCase = null; $testFile = $testCase = null;
$testCaseFolder = str_replace(APP, '', APP_TEST_CASES);
if (preg_match('@Test[\\\/]@', $file)) { if (preg_match('@Test[\\\/]@', $file)) {
if (substr($file, -8) === 'Test.php') { if (substr($file, -8) === 'Test.php') {
$testCase = substr($file, 0, -8); $testCase = substr($file, 0, -8);
$testCase = str_replace(DS, '/', $testCase); $testCase = str_replace(DS, '/', $testCase);
$testCaseFolderEscaped = str_replace('/', '\/', $testCaseFolder);
if ($testCase = preg_replace('@.*Test\/Case\/@', '', $testCase)) { $testCase = preg_replace('@.*' . $testCaseFolderEscaped . '\/@', '', $testCase);
if (!empty($testCase)) {
if ($category === 'core') { if ($category === 'core') {
$testCase = str_replace('lib/Cake', '', $testCase); $testCase = str_replace('lib/Cake', '', $testCase);
} }
return $testCase; return $testCase;
} }
throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile)); throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
} }
} }
@ -397,11 +393,11 @@ class TestShell extends Shell {
} }
if ($category === 'app') { if ($category === 'app') {
$testFile = str_replace(APP, APP . 'Test/Case/', $file) . 'Test.php'; $testFile = str_replace(APP, APP_TEST_CASES . '/', $file) . 'Test.php';
} else { } else {
$testFile = preg_replace( $testFile = preg_replace(
"@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@", "@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@",
'\1Test/Case/\2Test.php', '\1' . $testCaseFolder . '/\2Test.php',
$file $file
); );
} }
@ -412,8 +408,7 @@ class TestShell extends Shell {
$testCase = substr($testFile, 0, -8); $testCase = substr($testFile, 0, -8);
$testCase = str_replace(DS, '/', $testCase); $testCase = str_replace(DS, '/', $testCase);
$testCase = preg_replace('@.*Test/Case/@', '', $testCase); $testCase = preg_replace('@.*' . $testCaseFolder . '/@', '', $testCase);
return $testCase; return $testCase;
} }

View file

@ -16,8 +16,24 @@
* @license https://opensource.org/licenses/mit-license.php MIT License * @license https://opensource.org/licenses/mit-license.php MIT License
*/ */
/**
* Path to the tests directory of the app.
*/
if (!defined('TESTS')) {
define('TESTS', APP . 'Test' . DS);
}
/**
* Path to the test cases directory of CakePHP.
*/
define('CORE_TEST_CASES', CAKE . 'Test' . DS . 'Case'); define('CORE_TEST_CASES', CAKE . 'Test' . DS . 'Case');
define('APP_TEST_CASES', TESTS . 'Case');
/**
* Path to the test cases directory of the app.
*/
if (!defined('APP_TEST_CASES')) {
define('APP_TEST_CASES', TESTS . 'Case');
}
App::uses('CakeTestSuiteCommand', 'TestSuite'); App::uses('CakeTestSuiteCommand', 'TestSuite');

View file

@ -86,13 +86,6 @@ if (!defined('IMAGES')) {
define('IMAGES', WWW_ROOT . 'img' . DS); define('IMAGES', WWW_ROOT . 'img' . DS);
} }
/**
* Path to the tests directory.
*/
if (!defined('TESTS')) {
define('TESTS', APP . 'Test' . DS);
}
/** /**
* Path to the temporary files directory. * Path to the temporary files directory.
*/ */