3
* TestManager for CakePHP Test suite.
7
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The Open Group Test Suite License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
16
* @subpackage cake.cake.tests.lib
17
* @since CakePHP(tm) v 1.2.0.4433
18
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
20
define('CORE_TEST_CASES', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'cases');
21
define('CORE_TEST_GROUPS', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'groups');
22
define('APP_TEST_CASES', TESTS . 'cases');
23
define('APP_TEST_GROUPS', TESTS . 'groups');
26
* TestManager is the base class that handles loading and initiating the running
27
* of TestCase and TestSuite classes that the user has selected.
30
* @subpackage cake.cake.tests.lib
34
* Extension suffix for test case files.
38
var $_testExtension = '.test.php';
41
* Extension suffix for group test case files.
45
var $_groupExtension = '.group.php';
48
* Is this test an AppTest?
55
* Is this test a plugin test?
57
* @var mixed boolean false or string name of the plugin being used.
59
var $pluginTest = false;
62
* Constructor for the TestManager class
67
function TestManager() {
68
$this->_installSimpleTest();
69
if (isset($_GET['app'])) {
70
$this->appTest = true;
72
if (isset($_GET['plugin'])) {
73
$this->pluginTest = htmlentities($_GET['plugin']);
78
* Includes the required simpletest files in order for the testsuite to run
83
function _installSimpleTest() {
84
App::import('Vendor', array(
85
'simpletest' . DS . 'unit_tester',
86
'simpletest' . DS . 'mock_objects',
87
'simpletest' . DS . 'web_tester'
89
require_once(CAKE_TESTS_LIB . 'cake_web_test_case.php');
90
require_once(CAKE_TESTS_LIB . 'cake_test_case.php');
94
* Runs all tests in the Application depending on the current appTest setting
96
* @param Object $reporter Reporter object for the tests being run.
97
* @param boolean $testing Are tests supposed to be auto run. Set to true to return testcase list.
101
function runAllTests(&$reporter, $testing = false) {
102
$testCases =& $this->_getTestFileList($this->_getTestsPath());
103
if ($this->appTest) {
104
$test =& new TestSuite(__('All App Tests', true));
105
} else if ($this->pluginTest) {
106
$test =& new TestSuite(sprintf(__('All %s Plugin Tests', true), Inflector::humanize($this->pluginTest)));
108
$test =& new TestSuite(__('All Core Tests', true));
115
foreach ($testCases as $testCase) {
116
$test->addTestFile($testCase);
119
return $test->run($reporter);
123
* Runs a specific test case file
125
* @param string $testCaseFile Filename of the test to be run.
126
* @param Object $reporter Reporter instance to attach to the test case.
127
* @param boolean $testing Set to true if testing, otherwise test case will be run.
128
* @return mixed Result of test case being run.
131
function runTestCase($testCaseFile, &$reporter, $testing = false) {
132
$testCaseFileWithPath = $this->_getTestsPath() . DS . $testCaseFile;
134
if (!file_exists($testCaseFileWithPath) || strpos($testCaseFileWithPath, '..')) {
136
sprintf(__("Test case %s cannot be found", true), htmlentities($testCaseFile)),
146
$test =& new TestSuite(sprintf(__('Individual test case: %s', true), $testCaseFile));
147
$test->addTestFile($testCaseFileWithPath);
148
return $test->run($reporter);
152
* Runs a specific group test file
154
* @param string $groupTestName GroupTest that you want to run.
155
* @param Object $reporter Reporter instance to use with the group test being run.
156
* @return mixed Results of group test being run.
159
function runGroupTest($groupTestName, &$reporter) {
160
$filePath = $this->_getTestsPath('groups') . DS . strtolower($groupTestName) . $this->_groupExtension;
162
if (!file_exists($filePath) || strpos($filePath, '..')) {
163
trigger_error(sprintf(
164
__("Group test %s cannot be found at %s", true),
165
htmlentities($groupTestName),
166
htmlentities($filePath)
172
require_once $filePath;
173
$test =& new TestSuite(sprintf(__('%s group test', true), $groupTestName));
174
foreach ($this->_getGroupTestClassNames($filePath) as $groupTest) {
175
$testCase = new $groupTest();
176
$test->addTestCase($testCase);
177
if (isset($testCase->label)) {
178
$test->_label = $testCase->label;
181
return $test->run($reporter);
185
* Adds all testcases in a given directory to a given GroupTest object
187
* @param object $groupTest Instance of TestSuite/GroupTest that files are to be added to.
188
* @param string $directory The directory to add tests from.
193
function addTestCasesFromDirectory(&$groupTest, $directory = '.') {
194
$manager =& new TestManager();
195
$testCases =& $manager->_getTestFileList($directory);
196
foreach ($testCases as $testCase) {
197
$groupTest->addTestFile($testCase);
202
* Adds a specific test file and thereby all of its test cases and group tests to a given group test file
204
* @param object $groupTest Instance of TestSuite/GroupTest that a file should be added to.
205
* @param string $file The file name, minus the suffix to add.
210
function addTestFile(&$groupTest, $file) {
211
$manager =& new TestManager();
213
if (file_exists($file . $manager->_testExtension)) {
214
$file .= $manager->_testExtension;
215
} elseif (file_exists($file . $manager->_groupExtension)) {
216
$file .= $manager->_groupExtension;
218
$groupTest->addTestFile($file);
222
* Returns a list of test cases found in the current valid test case path
227
function &getTestCaseList() {
228
$manager =& new TestManager();
229
$return = $manager->_getTestCaseList($manager->_getTestsPath());
234
* Builds the list of test cases from a given directory
236
* @param string $directory Directory to get test case list from.
239
function &_getTestCaseList($directory = '.') {
240
$fileList =& $this->_getTestFileList($directory);
241
$testCases = array();
242
foreach ($fileList as $testCaseFile) {
243
$testCases[$testCaseFile] = str_replace($directory . DS, '', $testCaseFile);
249
* Returns a list of test files from a given directory
251
* @param string $directory Directory to get test case files from.
254
function &_getTestFileList($directory = '.') {
255
$return = $this->_getRecursiveFileList($directory, array(&$this, '_isTestCaseFile'));
260
* Returns a list of group tests found in the current valid test case path
265
function &getGroupTestList() {
266
$manager =& new TestManager();
267
$return = $manager->_getTestGroupList($manager->_getTestsPath('groups'));
272
* Returns a list of group test files from a given directory
274
* @param string $directory The directory to get group test files from.
277
function &_getTestGroupFileList($directory = '.') {
278
$return = $this->_getRecursiveFileList($directory, array(&$this, '_isTestGroupFile'));
283
* Returns a list of group test files from a given directory
285
* @param string $directory The directory to get group tests from.
288
function &_getTestGroupList($directory = '.') {
289
$fileList =& $this->_getTestGroupFileList($directory);
290
$groupTests = array();
292
foreach ($fileList as $groupTestFile) {
293
$groupTests[$groupTestFile] = str_replace($this->_groupExtension, '', basename($groupTestFile));
300
* Returns a list of class names from a group test file
302
* @param string $groupTestFile The groupTest file to scan for TestSuite classnames.
305
function &_getGroupTestClassNames($groupTestFile) {
306
$file = implode("\n", file($groupTestFile));
307
preg_match("~lass\s+?(.*)\s+?extends TestSuite~", $file, $matches);
308
if (!empty($matches)) {
317
* Gets a recursive list of files from a given directory and matches then against
318
* a given fileTestFunction, like isTestCaseFile()
320
* @param string $directory The directory to scan for files.
321
* @param mixed $fileTestFunction
324
function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
326
if (!is_dir($directory)) {
330
$files = glob($directory . DS . '*');
331
$files = $files ? $files : array();
333
foreach ($files as $file) {
335
$fileList = array_merge($fileList, $this->_getRecursiveFileList($file, $fileTestFunction));
336
} elseif ($fileTestFunction[0]->$fileTestFunction[1]($file)) {
344
* Tests if a file has the correct test case extension
346
* @param string $file
347
* @return boolean Whether $file is a test case.
350
function _isTestCaseFile($file) {
351
return $this->_hasExpectedExtension($file, $this->_testExtension);
355
* Tests if a file has the correct group test extension
357
* @param string $file
358
* @return boolean Whether $file is a group
361
function _isTestGroupFile($file) {
362
return $this->_hasExpectedExtension($file, $this->_groupExtension);
366
* Check if a file has a specific extension
368
* @param string $file
369
* @param string $extension
373
function _hasExpectedExtension($file, $extension) {
374
return $extension == strtolower(substr($file, (0 - strlen($extension))));
378
* Returns the given path to the test files depending on a given type of tests (cases, group, ..)
380
* @param string $type either 'cases' or 'groups'
381
* @return string The path tests are located on
384
function _getTestsPath($type = 'cases') {
385
if (!empty($this->appTest)) {
386
if ($type == 'cases') {
387
$result = APP_TEST_CASES;
388
} else if ($type == 'groups') {
389
$result = APP_TEST_GROUPS;
391
} else if (!empty($this->pluginTest)) {
392
$_pluginBasePath = APP . 'plugins' . DS . $this->pluginTest . DS . 'tests';
393
$pluginPath = App::pluginPath($this->pluginTest);
394
if (file_exists($pluginPath . DS . 'tests')) {
395
$_pluginBasePath = $pluginPath . DS . 'tests';
397
$result = $_pluginBasePath . DS . $type;
399
if ($type == 'cases') {
400
$result = CORE_TEST_CASES;
401
} else if ($type == 'groups') {
402
$result = CORE_TEST_GROUPS;
409
* Get the extension for either 'group' or 'test' types.
411
* @param string $type Type of test to get, either 'test' or 'group'
412
* @return string Extension suffix for test.
415
function getExtension($type = 'test') {
416
if ($type == 'test' || $type == 'case') {
417
return $this->_testExtension;
419
return $this->_groupExtension;