DiffFilter   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 27

15 Methods

Rating   Name   Duplication   Size   Complexity  
A endTest() 0 2 1
A addIncompleteTest() 0 2 1
A onCancel() 0 2 1
A addFailure() 0 2 1
A addSkippedTest() 0 2 1
A endTestSuite() 0 2 1
A addRiskyTest() 0 2 1
A onFatalError() 0 2 1
A startTest() 0 2 1
A startsWith() 0 4 1
A addError() 0 2 1
A hasTestChanged() 0 9 3
A shouldRunTest() 0 16 5
A __construct() 0 12 2
A startTestSuite() 0 28 6
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use Exception;
5
use PHPUnit_Framework_AssertionFailedError as AssertionFailedError;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_AssertionFailedError was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use PHPUnit_Framework_Test as Test;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_Test was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use PHPUnit_Framework_TestCase as TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PHPUnit_Framework_TestListener as TestListener;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestListener was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use PHPUnit_Framework_TestSuite as TestSuite;
0 ignored issues
show
Bug introduced by
The type PHPUnit_Framework_TestSuite was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * Coverage ignored due to not being able to run this
13
 * @codeCoverageIgnore
14
 * PHPMD suppressed due to having to implement TestListener
15
 * @SuppressWarnings(PHPMD)
16
 */
17
class DiffFilter implements TestListener
18
{
19
    protected $modifiedTests = null;
20
    protected $modifiedSuites = null;
21
    public function __construct($old, $diff, $fuzziness = 0)
22
    {
23
        try {
24
            $diff = new DiffFileLoaderOldVersion($diff);
25
            $matcher = new FileMatchers\EndsWith();
26
            $coverage = new PhpunitFilter($diff, $matcher, $old);
27
            $this->modifiedTests = $coverage->getTestsForRunning($fuzziness);
28
            $this->modifiedSuites = array_keys($this->modifiedTests);
29
            unset($coverage);
30
        } catch (Exception $exception) {
31
            //Something has gone wrong, Don't filter
32
            echo "Missing required diff / php coverage, Running all tests\n";
33
        }
34
    }
35
36
    public function addError(Test $test, Exception $exception, $time)
37
    {
38
    }
39
    public function addFailure(Test $test, AssertionFailedError $exception, $time)
40
    {
41
    }
42
    public function addRiskyTest(Test $test, Exception $exception, $time)
43
    {
44
    }
45
    public function startTestSuite(TestSuite $suite)
46
    {
47
        if (!is_array($this->modifiedTests)) {
48
            return;
49
        }
50
51
        $suiteName = $suite->getName();
52
        $runTests = [];
53
        if (empty($suiteName)) {
54
            return;
55
        }
56
57
        $tests = $suite->tests();
58
                         
59
        foreach ($tests as $test) {
60
            $skipTest =
61
                $test instanceof TestCase &&
62
                !$this->hasTestChanged(
63
                    $test
64
                );
65
66
            if ($skipTest) {
67
                continue;
68
            }
69
            $runTests[] = $test;
70
        }
71
72
        $suite->setTests($runTests);
73
    }
74
    public function startTest(Test $test)
75
    {
76
    }
77
    public function endTest(Test $test, $time)
78
    {
79
    }
80
    public function addIncompleteTest(Test $test, Exception $e, $time)
81
    {
82
    }
83
    public function addSkippedTest(Test $test, Exception $e, $time)
84
    {
85
    }
86
    public function endTestSuite(TestSuite $suite)
87
    {
88
    }
89
    public function onFatalError()
90
    {
91
    }
92
    public function onCancel()
93
    {
94
    }
95
96
    public function startsWith($haystack, $needle)
97
    {
98
        $length = strlen($needle);
99
        return (substr($haystack, 0, $length) === $needle);
100
    }
101
102
    protected function shouldRunTest($modifiedTest, $currentTest, $class)
103
    {
104
        foreach ($modifiedTest as $test) {
105
            $testName = $currentTest->getName();
106
            $testMatches =
107
                strpos($class, get_class($currentTest)) !== false &&
108
                (
109
                    empty($test) ||
110
                    strpos($test, $testName) !== false
111
                )
112
            ;
113
            if ($testMatches) {
114
                return true;
115
            }
116
        }
117
        return false;
118
    }
119
120
    private function hasTestChanged(TestCase $test)
121
    {
122
        foreach ($this->modifiedTests as $class => $modifiedTest) {
123
            if ($this->shouldRunTest($modifiedTest, $test, $class)) {
124
                return true;
125
            }
126
        }
127
128
        return false;
129
    }
130
}
131