-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathTestCaseRunner.php
More file actions
91 lines (73 loc) · 1.8 KB
/
TestCaseRunner.php
File metadata and controls
91 lines (73 loc) · 1.8 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
<?php
/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Tester;
/**
* Runner of TestCase.
*/
class TestCaseRunner
{
/** @var array */
private $classes = [];
public function findTests(string $fileMask): self
{
$files = [];
foreach (glob($fileMask) as $file) {
require_once $file;
$files[realpath($file)] = true;
}
foreach (get_declared_classes() as $class) {
$rc = new \ReflectionClass($class);
if ($rc->isSubclassOf(TestCase::class) && isset($files[$rc->getFileName()])) {
$this->classes[] = $class;
}
}
return $this;
}
public function run(TestCase $test = null): void
{
if ($this->runFromCli($test)) {
return;
} elseif ($test) {
$test->run();
} else {
foreach ($this->classes as $class) {
$test = $this->createInstance($class);
$test->run();
}
}
}
private function runFromCli(TestCase $test = null): bool
{
$args = preg_filter('#--method=([\w:-]+)$#Ai', '$1', $_SERVER['argv'] ?? []);
$arg = reset($args);
if ($arg) {
[$class, $method] = explode('::', $arg);
$test = $test ?: $this->createInstance($class);
$test->runTest($method);
return true;
} elseif (getenv(Environment::RUNNER)) {
Environment::$checkAssertions = false;
$methods = [];
$classes = $test ? [get_class($test)] : $this->classes;
foreach ($classes as $class) {
foreach ($class::findMethods() as $method) {
$methods[] = $class . '::' . $method;
}
}
header('Content-Type: text/plain');
echo '[' . implode(',', $methods) . ']';
exit(Runner\Job::CODE_TESTCASE);
} else {
return false;
}
}
protected function createInstance(string $class): TestCase
{
// TOO: can be altered via setFactory(callable)
return new $class;
}
}