1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\PhpUnitExtended\Listener; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
5
|
|
|
use PHPUnit\Framework\CoveredCodeNotExecutedException; |
6
|
|
|
use PHPUnit\Framework\InvalidCoversTargetException; |
7
|
|
|
use PHPUnit\Framework\MissingCoversAnnotationException; |
8
|
|
|
use PHPUnit\Framework\OutputError; |
9
|
|
|
use PHPUnit\Framework\Test; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use PHPUnit\Framework\TestListener; |
12
|
|
|
use PHPUnit\Framework\TestListenerDefaultImplementation; |
13
|
|
|
use PHPUnit\Framework\TestResult; |
14
|
|
|
use PHPUnit\Framework\UnintentionallyCoveredCodeError; |
15
|
|
|
use PHPUnit\Framework\Warning; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @see doc/listener/StrictCoverageListener.md |
19
|
|
|
*/ |
20
|
|
|
class RiskyToFailedListener implements TestListener |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
use TestListenerDefaultImplementation; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
1 |
|
public function addWarning(Test $test, Warning $e, float $time) : void |
28
|
|
|
{ |
29
|
1 |
|
$this->addErrorIfNeeded($test, $e, $time); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
11 |
|
public function addRiskyTest(Test $test, \Throwable $exception, float $time) : void |
36
|
|
|
{ |
37
|
11 |
|
$this->addErrorIfNeeded($test, $exception, $time); |
38
|
|
|
} |
39
|
|
|
|
40
|
12 |
|
protected function addErrorIfNeeded(Test $test, \Throwable $exception, $time) |
41
|
|
|
{ |
42
|
|
|
/* Must be TestCase instance to have access to "getTestResultObject" method */ |
43
|
12 |
|
if ($test instanceof TestCase) { |
44
|
12 |
|
if (!$test->getTestResultObject()) { |
45
|
11 |
|
$test->setTestResultObject(new TestResult()); |
46
|
|
|
} |
47
|
12 |
|
$test->getTestResultObject()->addFailure( |
48
|
12 |
|
$test, |
49
|
12 |
|
new AssertionFailedError( |
50
|
12 |
|
sprintf( |
51
|
12 |
|
"Strict mode - %s :\n%s", |
52
|
12 |
|
$this->getErrorReason($exception), |
53
|
12 |
|
$exception->getMessage() |
54
|
12 |
|
) |
55
|
12 |
|
), |
56
|
12 |
|
$time |
57
|
12 |
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
12 |
|
protected function getErrorReason(\Throwable $exception): string |
62
|
|
|
{ |
63
|
12 |
|
if ($exception instanceof OutputError) { |
64
|
|
|
/* beStrictAboutOutputDuringTests="true" */ |
65
|
1 |
|
return 'No output during test'; |
66
|
11 |
|
} elseif ($exception instanceof UnintentionallyCoveredCodeError |
67
|
11 |
|
|| $exception instanceof InvalidCoversTargetException |
68
|
|
|
) { |
69
|
|
|
/* checkForUnintentionallyCoveredCode="true" */ |
70
|
2 |
|
return 'Executed code must be defined with @covers and @uses annotations'; |
71
|
9 |
|
} elseif (str_contains($exception->getMessage(), '--- Global variables before the test')) { |
72
|
|
|
/* beStrictAboutChangesToGlobalState="true" (no specific exception) for globals */ |
73
|
1 |
|
return 'No global variable manipulation during test'; |
74
|
8 |
|
} elseif (str_contains($exception->getMessage(), '--- Static attributes before the test')) { |
75
|
|
|
/* beStrictAboutChangesToGlobalState="true" (no specific exception) for static var */ |
76
|
|
|
/* Only when beStrictAboutChangesToGlobalState="true" */ |
77
|
1 |
|
return 'No static attribute manipulation during test'; |
78
|
7 |
|
} elseif (str_contains($exception->getMessage(), 'This test did not perform any assertions')) { |
79
|
|
|
/* beStrictAboutTestsThatDoNotTestAnything="true" (no specific exception) */ |
80
|
1 |
|
return 'No test that do not test anything'; |
81
|
6 |
|
} elseif ($exception instanceof CoveredCodeNotExecutedException |
82
|
6 |
|
|| preg_match('#"@covers [^"]+" is invalid#', $exception->getMessage()) |
83
|
|
|
) { |
84
|
|
|
/* forceCoversAnnotation="true" (no specific exception) */ |
85
|
4 |
|
return 'Only executed code must be defined with @covers and @uses annotations'; |
86
|
2 |
|
} elseif ($exception instanceof MissingCoversAnnotationException |
87
|
2 |
|
|| str_contains( |
88
|
2 |
|
$exception->getMessage(), |
89
|
2 |
|
'This test does not have a @covers annotation but is expected to have one' |
90
|
2 |
|
) |
91
|
|
|
) { |
92
|
|
|
/* forceCoversAnnotation="true" (no specific exception) */ |
93
|
1 |
|
return 'Missing @covers or @coversNothing annotation'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Always return an error even if it's not a known/managed error |
97
|
1 |
|
return $exception->getMessage(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.