Skip to content

Commit 06e52ad

Browse files
Allow to set cleanUpCloverXml
1 parent 88669de commit 06e52ad

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Navigate to your `phpunit.xml.dist` file and add following config to set default
3030
```xml
3131
<extensions>
3232
<bootstrap class="RobinIngelbrecht\PHPUnitCoverageTools\PhpUnitExtension">
33-
<parameter name="exitOnLowCoverage" value="true|false"/>
33+
<parameter name="exitOnLowCoverage" value="0|1"/>
34+
<parameter name="cleanUpCloverXml" value="0|1"/>
3435
</bootstrap>
3536
</extensions>
3637
```
@@ -69,6 +70,7 @@ return [
6970
MinCoverageRules::TOTAL => 20,
7071
'RobinIngelbrecht\PHPUnitCoverageTools\*' => 80,
7172
'RobinIngelbrecht\PHPUnitCoverageTools\Subscriber\Application\ApplicationFinishedSubscriber' => 100,
73+
'RobinIngelbrecht\PHPUnitCoverageTools\*CommandHandler' => 100,
7274
];
7375
```
7476

@@ -77,6 +79,11 @@ This example will enforce:
7779
- A minimum total coverage of *20%*
7880
- A minimum coverage of *80%* for all classes in namespace `RobinIngelbrecht\PHPUnitCoverageTools`
7981
- *100%* code coverage for the class `ApplicationFinishedSubscriber`
82+
- *100%* code coverage for the classes ending with `CommandHandler`
83+
84+
### --clean-up-clover-xml
85+
86+
Adding this argument will clean up the generated clover file after the application has finished running.
8087

8188
### Example when coverage is too low
8289

src/Subscriber/Application/ApplicationFinishedSubscriber.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __construct(
2121
private readonly string $relativePathToCloverXml,
2222
private readonly MinCoverageRules $minCoverageRules,
2323
private readonly bool $exitOnLowCoverage,
24+
private readonly bool $cleanUpCloverXml,
2425
private readonly Exitter $exitter,
2526
private readonly ConsoleOutput $consoleOutput,
2627
) {
@@ -61,6 +62,10 @@ public function notify(Finished $event): void
6162
}
6263
$reader->close();
6364

65+
if ($this->cleanUpCloverXml) {
66+
unlink($absolutePathToCloverXml);
67+
}
68+
6469
if (!$metrics && !$metricTotal) {
6570
throw new \RuntimeException('Could not determine coverage metrics');
6671
}
@@ -116,10 +121,15 @@ public static function fromConfigurationAndParameters(
116121
return null;
117122
}
118123

124+
if (!$cleanUpCloverXml = in_array('--clean-up-clover-xml', $_SERVER['argv'], true)) {
125+
$cleanUpCloverXml = $parameters->has('cleanUpCloverXml') && (int) $parameters->get('cleanUpCloverXml');
126+
}
127+
119128
return new self(
120129
$configuration->coverageClover(),
121130
$rules,
122-
$parameters->has('exitOnLowCoverage') && $parameters->get('exitOnLowCoverage'),
131+
$parameters->has('exitOnLowCoverage') && (int) $parameters->get('exitOnLowCoverage'),
132+
$cleanUpCloverXml,
123133
new Exitter(),
124134
new ConsoleOutput(new \Symfony\Component\Console\Output\ConsoleOutput()),
125135
);

tests/Subscriber/Application/ApplicationFinishedSubscriberTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function testNotifyWithAtLeastOneFailedRule(): void
3737
'tests/clover.xml',
3838
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-failed-rule.php'),
3939
true,
40+
false,
4041
$exitter,
4142
new ConsoleOutput($spyOutput),
4243
);
@@ -67,6 +68,7 @@ public function testNotifyWithAWarning(): void
6768
'tests/clover.xml',
6869
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-warning.php'),
6970
false,
71+
false,
7072
new Exitter(),
7173
new ConsoleOutput($spyOutput),
7274
);
@@ -97,6 +99,7 @@ public function testNotifyWhenCoverageIsOk(): void
9799
'tests/clover.xml',
98100
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
99101
false,
102+
false,
100103
new Exitter(),
101104
new ConsoleOutput($spyOutput),
102105
);
@@ -127,6 +130,7 @@ public function testNotifyWhitOnlyTotal(): void
127130
'tests/clover.xml',
128131
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-total-only.php'),
129132
false,
133+
false,
130134
new Exitter(),
131135
new ConsoleOutput($spyOutput),
132136
);
@@ -161,6 +165,7 @@ public function testNotifyWhitInvalidRules(): void
161165
'tests/clover.xml',
162166
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-invalid.php'),
163167
false,
168+
false,
164169
new Exitter(),
165170
new ConsoleOutput($spyOutput),
166171
);
@@ -173,6 +178,7 @@ public function testNotifyWithNonExistingCloverFile(): void
173178
'tests/clover-wrong.xml',
174179
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
175180
false,
181+
false,
176182
new Exitter(),
177183
new ConsoleOutput($spyOutput),
178184
);
@@ -203,6 +209,7 @@ public function testNotifyWithInvalidCloverFile(): void
203209
'tests/clover-invalid.xml',
204210
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
205211
false,
212+
false,
206213
new Exitter(),
207214
new ConsoleOutput($spyOutput),
208215
);
@@ -227,13 +234,53 @@ public function testNotifyWithInvalidCloverFile(): void
227234
));
228235
}
229236

237+
public function testNotifyWithCleanUpCloverFile(): void
238+
{
239+
copy(dirname(__DIR__, 2).'/clover.xml', dirname(__DIR__, 2).'/clover-to-delete.xml');
240+
$exitter = $this->createMock(Exitter::class);
241+
242+
$exitter
243+
->expects($this->once())
244+
->method('exit')
245+
->with(1);
246+
247+
$spyOutput = new SpyOutput();
248+
$subscriber = new ApplicationFinishedSubscriber(
249+
'tests/clover-to-delete.xml',
250+
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-failed-rule.php'),
251+
true,
252+
true,
253+
$exitter,
254+
new ConsoleOutput($spyOutput),
255+
);
256+
257+
$subscriber->notify(new Finished(
258+
new Info(
259+
new Snapshot(
260+
HRTime::fromSecondsAndNanoseconds(1, 0),
261+
MemoryUsage::fromBytes(100),
262+
MemoryUsage::fromBytes(100),
263+
new GarbageCollectorStatus(0, 0, 0, 0, null, null, null, null)
264+
),
265+
Duration::fromSecondsAndNanoseconds(1, 0),
266+
MemoryUsage::fromBytes(100),
267+
Duration::fromSecondsAndNanoseconds(1, 0),
268+
MemoryUsage::fromBytes(100),
269+
),
270+
0
271+
));
272+
273+
$this->assertFileDoesNotExist(dirname(__DIR__, 2).'/clover-to-delete.xml');
274+
}
275+
230276
public function testFromConfigurationAndParameters(): void
231277
{
232278
$this->assertEquals(
233279
new ApplicationFinishedSubscriber(
234280
'tests/clover.xml',
235281
MinCoverageRules::fromInt(90),
236282
false,
283+
false,
237284
new Exitter(),
238285
new ConsoleOutput(new \Symfony\Component\Console\Output\ConsoleOutput()),
239286
),
@@ -254,6 +301,7 @@ public function testFromConfigurationAndParametersFromFile(): void
254301
'tests/clover.xml',
255302
MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
256303
false,
304+
false,
257305
new Exitter(),
258306
new ConsoleOutput(new \Symfony\Component\Console\Output\ConsoleOutput()),
259307
),

0 commit comments

Comments
 (0)