| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
/*
|
|
|
3 |
* This file is part of PHPUnit.
|
|
|
4 |
*
|
|
|
5 |
* (c) Sebastian Bergmann <sebastian@phpunit.de>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
namespace PHPUnit\Runner;
|
|
|
11 |
|
|
|
12 |
use function preg_match;
|
|
|
13 |
use function round;
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* @internal This class is not covered by the backward compatibility promise for PHPUnit
|
|
|
17 |
*/
|
|
|
18 |
final class ResultCacheExtension implements AfterIncompleteTestHook, AfterLastTestHook, AfterRiskyTestHook, AfterSkippedTestHook, AfterSuccessfulTestHook, AfterTestErrorHook, AfterTestFailureHook, AfterTestWarningHook
|
|
|
19 |
{
|
|
|
20 |
/**
|
|
|
21 |
* @var TestResultCache
|
|
|
22 |
*/
|
|
|
23 |
private $cache;
|
|
|
24 |
|
|
|
25 |
public function __construct(TestResultCache $cache)
|
|
|
26 |
{
|
|
|
27 |
$this->cache = $cache;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function flush(): void
|
|
|
31 |
{
|
|
|
32 |
$this->cache->persist();
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
public function executeAfterSuccessfulTest(string $test, float $time): void
|
|
|
36 |
{
|
|
|
37 |
$testName = $this->getTestName($test);
|
|
|
38 |
|
|
|
39 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
public function executeAfterIncompleteTest(string $test, string $message, float $time): void
|
|
|
43 |
{
|
|
|
44 |
$testName = $this->getTestName($test);
|
|
|
45 |
|
|
|
46 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
47 |
$this->cache->setState($testName, BaseTestRunner::STATUS_INCOMPLETE);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
public function executeAfterRiskyTest(string $test, string $message, float $time): void
|
|
|
51 |
{
|
|
|
52 |
$testName = $this->getTestName($test);
|
|
|
53 |
|
|
|
54 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
55 |
$this->cache->setState($testName, BaseTestRunner::STATUS_RISKY);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
public function executeAfterSkippedTest(string $test, string $message, float $time): void
|
|
|
59 |
{
|
|
|
60 |
$testName = $this->getTestName($test);
|
|
|
61 |
|
|
|
62 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
63 |
$this->cache->setState($testName, BaseTestRunner::STATUS_SKIPPED);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
public function executeAfterTestError(string $test, string $message, float $time): void
|
|
|
67 |
{
|
|
|
68 |
$testName = $this->getTestName($test);
|
|
|
69 |
|
|
|
70 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
71 |
$this->cache->setState($testName, BaseTestRunner::STATUS_ERROR);
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
public function executeAfterTestFailure(string $test, string $message, float $time): void
|
|
|
75 |
{
|
|
|
76 |
$testName = $this->getTestName($test);
|
|
|
77 |
|
|
|
78 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
79 |
$this->cache->setState($testName, BaseTestRunner::STATUS_FAILURE);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function executeAfterTestWarning(string $test, string $message, float $time): void
|
|
|
83 |
{
|
|
|
84 |
$testName = $this->getTestName($test);
|
|
|
85 |
|
|
|
86 |
$this->cache->setTime($testName, round($time, 3));
|
|
|
87 |
$this->cache->setState($testName, BaseTestRunner::STATUS_WARNING);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
public function executeAfterLastTest(): void
|
|
|
91 |
{
|
|
|
92 |
$this->flush();
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* @param string $test A long description format of the current test
|
|
|
97 |
*
|
|
|
98 |
* @return string The test name without TestSuiteClassName:: and @dataprovider details
|
|
|
99 |
*/
|
|
|
100 |
private function getTestName(string $test): string
|
|
|
101 |
{
|
|
|
102 |
$matches = [];
|
|
|
103 |
|
|
|
104 |
if (preg_match('/^(?<name>\S+::\S+)(?:(?<dataname> with data set (?:#\d+|"[^"]+"))\s\()?/', $test, $matches)) {
|
|
|
105 |
$test = $matches['name'] . ($matches['dataname'] ?? '');
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
return $test;
|
|
|
109 |
}
|
|
|
110 |
}
|