/
test_manager.php
458 lines (415 loc) · 12.1 KB
/
test_manager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* Short description for file.
*
* PHP versions 4 and 5
*
* CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
* @package cake
* @subpackage cake.cake.tests.lib
* @since CakePHP(tm) v 1.2.0.4433
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
define('CORE_TEST_CASES', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'cases');
define('CORE_TEST_GROUPS', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'groups');
define('APP_TEST_CASES', TESTS . 'cases');
define('APP_TEST_GROUPS', TESTS . 'groups');
/**
* TestManager is the base class that handles loading and initiating the running
* of TestCase and TestSuite classes that the user has selected.
*
* @package cake
* @subpackage cake.cake.tests.lib
*/
class TestManager {
/**
* Extension suffix for test case files.
*
* @var string
*/
var $_testExtension = '.test.php';
/**
* Extension suffix for group test case files.
*
* @var string
*/
var $_groupExtension = '.group.php';
/**
* Is this test an AppTest?
*
* @var boolean
*/
var $appTest = false;
/**
* Is this test a plugin test?
*
* @var mixed boolean false or string name of the plugin being used.
*/
var $pluginTest = false;
/**
* Constructor for the TestManager class
*
* @return void
* @access public
*/
function TestManager() {
$this->_installSimpleTest();
if (isset($_GET['app'])) {
$this->appTest = true;
}
if (isset($_GET['plugin'])) {
$this->pluginTest = $_GET['plugin'];
}
}
/**
* Includes the required simpletest files in order for the testsuite to run
*
* @return void
* @access public
*/
function _installSimpleTest() {
App::import('Vendor', array(
'simpletest' . DS . 'unit_tester',
'simpletest' . DS . 'mock_objects',
'simpletest' . DS . 'web_tester'
));
require_once(CAKE_TESTS_LIB . 'cake_web_test_case.php');
require_once(CAKE_TESTS_LIB . 'cake_test_case.php');
}
/**
* Runs all tests in the Application depending on the current appTest setting
*
* @param Object $reporter Reporter object for the tests being run.
* @param boolean $testing Are tests supposed to be auto run. Set to true to return testcase list.
* @return mixed
* @access public
* @static
*/
function runAllTests(&$reporter, $testing = false) {
$manager =& new TestManager();
$testCases =& $manager->_getTestFileList($manager->_getTestsPath());
if ($manager->appTest) {
$test =& new TestSuite('All App Tests');
} else if ($manager->pluginTest) {
$test =& new TestSuite('All ' . Inflector::humanize($manager->pluginTest) . ' Plugin Tests');
} else {
$test =& new TestSuite('All Core Tests');
}
if ($testing) {
return $testCases;
}
foreach ($testCases as $testCase) {
$test->addTestFile($testCase);
}
return $test->run($reporter);
}
/**
* Runs a specific test case file
*
* @param string $testCaseFile Filename of the test to be run.
* @param Object $reporter Reporter instance to attach to the test case.
* @param boolean $testing Set to true if testing, otherwise test case will be run.
* @return mixed
* @access public
*/
function runTestCase($testCaseFile, &$reporter, $testing = false) {
$testCaseFileWithPath = $this->_getTestsPath() . DS . $testCaseFile;
if (!file_exists($testCaseFileWithPath)) {
trigger_error("Test case {$testCaseFile} cannot be found", E_USER_ERROR);
return false;
}
if ($testing) {
return true;
}
$test =& new TestSuite("Individual test case: " . $testCaseFile);
$test->addTestFile($testCaseFileWithPath);
return $test->run($reporter);
}
/**
* Runs a specific group test file
*
* @param string $groupTestName GroupTest that you want to run.
* @param Object $reporter Reporter instance to use with the group test being run.
* @return mixed
* @access public
*/
function runGroupTest($groupTestName, &$reporter) {
$filePath = $this->_getTestsPath('groups') . DS . strtolower($groupTestName) . $this->_groupExtension;
if (!file_exists($filePath)) {
trigger_error("Group test {$groupTestName} cannot be found at {$filePath}", E_USER_ERROR);
}
require_once $filePath;
$test =& new TestSuite($groupTestName . ' group test');
foreach ($this->_getGroupTestClassNames($filePath) as $groupTest) {
$testCase = new $groupTest();
$test->addTestCase($testCase);
if (isset($testCase->label)) {
$test->_label = $testCase->label;
}
}
return $test->run($reporter);
}
/**
* Adds all testcases in a given directory to a given GroupTest object
*
* @param object $groupTest Instance of TestSuite/GroupTest that files are to be added to.
* @param string $directory The directory to add tests from.
* @return void
* @access public
* @static
*/
function addTestCasesFromDirectory(&$groupTest, $directory = '.') {
$manager =& new TestManager();
$testCases =& $manager->_getTestFileList($directory);
foreach ($testCases as $testCase) {
$groupTest->addTestFile($testCase);
}
}
/**
* Adds a specific test file and thereby all of its test cases and group tests to a given group test file
*
* @param object $groupTest Instance of TestSuite/GroupTest that a file should be added to.
* @param string $file The file name, minus the suffix to add.
* @return void
* @access public
* @static
*/
function addTestFile(&$groupTest, $file) {
$manager =& new TestManager();
if (file_exists($file . $manager->_testExtension)) {
$file .= $manager->_testExtension;
} elseif (file_exists($file . $manager->_groupExtension)) {
$file .= $manager->_groupExtension;
}
$groupTest->addTestFile($file);
}
/**
* Returns a list of test cases found in the current valid test case path
*
* @access public
*/
function &getTestCaseList() {
$manager =& new TestManager();
$return = $manager->_getTestCaseList($manager->_getTestsPath());
return $return;
}
/**
* Builds the list of test cases from a given directory
*
* @param string $directory Directory to get test case list from.
* @access public
*/
function &_getTestCaseList($directory = '.') {
$fileList =& $this->_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.
* @access protected
*/
function &_getTestFileList($directory = '.') {
$return = $this->_getRecursiveFileList($directory, array(&$this, '_isTestCaseFile'));
return $return;
}
/**
* Returns a list of group tests found in the current valid test case path
*
* @access public
* @static
*/
function &getGroupTestList() {
$manager =& new TestManager();
$return = $manager->_getTestGroupList($manager->_getTestsPath('groups'));
return $return;
}
/**
* Returns a list of group test files from a given directory
*
* @param string $directory The directory to get group test files from.
* @access protected
*/
function &_getTestGroupFileList($directory = '.') {
$return = $this->_getRecursiveFileList($directory, array(&$this, '_isTestGroupFile'));
return $return;
}
/**
* Returns a list of group test files from a given directory
*
* @param string $directory The directory to get group tests from.
* @access protected
*/
function &_getTestGroupList($directory = '.') {
$fileList =& $this->_getTestGroupFileList($directory);
$groupTests = array();
foreach ($fileList as $groupTestFile) {
$groupTests[$groupTestFile] = str_replace($this->_groupExtension, '', basename($groupTestFile));
}
sort($groupTests);
return $groupTests;
}
/**
* Returns a list of class names from a group test file
*
* @param string $groupTestFile The groupTest file to scan for TestSuite classnames.
* @access protected
*/
function &_getGroupTestClassNames($groupTestFile) {
$file = implode("\n", file($groupTestFile));
preg_match("~lass\s+?(.*)\s+?extends TestSuite~", $file, $matches);
if (!empty($matches)) {
unset($matches[0]);
return $matches;
}
$matches = array();
return $matches;
}
/**
* 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
* @access protected
*/
function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
$fileList = array();
if (!is_dir($directory)) {
return $fileList;
}
$files = glob($directory . DS . '*');
$files = $files ? $files : array();
foreach ($files as $file) {
if (is_dir($file)) {
$fileList = array_merge($fileList, $this->_getRecursiveFileList($file, $fileTestFunction));
} elseif ($fileTestFunction[0]->$fileTestFunction[1]($file)) {
$fileList[] = $file;
}
}
return $fileList;
}
/**
* Tests if a file has the correct test case extension
*
* @param string $file
* @return void
* @access protected
*/
function _isTestCaseFile($file) {
return $this->_hasExpectedExtension($file, $this->_testExtension);
}
/**
* Tests if a file has the correct group test extension
*
* @param string $file
* @return void
* @access protected
*/
function _isTestGroupFile($file) {
return $this->_hasExpectedExtension($file, $this->_groupExtension);
}
/**
* Check if a file has a specific extension
*
* @param string $file
* @param string $extension
* @return void
* @access protected
*/
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 (cases, group, ..)
*
* @param string $type either 'cases' or 'groups'
* @return string The path tests are located on
* @access protected
*/
function _getTestsPath($type = 'cases') {
if (!empty($this->appTest)) {
if ($type == 'cases') {
$result = APP_TEST_CASES;
} else if ($type == 'groups') {
$result = APP_TEST_GROUPS;
}
} else if (!empty($this->pluginTest)) {
$_pluginBasePath = APP . 'plugins' . DS . $this->pluginTest . DS . 'tests';
$pluginPath = App::pluginPath($this->pluginTest);
if (file_exists($pluginPath . DS . 'tests')) {
$_pluginBasePath = $pluginPath . DS . 'tests';
}
$result = $_pluginBasePath . DS . $type;
} else {
if ($type == 'cases') {
$result = CORE_TEST_CASES;
} else if ($type == 'groups') {
$result = CORE_TEST_GROUPS;
}
}
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.
* @access public
*/
function getExtension($type = 'test') {
$manager =& new TestManager();
if ($type == 'test') {
return $manager->_testExtension;
}
return $manager->_groupExtension;
}
}
/**
* The CliTestManager ensures that the list of available files are printed in the correct cli format
*
* @package cake
* @subpackage cake.cake.tests.lib
*/
class CliTestManager extends TestManager {
/**
* Prints the list of group tests in a cli friendly format
*
* @access public
*/
function &getGroupTestList() {
$manager =& new CliTestManager();
$groupTests =& $manager->_getTestGroupList($manager->_getTestsPath('groups'));
$buffer = "Available Group Test:\n";
foreach ($groupTests as $groupTest) {
$buffer .= " " . $groupTest . "\n";
}
return $buffer . "\n";
}
/**
* Prints the list of test cases in a cli friendly format
*
* @access public
*/
function &getTestCaseList() {
$manager =& new CliTestManager();
$testCases =& $manager->_getTestCaseList($manager->_getTestsPath());
$buffer = "Available Test Cases:\n";
foreach ($testCases as $testCaseFile => $testCase) {
$buffer .= " " . $testCaseFile . "\n";
}
return $buffer . "\n";
}
}
?>