TestRunner.php
10.5 KB
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
<?php
/**
* @package framework
* @subpackage testing
*/
/**
* Controller that executes PHPUnit tests.
*
* Alternatively, you can also use the "phpunit" binary directly by
* pointing it to a file or folder containing unit tests.
* See phpunit.dist.xml in the webroot for configuration details.
*
* <h2>URL Options</h2>
* - SkipTests: A comma-separated list of test classes to skip (useful when running dev/tests/all)
*
* See {@link browse()} output for generic usage instructions.
*
* @package framework
* @subpackage testing
*/
class TestRunner extends Controller {
/** @ignore */
private static $default_reporter;
private static $url_handlers = array(
'' => 'browse',
'coverage/module/$ModuleName' => 'coverageModule',
'coverage/$TestCase!' => 'coverageOnly',
'coverage' => 'coverageAll',
'cleanupdb' => 'cleanupdb',
'module/$ModuleName' => 'module',
'all' => 'all',
'build' => 'build',
'$TestCase' => 'only'
);
private static $allowed_actions = array(
'index',
'browse',
'coverage',
'coverageAll',
'coverageModule',
'coverageOnly',
'cleanupdb',
'module',
'all',
'build',
'only'
);
/**
* @var Array Blacklist certain directories for the coverage report.
* Filepaths are relative to the webroot, without leading slash.
*
* @see http://www.phpunit.de/manual/current/en/appendixes.configuration.html
* #appendixes.configuration.blacklist-whitelist
*/
static $coverage_filter_dirs = array(
'*/thirdparty',
'*/tests',
'*/lang',
);
/**
* Override the default reporter with a custom configured subclass.
*
* @param string $reporter
*/
public static function set_reporter($reporter) {
if (is_string($reporter)) $reporter = new $reporter;
self::$default_reporter = $reporter;
}
/**
* Pushes a class and template manifest instance that include tests onto the
* top of the loader stacks.
*/
public static function use_test_manifest() {
$flush = true;
if(isset($_GET['flush']) && $_GET['flush'] === '0') {
$flush = false;
}
$classManifest = new SS_ClassManifest(
BASE_PATH, true, $flush
);
SS_ClassLoader::instance()->pushManifest($classManifest, false);
SapphireTest::set_test_class_manifest($classManifest);
SS_TemplateLoader::instance()->pushManifest(new SS_TemplateManifest(
BASE_PATH, project(), true, $flush
));
Config::inst()->pushConfigStaticManifest(new SS_ConfigStaticManifest(
BASE_PATH, true, $flush
));
// Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class
// (e.g. Member will now have various subclasses of DataObjects that implement TestOnly)
DataObject::clear_classname_spec_cache();
}
public function init() {
parent::init();
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
if(!$canAccess) return Security::permissionFailure($this);
if (!self::$default_reporter) self::set_reporter(Director::is_cli() ? 'CliDebugView' : 'DebugView');
if(!PhpUnitWrapper::has_php_unit()) {
die("Please install PHPUnit using Composer");
}
}
public function Link() {
return Controller::join_links(Director::absoluteBaseURL(), 'dev/tests/');
}
/**
* Run test classes that should be run with every commit.
* Currently excludes PhpSyntaxTest
*/
public function all($request, $coverage = false) {
self::use_test_manifest();
$tests = ClassInfo::subclassesFor('SapphireTest');
array_shift($tests);
unset($tests['FunctionalTest']);
// Remove tests that don't need to be executed every time
unset($tests['PhpSyntaxTest']);
foreach($tests as $class => $v) {
$reflection = new ReflectionClass($class);
if(!$reflection->isInstantiable()) unset($tests[$class]);
}
$this->runTests($tests, $coverage);
}
/**
* Run test classes that should be run before build - i.e., everything possible.
*/
public function build() {
self::use_test_manifest();
$tests = ClassInfo::subclassesFor('SapphireTest');
array_shift($tests);
unset($tests['FunctionalTest']);
foreach($tests as $class => $v) {
$reflection = new ReflectionClass($class);
if(!$reflection->isInstantiable()) unset($tests[$class]);
}
$this->runTests($tests);
}
/**
* Browse all enabled test cases in the environment
*/
public function browse() {
self::use_test_manifest();
self::$default_reporter->writeHeader();
self::$default_reporter->writeInfo('Available Tests', false);
if(Director::is_cli()) {
$tests = ClassInfo::subclassesFor('SapphireTest');
$relativeLink = Director::makeRelative($this->Link());
echo "sake {$relativeLink}all: Run all " . count($tests) . " tests\n";
echo "sake {$relativeLink}coverage: Runs all tests and make test coverage report\n";
echo "sake {$relativeLink}module/<modulename>: Runs all tests in a module folder\n";
foreach ($tests as $test) {
echo "sake {$relativeLink}$test: Run $test\n";
}
} else {
echo '<div class="trace">';
$tests = ClassInfo::subclassesFor('SapphireTest');
asort($tests);
echo "<h3><a href=\"" . $this->Link() . "all\">Run all " . count($tests) . " tests</a></h3>";
echo "<h3><a href=\"" . $this->Link() . "coverage\">Runs all tests and make test coverage report</a></h3>";
echo "<hr />";
foreach ($tests as $test) {
echo "<h3><a href=\"" . $this->Link() . "$test\">Run $test</a></h3>";
}
echo '</div>';
}
self::$default_reporter->writeFooter();
}
/**
* Run a coverage test across all modules
*/
public function coverageAll($request) {
self::use_test_manifest();
$this->all($request, true);
}
/**
* Run only a single coverage test class or a comma-separated list of tests
*/
public function coverageOnly($request) {
$this->only($request, true);
}
/**
* Run coverage tests for one or more "modules".
* A module is generally a toplevel folder, e.g. "mysite" or "framework".
*/
public function coverageModule($request) {
$this->module($request, true);
}
public function cleanupdb() {
SapphireTest::delete_all_temp_dbs();
}
/**
* Run only a single test class or a comma-separated list of tests
*/
public function only($request, $coverage = false) {
self::use_test_manifest();
if($request->param('TestCase') == 'all') {
$this->all();
} else {
$classNames = explode(',', $request->param('TestCase'));
foreach($classNames as $className) {
if(!class_exists($className) || !is_subclass_of($className, 'SapphireTest')) {
user_error("TestRunner::only(): Invalid TestCase '$className', cannot find matching class",
E_USER_ERROR);
}
}
$this->runTests($classNames, $coverage);
}
}
/**
* Run tests for one or more "modules".
* A module is generally a toplevel folder, e.g. "mysite" or "framework".
*/
public function module($request, $coverage = false) {
self::use_test_manifest();
$classNames = array();
$moduleNames = explode(',', $request->param('ModuleName'));
$ignored = array('functionaltest', 'phpsyntaxtest');
foreach($moduleNames as $moduleName) {
$classesForModule = ClassInfo::classes_for_folder($moduleName);
if($classesForModule) {
foreach($classesForModule as $className) {
if(class_exists($className) && is_subclass_of($className, 'SapphireTest')) {
if(!in_array($className, $ignored))
$classNames[] = $className;
}
}
}
}
$this->runTests($classNames, $coverage);
}
/**
* @param array $classList
* @param boolean $coverage
*/
public function runTests($classList, $coverage = false) {
$startTime = microtime(true);
// disable xdebug, as it messes up test execution
if(function_exists('xdebug_disable')) xdebug_disable();
ini_set('max_execution_time', 0);
$this->setUp();
// Optionally skip certain tests
$skipTests = array();
if($this->request->getVar('SkipTests')) {
$skipTests = explode(',', $this->request->getVar('SkipTests'));
}
$abstractClasses = array();
foreach($classList as $className) {
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
class_exists($className);
$reflection = new ReflectionClass($className);
if ($reflection->isAbstract()) {
array_push($abstractClasses, $className);
}
}
$classList = array_diff($classList, $skipTests, $abstractClasses);
// run tests before outputting anything to the client
$suite = new PHPUnit_Framework_TestSuite();
natcasesort($classList);
foreach($classList as $className) {
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
class_exists($className);
$suite->addTest(new SapphireTestSuite($className));
}
// Remove the error handler so that PHPUnit can add its own
restore_error_handler();
self::$default_reporter->writeHeader("SilverStripe Test Runner");
if (count($classList) > 1) {
self::$default_reporter->writeInfo("All Tests", "Running test cases: ",implode(", ", $classList));
} elseif (count($classList) == 1) {
self::$default_reporter->writeInfo(reset($classList), '');
} else {
// border case: no tests are available.
self::$default_reporter->writeInfo('', '');
}
// perform unit tests (use PhpUnitWrapper or derived versions)
$phpunitwrapper = PhpUnitWrapper::inst();
$phpunitwrapper->setSuite($suite);
$phpunitwrapper->setCoverageStatus($coverage);
// Make sure TearDown is called (even in the case of a fatal error)
$self = $this;
register_shutdown_function(function() use ($self) {
$self->tearDown();
});
$phpunitwrapper->runTests();
// get results of the PhpUnitWrapper class
$reporter = $phpunitwrapper->getReporter();
$results = $phpunitwrapper->getFrameworkTestResults();
if(!Director::is_cli()) echo '<div class="trace">';
$reporter->writeResults();
$endTime = microtime(true);
if(Director::is_cli()) echo "\n\nTotal time: " . round($endTime-$startTime,3) . " seconds\n";
else echo "<p class=\"total-time\">Total time: " . round($endTime-$startTime,3) . " seconds</p>\n";
if(!Director::is_cli()) echo '</div>';
// Put the error handlers back
Debug::loadErrorHandlers();
if(!Director::is_cli()) self::$default_reporter->writeFooter();
$this->tearDown();
// Todo: we should figure out how to pass this data back through Director more cleanly
if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2);
}
public function setUp() {
// The first DB test will sort out the DB, we don't have to
SSViewer::flush_template_cache();
}
public function tearDown() {
SapphireTest::kill_temp_db();
DB::set_alternative_database_name(null);
}
}