Passed
Branch master (cd2b9b)
by Scott
03:31
created

CoverageCheckTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 226
Duplicated Lines 48.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 110
loc 226
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testCoverage() 0 56 5
A testCoverageFailed() 0 54 3
A testAddingAllUnknownsCovered() 55 55 3
A testAddingAllUnknownsUnCovered() 55 55 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace exussum12\CoverageChecker\tests;
3
4
use PHPUnit\Framework\TestCase;
5
use exussum12\CoverageChecker\CoverageCheck;
6
use exussum12\CoverageChecker\DiffFileLoader;
7
use exussum12\CoverageChecker\FileMatchers;
8
use exussum12\CoverageChecker\XMLReport;
9
10
class CoverageCheckTest extends TestCase
11
{
12
    public function testCoverage()
13
    {
14
        $diffFileState = $this->createMock(DiffFileLoader::class);
15
        $diffFileState->method('getChangedLines')
16
            ->willReturn([
17
                'testFile1.php' => [1,2,3,4],
18
                'testFile2.php' => [3,4]
19
20
            ]);
21
22
        $xmlReport = $this->createMock(XMLReport::class);
23
        $xmlReport->method('getLines')
24
            ->willReturn([
25
                '/full/path/to/testFile1.php' => [1 => 1,2 => 0,3 => 1,4 => 1],
26
                '/full/path/to/testFile2.php' => [3 => 1,4 => 0]
27
28
            ]);
29
30
        $xmlReport->method('isValidLine')
31
            ->will(
32
                $this->returnCallback(
33
                    function () {
34
                        $file = func_get_arg(0);
35
                        $line = func_get_arg(1);
36
37
                        if ($file == '/full/path/to/testFile1.php' && $line == 2) {
38
                            return false;
39
                        }
40
                        if ($file == '/full/path/to/testFile2.php' && $line == 4) {
41
                            return false;
42
                        }
43
44
                        return true;
45
                    }
46
                )
47
            );
48
49
        $matcher = new FileMatchers\EndsWith;
50
        $coverageCheck = new CoverageCheck($diffFileState, $xmlReport, $matcher);
51
        $lines = $coverageCheck->getCoveredLines();
52
        $uncoveredLines = [
53
            'testFile1.php' => [2 => 0],
54
            'testFile2.php' => [4 => 0],
55
        ];
56
        $coveredLines = [
57
            'testFile1.php' => [1,3,4],
58
            'testFile2.php' => [3],
59
        ];
60
61
        $expected = [
62
            'coveredLines' => $coveredLines,
63
            'uncoveredLines' => $uncoveredLines,
64
        ];
65
66
        $this->assertEquals($expected, $lines);
67
    }
68
69
    public function testCoverageFailed()
70
    {
71
        $diffFileState = $this->createMock(DiffFileLoader::class);
72
        $diffFileState->method('getChangedLines')
73
            ->willReturn([
74
                'testFile1.php' => [1,2,3,4],
75
                'testFile2.php' => [3,4],
76
77
            ]);
78
79
        $xmlReport = $this->createMock(XMLReport::class);
80
        $xmlReport->method('getLines')
81
            ->willReturn([
82
                '/full/path/to/testFile1.php' => [1 => 1,2 => 0,3 => 1,4 => 1],
83
84
            ]);
85
86
        $xmlReport->method('handleNotFoundFile')
87
            ->willReturn(null);
88
89
        $xmlReport->method('isValidLine')
90
            ->will(
91
                $this->returnCallback(
92
                    function () {
93
                        $file = func_get_arg(0);
94
                        $line = func_get_arg(1);
95
96
                        if ($file == '/full/path/to/testFile1.php' && $line == 2) {
97
                            return false;
98
                        }
99
100
                        return true;
101
                    }
102
                )
103
            );
104
105
        $matcher = new FileMatchers\EndsWith;
106
        $coverageCheck = new CoverageCheck($diffFileState, $xmlReport, $matcher);
107
        $lines = $coverageCheck->getCoveredLines();
108
109
        $uncoveredLines = [
110
            'testFile1.php' => [2 => 0],
111
        ];
112
        $coveredLines = [
113
            'testFile1.php' => [1,3,4],
114
        ];
115
116
        $expected = [
117
            'coveredLines' => $coveredLines,
118
            'uncoveredLines' => $uncoveredLines,
119
        ];
120
121
        $this->assertEquals($expected, $lines);
122
    }
123
124 View Code Duplication
    public function testAddingAllUnknownsCovered()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $diffFileState = $this->createMock(DiffFileLoader::class);
127
        $diffFileState->method('getChangedLines')
128
            ->willReturn([
129
                'testFile1.php' => [1,2,3,4],
130
                'testFile2.php' => [3,4],
131
132
            ]);
133
134
        $xmlReport = $this->createMock(XMLReport::class);
135
        $xmlReport->method('getLines')
136
            ->willReturn([
137
                '/full/path/to/testFile1.php' => [1 => 1,2 => 0,3 => 1,4 => 1],
138
139
            ]);
140
141
        $xmlReport->method('handleNotFoundFile')
142
            ->willReturn(true);
143
144
        $xmlReport->method('isValidLine')
145
            ->will(
146
                $this->returnCallback(
147
                    function () {
148
                        $file = func_get_arg(0);
149
                        $line = func_get_arg(1);
150
151
                        if ($file == '/full/path/to/testFile1.php' && $line == 2) {
152
                            return false;
153
                        }
154
155
                        return true;
156
                    }
157
                )
158
            );
159
160
        $matcher = new FileMatchers\EndsWith;
161
        $coverageCheck = new CoverageCheck($diffFileState, $xmlReport, $matcher);
162
        $lines = $coverageCheck->getCoveredLines();
163
164
        $uncoveredLines = [
165
            'testFile1.php' => [2 => 0],
166
        ];
167
        $coveredLines = [
168
            'testFile1.php' => [1,3,4],
169
            'testFile2.php' => [3,4],
170
        ];
171
172
        $expected = [
173
            'coveredLines' => $coveredLines,
174
            'uncoveredLines' => $uncoveredLines,
175
        ];
176
177
        $this->assertEquals($expected, $lines);
178
    }
