Completed
Push — master ( 45b1e4...2e87eb )
by Yo
02:14
created

RiskyToFailedListener::addWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
crap 1
1
<?php
2
namespace Yoanm\PhpUnitExtended\Listener;
3
4
use PHPUnit\Framework\AssertionFailedError;
5
use PHPUnit\Framework\OutputError;
6
use PHPUnit\Framework\Test;
7
use PHPUnit\Framework\TestCase;
8
use PHPUnit\Framework\TestListener;
9
use PHPUnit\Framework\TestListenerDefaultImplementation;
10
use PHPUnit\Framework\UnintentionallyCoveredCodeError;
11
use PHPUnit\Framework\Warning;
12
13
/**
14
 * @see doc/listener/StrictCoverageListener.md
15
 */
16
class RiskyToFailedListener implements TestListener
17
{
18
    use TestListenerDefaultImplementation;
19
20 1
    public function addWarning(Test $test, Warning $e, $time)
21
    {
22 1
        $this->addErrorIfNeeded($test, $e, $time);
23 1
    }
24
25
    /**
26
     * @param Test $test
27
     * @param \Exception              $e
28
     * @param float                   $time
29
     */
30 6
    public function addRiskyTest(Test $test, \Exception $e, $time)
31
    {
32 6
        $this->addErrorIfNeeded($test, $e, $time);
33 6
    }
34
35
    /**
36
     * @param Test $test
37
     * @param \Exception $e
38
     * @param $time
39
     */
40 7
    protected function addErrorIfNeeded(Test $test, \Exception $e, $time)
41
    {
42
        /* Must be TestCase instance to have access to "getTestResultObject" method */
43 7
        if ($test instanceof TestCase) {
44 7
            $reason = $this->getErrorReason($e);
45 7
            if (null !== $reason) {
46 6
                $test->getTestResultObject()->addFailure(
47 6
                    $test,
48 6
                    new AssertionFailedError(
49 6
                        sprintf(
50 6
                            "Strict mode - %s :\n%s",
51 6
                            $reason,
52 6
                            $e->getMessage()
53
                        )
54
                    ),
55 6
                    $time
56
                );
57
            }
58
        }
59 7
    }
60
61
    /**
62
     * @param \Exception $e
63
     *
64
     * @return null|string
65
     */
66 7
    protected function getErrorReason(\Exception $e)
67
    {
68 7
        $reason = null;
69
        switch (true) {
70
            /* beStrictAboutOutputDuringTests="true" */
71 7
            case $e instanceof OutputError:
72 1
                $reason = 'No output during test';
73 1
                break;
74
            /* checkForUnintentionallyCoveredCode="true" */
75 6
            case $e instanceof UnintentionallyCoveredCodeError:
76 1
                $reason = 'Executed code must be defined with @covers and @uses annotations';
77 1
                break;
78
            default:
79 5
                if (preg_match('#\-\-\- Global variables before the test#', $e->getMessage())) {
80
                    /* beStrictAboutChangesToGlobalState="true" (no specific exception) for globals */
81 1
                    $reason = 'No global variable manipulation during test';
82 4
                } elseif (preg_match('#\-\-\- Static attributes before the test#', $e->getMessage())) {
83
                    /* beStrictAboutChangesToGlobalState="true" (no specific exception) for static var */
84
                    /* Only when beStrictAboutChangesToGlobalState="true" */
85 1
                    $reason = 'No static attribute manipulation during test';
86 3
                } elseif (preg_match('#This test did not perform any assertions#', $e->getMessage())) {
87
                    /* beStrictAboutTestsThatDoNotTestAnything="true" (no specific exception) */
88 1
                    $reason = 'No test that do not test anything';
89 2
                } elseif (preg_match('#Trying to @cover or @use not existing #', $e->getMessage())) {
90
                    /* forceCoversAnnotation="true" (no specific exception) */
91 1
                    $reason = 'Only executed code must be defined with @covers and @uses annotations';
92
                }
93 5
                break;
94
        }
95 7
        return $reason;
96
    }
97
}
98