-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Test.php
272 lines (236 loc) · 8.39 KB
/
Test.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
<?php
declare(strict_types=1);
namespace Codeception\Test;
use Codeception\Event\FailEvent;
use Codeception\Event\TestEvent;
use Codeception\Events;
use Codeception\Exception\UselessTestException;
use Codeception\PHPUnit\Wrapper\Test as TestWrapper;
use Codeception\ResultAggregator;
use Codeception\TestInterface;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\IncompleteTestError;
use PHPUnit\Framework\SkippedTest;
use PHPUnit\Framework\SkippedTestError;
use PHPUnit\Runner\Version as PHPUnitVersion;
use RuntimeException;
use SebastianBergmann\Timer\Timer;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Throwable;
use function array_reverse;
use function method_exists;
// phpcs:disable
if (PHPUnitVersion::series() < 10) {
require_once __DIR__ . '/../../PHPUnit/Wrapper/PhpUnit9/Test.php';
} else {
require_once __DIR__ . '/../../PHPUnit/Wrapper/PhpUnit10/Test.php';
}
// phpcs:enable
/**
* The most simple testcase (with only one test in it) which can be executed by PHPUnit/Codeception.
* It can be extended with included traits. Turning on/off a trait should not break class functionality.
*
* Class has exactly one method to be executed for testing, wrapped with before/after callbacks delivered from included traits.
* A trait providing before/after callback should contain corresponding protected methods: `{traitName}Start` and `{traitName}End`,
* then this trait should be enabled in `hooks` property.
*
* Inherited class must implement `test` method.
*/
abstract class Test extends TestWrapper implements TestInterface, Interfaces\Descriptive
{
use Feature\AssertionCounter;
use Feature\CodeCoverage;
use Feature\MetadataCollector;
use Feature\IgnoreIfMetadataBlocked;
private ?ResultAggregator $resultAggregator = null;
private bool $ignored = false;
private int $assertionCount = 0;
private ?EventDispatcher $eventDispatcher = null;
/**
* Enabled traits with methods to be called before and after the test.
*/
protected array $hooks = [
'ignoreIfMetadataBlocked',
'codeCoverage',
'assertionCounter',
'errorLogger'
];
/**
* @var string
*/
public const STATUS_FAIL = 'fail';
/**
* @var string
*/
public const STATUS_ERROR = 'error';
/**
* @var string
*/
public const STATUS_OK = 'ok';
/**
* @var string
*/
public const STATUS_PENDING = 'pending';
/**
* @var string
*/
public const STATUS_USELESS = 'useless';
/**
* @var string
*/
public const STATUS_INCOMPLETE = 'incomplete';
/**
* @var string
*/
public const STATUS_SKIPPED = 'skipped';
protected bool $reportUselessTests = false;
private bool $collectCodeCoverage = false;
/**
* Everything inside this method is treated as a test.
*
* @return mixed
*/
abstract public function test();
/**
* Test representation
*/
abstract public function toString(): string;
public function collectCodeCoverage(bool $enabled): void
{
$this->collectCodeCoverage = $enabled;
}
public function reportUselessTests(bool $enabled): void
{
$this->reportUselessTests = $enabled;
}
public function setEventDispatcher(EventDispatcher $eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* Runs a test and collects its result in a TestResult instance.
* Executes before/after hooks coming from traits.
*/
final public function realRun(ResultAggregator $result): void
{
$this->resultAggregator = $result;
$status = self::STATUS_PENDING;
$time = 0;
$e = null;
$timer = new Timer();
$result->addTest($this);
try {
$this->fire(Events::TEST_BEFORE, new TestEvent($this));
foreach ($this->hooks as $hook) {
if ($hook === 'codeCoverage' && !$this->collectCodeCoverage) {
continue;
}
if (method_exists($this, $hook . 'Start')) {
$this->{$hook . 'Start'}();
}
}
$failedToStart = false;
} catch (\Exception $e) {
$failedToStart = true;
$result->addError(new FailEvent($this, $e, $time));
$this->fire(Events::TEST_ERROR, new FailEvent($this, $e, $time));
}
if (!$this->ignored && !$failedToStart) {
Assert::resetCount();
$timer->start();
try {
$this->test();
$status = self::STATUS_OK;
$eventType = Events::TEST_SUCCESS;
} catch (UselessTestException $e) {
$result->addUseless(new FailEvent($this, $e, $time));
$status = self::STATUS_USELESS;
$eventType = Events::TEST_USELESS;
} catch (IncompleteTestError $e) {
$result->addIncomplete(new FailEvent($this, $e, $time));
$status = self::STATUS_INCOMPLETE;
$eventType = Events::TEST_INCOMPLETE;
} catch (SkippedTest | SkippedTestError $e) {
$result->addSkipped(new FailEvent($this, $e, $time));
$status = self::STATUS_SKIPPED;
$eventType = Events::TEST_SKIPPED;
} catch (AssertionFailedError $e) {
$result->addFailure(new FailEvent($this, $e, $time));
$status = self::STATUS_FAIL;
$eventType = Events::TEST_FAIL;
} catch (Exception $e) {
$result->addError(new FailEvent($this, $e, $time));
$status = self::STATUS_ERROR;
$eventType = Events::TEST_ERROR;
} catch (Throwable $e) {
$result->addError(new FailEvent($this, $e, $time));
$status = self::STATUS_ERROR;
$eventType = Events::TEST_ERROR;
}
$time = $timer->stop()->asSeconds();
$this->assertionCount = Assert::getCount();
$result->addToAssertionCount($this->assertionCount);
if ($this->reportUselessTests && $this->assertionCount === 0 && $eventType === Events::TEST_SUCCESS) {
$eventType = Events::TEST_USELESS;
$e = new UselessTestException('This test did not perform any assertions');
$result->addUseless(new FailEvent($this, $e, $time));
}
if ($eventType === Events::TEST_SUCCESS) {
$this->fire($eventType, new TestEvent($this, $time));
} else {
$this->fire($eventType, new FailEvent($this, $e, $time));
}
}
foreach (array_reverse($this->hooks) as $hook) {
if ($hook === 'codeCoverage' && !$this->collectCodeCoverage) {
continue;
}
if (method_exists($this, $hook . 'End')) {
$this->{$hook . 'End'}($status, $time, $e);
}
}
$this->fire(Events::TEST_AFTER, new TestEvent($this, $time));
$this->eventDispatcher->dispatch(new TestEvent($this, $time), Events::TEST_END);
$result->addSuccessful($this, $time);
}
public function getResultAggregator(): ResultAggregator
{
if ($this->resultAggregator === null) {
throw new \LogicException('ResultAggregator is not set');
}
return $this->resultAggregator;
}
/**
* This class represents exactly one test
*/
public function count(): int
{
return 1;
}
/**
* Should a test be skipped (can be set from hooks)
*/
protected function ignore(bool $ignored): void
{
$this->ignored = $ignored;
}
public function numberOfAssertionsPerformed(): int
{
return $this->assertionCount;
}
protected function fire(string $eventType, TestEvent $event): void
{
if ($this->eventDispatcher === null) {
throw new RuntimeException('EventDispatcher must be injected before running test');
}
$test = $event->getTest();
if ($test instanceof TestInterface) {
foreach ($test->getMetadata()->getGroups() as $group) {
$this->eventDispatcher->dispatch($event, $eventType . '.' . $group);
}
}
$this->eventDispatcher->dispatch($event, $eventType);
}
}