-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRunner.php
124 lines (108 loc) · 3.58 KB
/
TestRunner.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
<?php
namespace Framework\Test;
use Framework\Facades\File;
use Framework\Facades\Path;
/** The TestRunner runs all test cases in the given test file path */
class TestRunner
{
private string $testFilePath = '';
public function __construct(string $testFilePath = null) {
if ($testFilePath !== null) {
$this->testFilePath = $testFilePath;
} else {
$this->testFilePath = Path::join(__DIR__, '..', '..', 'resources', 'Tests');
}
}
/** Run all tests in the given test file path */
public function run(): bool
{
$GLOBALS['isTestRun'] = true;
return $this->runUnitTests(Path::join(__DIR__, '..', 'resources', 'Tests'))
&& $this->runUnitTests($this->testFilePath);
}
/** Run all unit tests */
private function runUnitTests(string $testFilePath): bool
{
$testFiles = $this->getUnitTestFiles($testFilePath);
if (count($testFiles) === 0) {
printLn('No unit test files found');
return true;
}
printLn('Running unit tests...');
$success = true;
foreach ($testFiles as $testFile) {
printLn('- ' . $testFile);
if (!$this->runUnitTestFile($testFilePath, $testFile)) {
$success = false;
}
}
if ($success) {
printLn('All tests were successful!');
} else {
printLn('One or more test case failed!');
}
printLn('');
return $success;
}
/** Run all test methods in the given unit test file */
private function runUnitTestFile(string $testFilePath, string $filepath): bool
{
$filename = basename($filepath);
require Path::join($testFilePath, 'Unit', $filepath);
$testCaseName = str_replace('.php', '', $filename);
$testCase = new $testCaseName();
$methods = get_class_methods($testCase);
$success = true;
/** @var string $method */
foreach ($methods as $method) {
if (!$this->isTestMethod($method)) {
continue;
}
try {
$testCase->$method();
} catch (AssertionFailedException $ex) {
printLn($ex->getTestCase() . ' failed:');
printLn($ex->__toString());
$success = false;
} catch (\Throwable $th) {
$success = false;
throw $th;
}
}
return $success;
}
/** Get array with all unit test files in the test file directory */
private function getUnitTestFiles(string $testFilePath): array
{
$allFiles = File::findFilesInDir(Path::join($testFilePath, 'Unit'), recursive: true, onlyFiles: true);
$testFiles = [];
/** @var string $file */
foreach ($allFiles as $file) {
if (!$this->isTestFile(basename($file))) {
continue;
}
array_push($testFiles, $file);
}
return $testFiles;
}
private function isTestMethod(string $methodName): bool
{
// All test method must start with 'test'
if (!str_starts_with($methodName, 'test')) {
return false;
}
return true;
}
private function isTestFile(string $filename): bool
{
// All test files must start with 'Test'
if (!str_starts_with($filename, 'Test')) {
return false;
}
// All test files must be PHP files
if (!str_ends_with($filename, '.php')) {
return false;
}
return true;
}
}