|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace PHPUnit\Framework; |
|
5
|
|
|
|
|
6
|
|
|
use Throwable; |
|
7
|
|
|
|
|
8
|
|
|
class AssertionFailedError extends \Exception |
|
9
|
|
|
{ |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
abstract class TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var list<array{test:string,status:string,message?:string,exception?:Throwable}> */ |
|
|
|
|
|
|
15
|
|
|
private $results = []; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Override in child classes to provide per-test setup |
|
19
|
|
|
*/ |
|
20
|
|
|
protected function setUp(): void |
|
21
|
|
|
{ |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Override in child classes to provide per-test teardown |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function tearDown(): void |
|
28
|
|
|
{ |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Execute all test* methods in the current test case. |
|
33
|
|
|
* |
|
34
|
|
|
* @return array{class:string,count:int,passed:int,failures:list<mixed>,errors:list<mixed>,details:list<mixed>} |
|
|
|
|
|
|
35
|
|
|
*/ |
|
36
|
|
|
public function run(): array |
|
37
|
|
|
{ |
|
38
|
|
|
$methods = array_filter(get_class_methods($this), static function (string $method): bool { |
|
39
|
|
|
return 0 === strpos($method, 'test'); |
|
40
|
|
|
}); |
|
41
|
|
|
sort($methods); |
|
42
|
|
|
|
|
43
|
|
|
$failures = []; |
|
44
|
|
|
$errors = []; |
|
45
|
|
|
$details = []; |
|
46
|
|
|
|
|
47
|
|
|
foreach ($methods as $method) { |
|
48
|
|
|
$status = 'passed'; |
|
49
|
|
|
$message = ''; |
|
50
|
|
|
$exception = null; |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
$this->setUp(); |
|
54
|
|
|
$this->$method(); |
|
55
|
|
|
} catch (AssertionFailedError $failure) { |
|
56
|
|
|
$status = 'failure'; |
|
57
|
|
|
$message = $failure->getMessage(); |
|
58
|
|
|
$exception = $failure; |
|
59
|
|
|
} catch (Throwable $throwable) { |
|
60
|
|
|
$status = 'error'; |
|
61
|
|
|
$message = $throwable->getMessage(); |
|
62
|
|
|
$exception = $throwable; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
try { |
|
66
|
|
|
$this->tearDown(); |
|
67
|
|
|
} catch (Throwable $teardown) { |
|
68
|
|
|
$status = 'error'; |
|
69
|
|
|
$message = 'tearDown failure: ' . $teardown->getMessage(); |
|
70
|
|
|
$exception = $teardown; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ('failure' === $status) { |
|
74
|
|
|
$failures[] = ['test' => $method, 'message' => $message, 'exception' => $exception]; |
|
75
|
|
|
} elseif ('error' === $status) { |
|
76
|
|
|
$errors[] = ['test' => $method, 'message' => $message, 'exception' => $exception]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$details[] = ['test' => $method, 'status' => $status, 'message' => $message]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$count = count($methods); |
|
83
|
|
|
$passed = $count - count($failures) - count($errors); |
|
84
|
|
|
|
|
85
|
|
|
return [ |
|
86
|
|
|
'class' => static::class, |
|
87
|
|
|
'count' => $count, |
|
88
|
|
|
'passed' => $passed, |
|
89
|
|
|
'failures' => $failures, |
|
90
|
|
|
'errors' => $errors, |
|
91
|
|
|
'details' => $details, |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected static function failureDescription($expected, $actual): string |
|
96
|
|
|
{ |
|
97
|
|
|
return 'Failed asserting that ' . var_export($actual, true) . ' matches expected ' . var_export($expected, true); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public static function assertTrue($condition, string $message = ''): void |
|
101
|
|
|
{ |
|
102
|
|
|
if (true !== $condition) { |
|
103
|
|
|
throw new AssertionFailedError($message ?: 'Failed asserting that condition is true.'); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function assertFalse($condition, string $message = ''): void |
|
108
|
|
|
{ |
|
109
|
|
|
if (false !== $condition) { |
|
110
|
|
|
throw new AssertionFailedError($message ?: 'Failed asserting that condition is false.'); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function assertSame($expected, $actual, string $message = ''): void |
|
115
|
|
|
{ |
|
116
|
|
|
if ($expected !== $actual) { |
|
117
|
|
|
throw new AssertionFailedError($message ?: self::failureDescription($expected, $actual)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public static function assertEquals($expected, $actual, string $message = ''): void |
|
122
|
|
|
{ |
|
123
|
|
|
if ($expected != $actual) { |
|
124
|
|
|
throw new AssertionFailedError($message ?: self::failureDescription($expected, $actual)); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public static function assertInstanceOf(string $expected, $actual, string $message = ''): void |
|
129
|
|
|
{ |
|
130
|
|
|
if (!($actual instanceof $expected)) { |
|
131
|
|
|
throw new AssertionFailedError($message ?: 'Failed asserting that object is instance of ' . $expected . '.'); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public static function assertNull($actual, string $message = ''): void |
|
136
|
|
|
{ |
|
137
|
|
|
if (null !== $actual) { |
|
138
|
|
|
throw new AssertionFailedError($message ?: 'Failed asserting that value is null.'); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public static function assertNotNull($actual, string $message = ''): void |
|
143
|
|
|
{ |
|
144
|
|
|
if (null === $actual) { |
|
145
|
|
|
throw new AssertionFailedError($message ?: 'Failed asserting that value is not null.'); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public static function assertArrayHasKey($key, array $array, string $message = ''): void |
|
150
|
|
|
{ |
|
151
|
|
|
if (!array_key_exists($key, $array)) { |
|
152
|
|
|
throw new AssertionFailedError($message ?: sprintf('Failed asserting that the array has the key %s.', var_export($key, true))); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public static function fail(string $message = ''): void |
|
157
|
|
|
{ |
|
158
|
|
|
throw new AssertionFailedError($message ?: 'Failed assertion.'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths