Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bancer committed Oct 26, 2018
1 parent 2acad7e commit 53e3df2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
19 changes: 7 additions & 12 deletions lib/Cake/Console/Command/TestShell.php
Expand Up @@ -360,23 +360,19 @@ protected function _mapFileToCase($file, $category, $throwOnMissingFile = true)
}

$testFile = $testCase = null;

$testCaseFolder = str_replace(APP, '', APP_TEST_CASES);
if (preg_match('@Test[\\\/]@', $file)) {

if (substr($file, -8) === 'Test.php') {

$testCase = substr($file, 0, -8);
$testCase = str_replace(DS, '/', $testCase);

if ($testCase = preg_replace('@.*Test\/Case\/@', '', $testCase)) {

$testCaseFolderEscaped = str_replace('/', '\/', $testCaseFolder);
$testCase = preg_replace('@.*' . $testCaseFolderEscaped . '\/@', '', $testCase);
if (!empty($testCase)) {
if ($category === 'core') {
$testCase = str_replace('lib/Cake', '', $testCase);
}

return $testCase;
}

throw new Exception(__d('cake_dev', 'Test case %s cannot be run via this shell', $testFile));
}
}
Expand All @@ -397,11 +393,11 @@ protected function _mapFileToCase($file, $category, $throwOnMissingFile = true)
}

if ($category === 'app') {
$testFile = str_replace(APP, APP . 'Test/Case/', $file) . 'Test.php';
$testFile = str_replace(APP, APP_TEST_CASES . '/', $file) . 'Test.php';
} else {
$testFile = preg_replace(
"@((?:plugins|Plugin)[\\/]{$category}[\\/])(.*)$@",
'\1Test/Case/\2Test.php',
'\1' . $testCaseFolder . '/\2Test.php',
$file
);
}
Expand All @@ -412,8 +408,7 @@ protected function _mapFileToCase($file, $category, $throwOnMissingFile = true)

$testCase = substr($testFile, 0, -8);
$testCase = str_replace(DS, '/', $testCase);
$testCase = preg_replace('@.*Test/Case/@', '', $testCase);

$testCase = preg_replace('@.*' . $testCaseFolder . '/@', '', $testCase);
return $testCase;
}

Expand Down
18 changes: 17 additions & 1 deletion lib/Cake/TestSuite/CakeTestSuiteDispatcher.php
Expand Up @@ -16,8 +16,24 @@
* @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('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');

Expand Down
7 changes: 0 additions & 7 deletions lib/Cake/bootstrap.php
Expand Up @@ -86,13 +86,6 @@
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.
*/
Expand Down

0 comments on commit 53e3df2

Please sign in to comment.