Skip to content

Commit 741c582

Browse files
Add support for multiple coverage files passed as argument
1 parent b3e00f5 commit 741c582

File tree

1 file changed

+89
-84
lines changed

1 file changed

+89
-84
lines changed

tests/Unit/Lib/IO/MetricsFactoryTest.php

+89-84
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,14 @@ class MetricsFactoryTest extends TestCase
1919
* @covers ::getMethodMetrics
2020
* @covers ::mergeFileMetrics
2121
* @covers ::getCoveredStatements
22+
* @dataProvider multiFilesDataProvider
2223
*/
23-
public function testGetFilesMetrics(): void
24+
public function testGetFilesMetrics(string $file1Data, string $file2Data, int $methods, float $coverage, array $coveredStatements): void
2425
{
2526
$xml1 = '<?xml version="1.0" encoding="UTF-8"?>
2627
<coverage generated="1598702199">
2728
<project timestamp="1598702199">
28-
<file name="/API\\Example.php">
29-
<line num="1" type="method" name="methodName" count="2"/>
30-
<line num="2" type="stmt" count="2"/>
31-
<line num="3" type="stmt" count="0"/>
32-
<line num="4" type="stmt" count="0"/>
33-
<line num="5" type="stmt" count="0"/>
34-
<metrics loc="11" ncloc="11" statements="4" coveredstatements="1"/>
35-
</file>
36-
<file name="/API\\Example2.php">
37-
<line num="1" type="method" name="methodName" count="0"/>
38-
<line num="2" type="stmt" count="0"/>
39-
<line num="3" type="stmt" count="0"/>
40-
<line num="4" type="stmt" count="0"/>
41-
<line num="5" type="stmt" count="0"/>
42-
<line num="6" type="stmt" count="0"/>
43-
<metrics loc="11" ncloc="11" statements="5" coveredstatements="0"/>
44-
</file>
45-
<file name="/API\\Example3.php">
46-
<metrics loc="11" ncloc="11" statements="0" coveredstatements="0"/>
47-
</file>
48-
<file name="/API\\Example4.php">
49-
<metrics loc="11" ncloc="11" statements="1" coveredstatements="1"/>
50-
</file>
51-
<file name="/API\\Example5.php">
52-
<metrics loc="11" ncloc="11" statements="2" coveredstatements="0"/>
53-
</file>
29+
' . $file1Data . '
5430
</project>
5531
</coverage>';
5632

@@ -60,71 +36,21 @@ public function testGetFilesMetrics(): void
6036
$xml2 = '<?xml version="1.0" encoding="UTF-8"?>
6137
<coverage generated="1598702199">
6238
<project timestamp="1598702199">
63-
<file name="/API\\Example.php">
64-
<line num="1" type="method" name="methodName" count="1"/>
65-
<line num="2" type="stmt" count="1"/>
66-
<line num="3" type="stmt" count="0"/>
67-
<line num="4" type="stmt" count="1"/>
68-
<line num="5" type="stmt" count="0"/>
69-
<metrics loc="11" ncloc="11" statements="4" coveredstatements="2"/>
70-
</file>
71-
<file name="/API\\Example2.php">
72-
<line num="1" type="method" name="methodName" count="1"/>
73-
<line num="2" type="stmt" count="1"/>
74-
<line num="3" type="stmt" count="1"/>
75-
<line num="4" type="stmt" count="1"/>
76-
<line num="5" type="stmt" count="0"/>
77-
<line num="6" type="stmt" count="1"/>
78-
<metrics loc="11" ncloc="11" statements="5" coveredstatements="4"/>
79-
</file>
80-
<file name="/API\\Example4.php">
81-
<metrics loc="11" ncloc="11" statements="0" coveredstatements="0"/>
82-
</file>
83-
<file name="/API\\Example5.php">
84-
<metrics loc="11" ncloc="11" statements="2" coveredstatements="2"/>
85-
</file>
39+
' . $file2Data . '
8640
</project>
8741
</coverage>';
8842

8943
$dom2 = new DOMDocument();
9044
$dom2->loadXML($xml2);
9145

9246
$metrics = MetricsFactory::getFilesMetrics([$dom1, $dom2]);
93-
static::assertCount(5, $metrics);
47+
static::assertCount(1, $metrics);
9448

95-
$metric = $metrics['/API/Example.php'];
96-
static::assertSame('/API/Example.php', $metric->getFilepath());
97-
static::assertCount(1, $metric->getMethods());
98-
$method = $metric->getMethods()['methodName'];
99-
static::assertSame('methodName', $method->getMethodName());
100-
static::assertSame(1, $method->getLineNumber());
101-
static::assertSame(2, $method->getCount());
102-
static::assertSame(50.0, $metric->getCoverage());
103-
static::assertSame([2, 4], $metric->getCoveredStatements());
104-
105-
$metric = $metrics['/API/Example2.php'];
106-
static::assertSame('/API/Example2.php', $metric->getFilepath());
107-
static::assertCount(1, $metric->getMethods());
108-
static::assertSame(80.0, $metric->getCoverage());
109-
static::assertSame([2, 3, 4, 6], $metric->getCoveredStatements());
110-
111-
$metric = $metrics['/API/Example3.php'];
112-
static::assertSame('/API/Example3.php', $metric->getFilepath());
113-
static::assertSame([], $metric->getMethods());
114-
static::assertSame(100.0, $metric->getCoverage());
115-
static::assertSame([], $metric->getCoveredStatements());
116-
117-
$metric = $metrics['/API/Example4.php'];
118-
static::assertSame('/API/Example4.php', $metric->getFilepath());
119-
static::assertSame([], $metric->getMethods());
120-
static::assertSame(100.0, $metric->getCoverage());
121-
static::assertSame([], $metric->getCoveredStatements());
122-
123-
$metric = $metrics['/API/Example5.php'];
124-
static::assertSame('/API/Example5.php', $metric->getFilepath());
125-
static::assertSame([], $metric->getMethods());
126-
static::assertSame(100.0, $metric->getCoverage());
127-
static::assertSame([], $metric->getCoveredStatements());
49+
$metric = $metrics['utTestFile.php'];
50+
static::assertSame('utTestFile.php', $metric->getFilepath());
51+
static::assertCount($methods, $metric->getMethods());
52+
static::assertSame($coverage, $metric->getCoverage());
53+
static::assertSame($coveredStatements, $metric->getCoveredStatements());
12854
}
12955

