-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
TestManager.php
302 lines (273 loc) · 8.27 KB
/
TestManager.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
<?php
/**
* TestManager for CakePHP Test suite.
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package cake
* @subpackage cake.cake.tests.lib
* @since CakePHP(tm) v 1.2.0.4433
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
define('CORE_TEST_CASES', LIBS . 'tests' . DS . 'cases');
define('CORE_TEST_GROUPS', LIBS . 'tests' . DS . 'groups');
define('APP_TEST_CASES', TESTS . 'cases');
define('APP_TEST_GROUPS', TESTS . 'groups');
PHP_CodeCoverage_Filter::getInstance()->addFileToBlacklist(__FILE__, 'DEFAULT');
App::uses('CakeTestSuite', 'TestSuite');
App::uses('CakeTestCase', 'TestSuite');
App::uses('CakeFixtureManager', 'TestSuite/Fixture');
App::uses('CakeTestModel', 'TestSuite/Fixture');
/**
* 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
*/
protected static $_testExtension = '.test.php';
/**
* Is this test an AppTest?
*
* @var boolean
*/
public $appTest = false;
/**
* Is this test a plugin test?
*
* @var mixed boolean false or string name of the plugin being used.
*/
public $pluginTest = false;
/**
* String to filter test case method names by.
*
* @var string
*/
public $filter = false;
/**
* TestSuite container for single or grouped test files
*
* @var PHPUnit_Framework_TestSuite
*/
protected $_testSuite = null;
/**
* Object instance responsible for managing the test fixtures
*
* @var CakeFixtureManager
*/
protected $_fixtureManager = null;
/**
* Params to configure test runner
*
* @var CakeFixtureManager
*/
public $params = array();
/**
* Constructor for the TestManager class
*
* @return void
*/
public function __construct($params = array()) {
$this->params = $params;
if (isset($params['app'])) {
$this->appTest = true;
}
if (isset($params['plugin'])) {
$this->pluginTest = htmlentities($params['plugin']);
}
if (
isset($params['filter']) &&
$params['filter'] !== false &&
preg_match('/^[a-zA-Z0-9_]/', $params['filter'])
) {
$this->filter = '/' . $params['filter'] . '/';
}
}
/**
* Runs a specific test case file
*
* @param string $testCaseFile Filename of the test to be run.
* @param PHPUnit_Framework_TestListener $reporter Reporter instance to attach to the test case.
* @throws InvalidArgumentException if the supplied $testCaseFile does not exists
* @return mixed Result of test case being run.
*/
public function runTestCase($testCaseFile, PHPUnit_Framework_TestListener $reporter, $codeCoverage = false) {
$this->loadCase($testCaseFile);
return $this->run($reporter, $codeCoverage);
}
/**
* Runs the main testSuite and attaches to it a reporter
*
* @param PHPUnit_Framework_TestListener $reporter Reporter instance to use with the group test being run.
* @return PHPUnit_Framework_TestResult Result object of the test run.
*/
protected function run($reporter, $codeCoverage = false) {
restore_error_handler();
restore_error_handler();
$result = new PHPUnit_Framework_TestResult;
$result->collectCodeCoverageInformation($codeCoverage);
$result->addListener($reporter);
$reporter->paintHeader();
$testSuite = $this->getTestSuite();
$testSuite->setFixtureManager($this->getFixtureManager());
$testSuite->run($result, $this->filter);
$reporter->paintResult($result);
return $result;
}
/**
* Loads a test case in a test suite, if the test suite is null it will create it
*
* @param string Test file path
* @param PHPUnit_Framework_TestSuite $suite the test suite to load the case in
* @throws InvalidArgumentException if test case file is not found
* @return PHPUnit_Framework_TestSuite the suite with the test case loaded
*/
public function loadCase($testCaseFile, PHPUnit_Framework_TestSuite $suite = null) {
$testCaseFileWithPath = $this->_getTestsPath($this->params) . DS . $testCaseFile;
if (!file_exists($testCaseFileWithPath) || strpos($testCaseFileWithPath, '..')) {
throw new InvalidArgumentException(__('Unable to load test file %s', htmlentities($testCaseFile)));
}
if (!$suite) {
$suite = $this->getTestSuite(__('Individual test case: %s', $testCaseFile));
}
$suite->addTestFile($testCaseFileWithPath);
return $suite;
}
/**
* Returns a list of test cases found in the current valid test case path
*
* @access public
* @static
*/
public static function getTestCaseList($params) {
$directory = self::_getTestsPath($params);
$fileList = self::_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.
* @static
*/
protected static function &_getTestFileList($directory = '.') {
$return = self::_getRecursiveFileList($directory, array('self', '_isTestCaseFile'));
return $return;
}
/**
* 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
* @static
*/
protected static function &_getRecursiveFileList($directory = '.', $fileTestFunction) {
$fileList = array();
if (!is_dir($directory)) {
return $fileList;
}
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
foreach ($files as $file) {
if (!$file->isFile()) {
continue;
}
$file = $file->getRealPath();
if (call_user_func_array($fileTestFunction, array($file))) {
$fileList[] = $file;
}
}
return $fileList;
}
/**
* Tests if a file has the correct test case extension
*
* @param string $file
* @return boolean Whether $file is a test case.
* @static
*/
protected static function _isTestCaseFile($file) {
return self::_hasExpectedExtension($file, self::$_testExtension);
}
/**
* Check if a file has a specific extension
*
* @param string $file
* @param string $extension
* @return void
* @static
*/
protected static 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 (core, app, plugin)
*
* @param array $params Array of parameters for getting test paths.
* Can contain app, type, and plugin params.
* @return string The path tests are located on
* @static
*/
protected static function _getTestsPath($params) {
$result = null;
if (!empty($params['app'])) {
$result = APP_TEST_CASES;
} else if (!empty($params['plugin'])) {
$pluginPath = App::pluginPath($params['plugin']);
$result = $pluginPath . 'tests' . DS . 'cases';
} else {
$result = CORE_TEST_CASES;
}
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.
*/
public static function getExtension($type = 'test') {
return self::$_testExtension;
}
/**
* Get the container testSuite instance for this runner or creates a new one
*
* @param string $name The name for the container test suite
* @return PHPUnit_Framework_TestSuite container test suite
*/
public function getTestSuite($name = '') {
if (!empty($this->_testSuite)) {
return $this->_testSuite;
}
return $this->_testSuite = new CakeTestSuite($name);
}
/**
* Get an instance of a Fixture manager to be used by the test cases
*
* @return CakeFixtureManager fixture manager
*/
public function getFixtureManager() {
if (!empty($this->_fixtureManager)) {
return $this->_fixtureManager;
}
return $this->_fixtureManager = new CakeFixtureManager;
}
}