Completed
Push — master ( 120c03...45b1e4 )
by Yo
02:28
created

RiskyToFailedListener   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 63
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addRiskyTest() 0 20 3
B processEvent() 0 28 6
1
<?php
2
namespace Yoanm\PhpUnitExtended\Listener;
3
4
/**
5
 * @see 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)
0 ignored issues
show
Unused Code introduced by
The parameter $test is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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