generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTestPassedSubscriber.php
101 lines (80 loc) · 2.7 KB
/
TestPassedSubscriber.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
<?php
declare(strict_types=1);
/**
* Copyright (c) 2021-2023 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/phpunit-slow-test-detector
*/
namespace Ergebnis\PHPUnit\SlowTestDetector\Subscriber;
use Ergebnis\PHPUnit\SlowTestDetector\Collector;
use Ergebnis\PHPUnit\SlowTestDetector\Duration;
use Ergebnis\PHPUnit\SlowTestDetector\SlowTest;
use Ergebnis\PHPUnit\SlowTestDetector\TestIdentifier;
use Ergebnis\PHPUnit\SlowTestDetector\Time;
use Ergebnis\PHPUnit\SlowTestDetector\TimeKeeper;
use PHPUnit\Event;
use PHPUnit\Metadata;
/**
* @internal
*/
final class TestPassedSubscriber implements Event\Test\PassedSubscriber
{
public function __construct(
private readonly Duration $maximumDuration,
private readonly TimeKeeper $timeKeeper,
private readonly Collector\Collector $collector,
) {
}
public function notify(Event\Test\Passed $event): void
{
$testIdentifier = TestIdentifier::fromString($event->test()->id());
$time = $event->telemetryInfo()->time();
$duration = $this->timeKeeper->stop(
$testIdentifier,
Time::fromSecondsAndNanoseconds(
$time->seconds(),
$time->nanoseconds(),
),
);
$maximumDuration = $this->resolveMaximumDuration($event->test());
if (!$duration->isGreaterThan($maximumDuration)) {
return;
}
$slowTest = SlowTest::create(
$testIdentifier,
$duration,
$maximumDuration,
);
$this->collector->collect($slowTest);
}
private function resolveMaximumDuration(Event\Code\Test $test): Duration
{
$annotations = [
'maximumDuration',
'slowThreshold',
];
/** @var Event\Code\TestMethod $test */
$docBlock = Metadata\Annotation\Parser\Registry::getInstance()->forMethod(
$test->className(),
$test->methodName(),
);
$symbolAnnotations = $docBlock->symbolAnnotations();
foreach ($annotations as $annotation) {
if (!\array_key_exists($annotation, $symbolAnnotations)) {
continue;
}
if (!\is_array($symbolAnnotations[$annotation])) {
continue;
}
$maximumDuration = \reset($symbolAnnotations[$annotation]);
if (1 !== \preg_match('/^\d+$/', $maximumDuration)) {
continue;
}
return Duration::fromMilliseconds((int) $maximumDuration);
}
return $this->maximumDuration;
}
}