Passed
Branch master (45b1e4)
by Yo
02:00
created

StrictCoverageListener::addRiskyTest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 3
crap 3
1
<?php
2
namespace Yoanm\PhpUnitExtended\Listener;
3
4
/**
5
 * @see doc/listener/StrictCoverageListener.md
6
 */
7
class StrictCoverageListener extends \PHPUnit_Framework_BaseTestListener
8
{
9
    /**
10
     * @param \PHPUnit_Framework_Test $test
11
     * @param \Exception              $e
12
     * @param float                   $time
13
     */
14 3
    public function addRiskyTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
15
    {
16
        if (/* Must be PHPUnit_Framework_TestCase instance to have access to "getTestResultObject" method */
17 3
            $test instanceof \PHPUnit_Framework_TestCase
18 3
            && $e instanceof \PHPUnit_Framework_OutputError
19
        ) {
20 3
            $this->removeCoverageFor($test);
21
        }
22 3
    }
23
24
    /**
25
     * @param \PHPUnit_Framework_TestCase $test
26
     */
27 3
    protected function removeCoverageFor(\PHPUnit_Framework_TestCase $test)
28
    {
29 3
        $coverage = $test->getTestResultObject()->getCodeCoverage();
30 3
        if (null !== $coverage) {
31 3
            $id = $test->toString();
32 3
            $data = $coverage->getData();
33 3
            foreach ($data as $fileName => $lineData) {
34 3
                foreach ($lineData as $lineNumber => $testIdList) {
35 3
                    if (is_array($testIdList)) {
36 3
                        foreach ($testIdList as $testIdKey => $testId) {
37 3
                            if ($id === $testId) {
38 3
                                unset($data[$fileName][$lineNumber][$testIdKey]);
39
                            }
40
                        }
41
                    }
42
                }
43
            }
44 3
            $coverage->setData($data);
45
        }
46 3
    }
47
}
48