|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\PhpUnitExtended\Listener; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* @see https://github.com/yoanm/PhpUnitExtended/doc/listener/StrictCoverageListener.md |
|
6
|
|
|
*/ |
|
7
|
|
|
class RiskyToFailedListener extends \PHPUnit_Framework_BaseTestListener |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @param \PHPUnit_Framework_Test $test |
|
11
|
|
|
* @param \Exception $e |
|
12
|
|
|
* @param float $time |
|
13
|
|
|
*/ |
|
14
|
6 |
|
public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
|
15
|
|
|
{ |
|
16
|
|
|
/* Must be PHPUnit_Framework_TestCase instance to have access to "getTestResultObject" method */ |
|
17
|
6 |
|
if ($test instanceof \PHPUnit_Framework_TestCase) { |
|
18
|
6 |
|
$reason = $this->processEvent($test, $e); |
|
19
|
6 |
|
if (null !== $reason) { |
|
20
|
5 |
|
$test->getTestResultObject()->addFailure( |
|
21
|
5 |
|
$test, |
|
22
|
5 |
|
new \PHPUnit_Framework_AssertionFailedError( |
|
23
|
5 |
|
sprintf( |
|
24
|
5 |
|
"Strict mode - %s :\n%s", |
|
25
|
5 |
|
$reason, |
|
26
|
5 |
|
$e->getMessage() |
|
27
|
5 |
|
) |
|
28
|
5 |
|
), |
|
29
|
|
|
$time |
|
30
|
5 |
|
); |
|
31
|
5 |
|
} |
|
32
|
6 |
|
} |
|
33
|
6 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param \PHPUnit_Framework_TestCase $test |
|
37
|
|
|
* @param \Exception $e |
|
38
|
|
|
* |
|
39
|
|
|
* @return null|string |
|
40
|
|
|
*/ |
|
41
|
6 |
|
protected function processEvent(\PHPUnit_Framework_TestCase $test, \Exception $e) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
6 |
|
$reason = null; |
|
44
|
6 |
|
switch (true) { |
|
45
|
|
|
/* beStrictAboutOutputDuringTests="true" */ |
|
46
|
6 |
|
case $e instanceof \PHPUnit_Framework_OutputError: |
|
47
|
1 |
|
$reason = 'No output during test'; |
|
48
|
1 |
|
break; |
|
49
|
|
|
/* checkForUnintentionallyCoveredCode="true" */ |
|
50
|
5 |
|
case $e instanceof \PHPUnit_Framework_UnintentionallyCoveredCodeError: |
|
51
|
1 |
|
$reason = 'Executed code must be defined with @covers and @uses annotations'; |
|
52
|
1 |
|
break; |
|
53
|
4 |
|
default: |
|
54
|
4 |
|
if (preg_match('#\-\-\- Global variables before the test#', $e->getMessage())) { |
|
55
|
|
|
/* beStrictAboutChangesToGlobalState="true" (no specific exception) for globals */ |
|
56
|
1 |
|
$reason = 'No global variable manipulation during test'; |
|
57
|
4 |
|
} elseif (preg_match('#\-\-\- Static attributes before the test#', $e->getMessage())) { |
|
58
|
|
|
/* beStrictAboutChangesToGlobalState="true" (no specific exception) for static var */ |
|
59
|
|
|
/* Only when beStrictAboutChangesToGlobalState="true" */ |
|
60
|
1 |
|
$reason = 'No static attribute manipulation during test'; |
|
61
|
3 |
|
} elseif (preg_match('#This test did not perform any assertions#', $e->getMessage())) { |
|
62
|
|
|
/* beStrictAboutTestsThatDoNotTestAnything="true" (no specific exception) */ |
|
63
|
1 |
|
$reason = 'No test that do not test anything'; |
|
64
|
1 |
|
} |
|
65
|
4 |
|
break; |
|
66
|
4 |
|
} |
|
67
|
6 |
|
return $reason; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.