179
180 View Code Duplication
    public function testAddingAllUnknownsUnCovered()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182
        $diffFileState = $this->createMock(DiffFileLoader::class);
183
        $diffFileState->method('getChangedLines')
184
            ->willReturn([
185
                'testFile1.php' => [1,2,3,4],
186
                'testFile2.php' => [3,4],
187
188
            ]);
189
190
        $xmlReport = $this->createMock(XMLReport::class);
191
        $xmlReport->method('getLines')
192
            ->willReturn([
193
                '/full/path/to/testFile1.php' => [1 => 1,2 => 0,3 => 1,4 => 1],
194
195
            ]);
196
197
        $xmlReport->method('handleNotFoundFile')
198
            ->willReturn(false);
199
200
        $xmlReport->method('isValidLine')
201
            ->will(
202
                $this->returnCallback(
203
                    function () {
204
                        $file = func_get_arg(0);
205
                        $line = func_get_arg(1);
206
207
                        if ($file == '/full/path/to/testFile1.php' && $line == 2) {
208
                            return false;
209
                        }
210
211
                        return true;
212
                    }
213
                )
214
            );
215
216
        $matcher = new FileMatchers\EndsWith;
217
        $coverageCheck = new CoverageCheck($diffFileState, $xmlReport, $matcher);
218
        $lines = $coverageCheck->getCoveredLines();
219
220
        $uncoveredLines = [
221
            'testFile1.php' => [2 => 0],
222
            'testFile2.php' => [3 => 0, 4 => 0],
223
        ];
224
        $coveredLines = [
225
            'testFile1.php' => [1,3,4],
226
        ];
227
228
        $expected = [
229
            'coveredLines' => $coveredLines,
230
            'uncoveredLines' => $uncoveredLines,
231
        ];
232
233
        $this->assertEquals($expected, $lines);
234
    }
235
}
236