13056
/**
@@ -176,4 +102,83 @@ public function testGetMetricsEmptyXmlShouldReturnEmptyArray(): void
176102

177103
static::assertCount(0, MetricsFactory::getFilesMetrics([$dom]));
178104
}
105+
106+
public static function multiFilesDataProvider(): array
107+
{
108+
return [
109+
'second coverage is used' => [
110+
'<file name="utTestFile.php">
111+
<line num="1" type="method" name="methodName" count="2"/>
112+
<line num="2" type="stmt" count="2"/>
113+
<line num="3" type="stmt" count="0"/>
114+
<line num="4" type="stmt" count="0"/>
115+
<line num="5" type="stmt" count="0"/>
116+
<metrics loc="11" ncloc="11" statements="4" coveredstatements="1"/>
117+
</file>',
118+
'<file name="utTestFile.php">
119+
<line num="1" type="method" name="methodName" count="1"/>
120+
<line num="2" type="stmt" count="1"/>
121+
<line num="3" type="stmt" count="0"/>
122+
<line num="4" type="stmt" count="1"/>
123+
<line num="5" type="stmt" count="0"/>
124+
<metrics loc="11" ncloc="11" statements="4" coveredstatements="2"/>
125+
</file>',
126+
1,
127+
50.0,
128+
[2, 4]
129+
],
130+
'second count is higher' => [
131+
'<file name="utTestFile.php">
132+
<line num="1" type="method" name="methodName" count="1"/>
133+
<line num="2" type="stmt" count="1"/>
134+
<line num="3" type="stmt" count="1"/>
135+
<line num="4" type="stmt" count="1"/>
136+
<line num="5" type="stmt" count="1"/>
137+
<metrics loc="11" ncloc="11" statements="4" coveredstatements="1"/>
138+
</file>',
139+
'<file name="utTestFile.php">
140+
<line num="1" type="method" name="methodName" count="2"/>
141+
<line num="2" type="stmt" count="1"/>
142+
<line num="3" type="stmt" count="0"/>
143+
<line num="4" type="stmt" count="1"/>
144+
<line num="5" type="stmt" count="0"/>
145+
<metrics loc="11" ncloc="11" statements="4" coveredstatements="2"/>
146+
</file>',
147+
1,
148+
100.0,
149+
[2, 3, 4, 5]
150+
],
151+
'no second coverage' => [
152+
'<file name="utTestFile.php">
153+
<metrics loc="11" ncloc="11" statements="0" coveredstatements="0"/>
154+
</file>',
155+
'',
156+
0,
157+
100.0,
158+
[]
159+
],
160+
'first 100 %' => [
161+
'<file name="utTestFile.php">
162+
<metrics loc="11" ncloc="11" statements="1" coveredstatements="1"/>
163+
</file>',
164+
' <file name="utTestFile.php">
165+
<metrics loc="11" ncloc="11" statements="0" coveredstatements="0"/>
166+
</file>',
167+
0,
168+
100.0,
169+
[]
170+
],
171+
'second 100%' => [
172+
'<file name="utTestFile.php">
173+
<metrics loc="11" ncloc="11" statements="2" coveredstatements="0"/>
174+
</file>',
175+
'<file name="utTestFile.php">
176+
<metrics loc="11" ncloc="11" statements="2" coveredstatements="2"/>
177+
</file>',
178+
0,
179+
100.0,
180+
[]
181+
]
182+
];
183+
}
179184
}

0 commit comments

Comments
 (0)