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

StrictCoverageListener   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 4
dl 0
loc 41
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addRiskyTest() 0 9 3
B removeCoverageFor() 0 20 7
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
            $test instanceof \PHPUnit_Framework_TestCase
18 3
            && $e instanceof \PHPUnit_Framework_OutputError
19 3
        ) {
20 3
            $this->removeCoverageFor($test);
21 3
        }
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 3
                            }
40 3
                        }
41 3
                    }
42 3
                }
43 3
            }
44 3
            $coverage->setData($data);
45 3
        }
46 3
    }
47
}
48