Skip to content

Commit 5e6ebd6

Browse files
ISSUE #21: Allow to use absolute path to reference clover file
1 parent 6499cd5 commit 5e6ebd6

File tree

3 files changed

+65
-23
lines changed

3 files changed

+65
-23
lines changed

Makefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
compose=docker compose
2+
3+
dc:
4+
@${compose} -f docker-compose.yml $(cmd)
5+
6+
dcr:
7+
@make dc cmd="run --rm php-cli $(cmd)"
8+
9+
stop:
10+
@make dc cmd="stop"
11+
12+
up:
13+
@make dc cmd="up -d"
14+
15+
build-containers:
16+
@make dc cmd="up -d --build"
17+
18+
down:
19+
@make dc cmd="down"
20+
21+
composer:
22+
@make dcr cmd="composer $(arg)"
23+
24+
# Code quality tools.
25+
phpunit:
26+
@make dcr cmd="vendor/bin/phpunit -d --enable-pretty-print -d --compact $(arg)"
27+
28+
phpunit-with-coverage-report:
29+
@make phpunit arg="--coverage-clover=clover.xml -d --min-coverage=min-coverage-rules.php"
30+
31+
phpstan:
32+
@make dcr cmd="vendor/bin/phpstan --memory-limit=1G $(arg)"
33+
34+
csfix:
35+
@make dcr cmd="vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php"
36+
37+
delete-snapshots:
38+
find . -name __snapshots__ -type d -prune -exec rm -rf {} \;

src/Subscriber/Application/ApplicationFinishedSubscriber.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
final class ApplicationFinishedSubscriber extends FormatterHelper implements FinishedSubscriber
2222
{
2323
public function __construct(
24-
private readonly string $relativePathToCloverXml,
24+
private readonly string $pathToCloverXml,
2525
private readonly MinCoverageRules $minCoverageRules,
2626
private readonly bool $cleanUpCloverXml,
2727
private readonly Exitter $exitter,
@@ -35,10 +35,15 @@ public function notify(Finished $event): void
3535
$this->timer->start();
3636
/** @var string $reflectionFileName */
3737
$reflectionFileName = (new \ReflectionClass(ClassLoader::class))->getFileName();
38-
$absolutePathToCloverXml = dirname($reflectionFileName, 3).'/'.$this->relativePathToCloverXml;
38+
39+
$absolutePathToCloverXml = $this->pathToCloverXml;
40+
if (!str_starts_with($this->pathToCloverXml, '/')) {
41+
// User is probably using relative path to clover.xml
42+
$absolutePathToCloverXml = dirname($reflectionFileName, 3).'/'.$this->pathToCloverXml;
43+
}
3944

4045
if (!file_exists($absolutePathToCloverXml)) {
41-
return;
46+
throw new \RuntimeException('Clover XML file not found at: '.$absolutePathToCloverXml);
4247
}
4348

4449
/** @var CoverageMetric[] $metrics */
@@ -146,7 +151,7 @@ public static function fromConfigurationAndParameters(
146151
}
147152

148153
return new self(
149-
relativePathToCloverXml: $configuration->coverageClover(),
154+
pathToCloverXml: $configuration->coverageClover(),
150155
minCoverageRules: $rules,
151156
cleanUpCloverXml: $cleanUpCloverXml,
152157
exitter: new Exitter(),

tests/Subscriber/Application/ApplicationFinishedSubscriberTest.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testNotifyWithAtLeastOneFailedRule(): void
5151
->method('exit');
5252

5353
$subscriber = new ApplicationFinishedSubscriber(
54-
relativePathToCloverXml: 'tests/clover.xml',
54+
pathToCloverXml: 'tests/clover.xml',
5555
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-failed-rule.php'),
5656
cleanUpCloverXml: false,
5757
exitter: $this->exitter,
@@ -85,7 +85,7 @@ public function testNotifyWithAWarning(): void
8585
->method('exit');
8686

8787
$subscriber = new ApplicationFinishedSubscriber(
88-
relativePathToCloverXml: 'tests/clover.xml',
88+
pathToCloverXml: 'tests/clover.xml',
8989
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-warning.php'),
9090
cleanUpCloverXml: false,
9191
exitter: $this->exitter,
@@ -119,7 +119,7 @@ public function testNotifyWhenCoverageIsOk(): void
119119
->method('exit');
120120

121121
$subscriber = new ApplicationFinishedSubscriber(
122-
relativePathToCloverXml: 'tests/clover.xml',
122+
pathToCloverXml: 'tests/clover.xml',
123123
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
124124
cleanUpCloverXml: false,
125125
exitter: $this->exitter,
@@ -153,7 +153,7 @@ public function testNotifyWithOnlyTotal(): void
153153
->method('exit');
154154

155155
$subscriber = new ApplicationFinishedSubscriber(
156-
relativePathToCloverXml: 'tests/clover.xml',
156+
pathToCloverXml: 'tests/clover.xml',
157157
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-total-only.php'),
158158
cleanUpCloverXml: false,
159159
exitter: $this->exitter,
@@ -187,7 +187,7 @@ public function testNotifyWithoutTotal(): void
187187
->method('exit');
188188

189189
$subscriber = new ApplicationFinishedSubscriber(
190-
relativePathToCloverXml: 'tests/clover.xml',
190+
pathToCloverXml: 'tests/clover.xml',
191191
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-without-total.php'),
192192
cleanUpCloverXml: false,
193193
exitter: $this->exitter,
@@ -221,7 +221,7 @@ public function testNotifyWithRulesThatDoNotExit(): void
221221
->method('exit');
222222

223223
$subscriber = new ApplicationFinishedSubscriber(
224-
relativePathToCloverXml: 'tests/clover.xml',
224+
pathToCloverXml: 'tests/clover.xml',
225225
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-no-exit.php'),
226226
cleanUpCloverXml: false,
227227
exitter: $this->exitter,
@@ -255,7 +255,7 @@ public function testDivideByZero(): void
255255
->method('exit');
256256

257257
$subscriber = new ApplicationFinishedSubscriber(
258-
relativePathToCloverXml: 'tests/clover-test-divide-by-zero.xml',
258+
pathToCloverXml: 'tests/clover-test-divide-by-zero.xml',
259259
minCoverageRules: MinCoverageRules::fromInt(100, true),
260260
cleanUpCloverXml: false,
261261
exitter: $this->exitter,
@@ -289,7 +289,7 @@ public function testNotifyWhenNoTrackedLines(): void
289289
->method('exit');
290290

291291
$subscriber = new ApplicationFinishedSubscriber(
292-
relativePathToCloverXml: 'tests/clover-with-no-tracked-lines.xml',
292+
pathToCloverXml: 'tests/clover-with-no-tracked-lines.xml',
293293
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-no-tracked-lines.php'),
294294
cleanUpCloverXml: false,
295295
exitter: $this->exitter,
@@ -323,14 +323,15 @@ public function testNotifyWithNonExistingCloverFile(): void
323323
->method('exit');
324324

325325
$subscriber = new ApplicationFinishedSubscriber(
326-
relativePathToCloverXml: 'tests/clover-wrong.xml',
326+
pathToCloverXml: 'tests/clover-wrong.xml',
327327
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
328328
cleanUpCloverXml: false,
329329
exitter: $this->exitter,
330330
consoleOutput: new ConsoleOutput($this->output, $this->resourceUsageFormatter),
331331
timer: $this->timer,
332332
);
333333

334+
$this->expectExceptionObject(new \RuntimeException('Clover XML file not found at: /var/www/tests/clover-wrong.xml'));
334335
$subscriber->notify(event: new Finished(
335336
new Info(
336337
current: new Snapshot(
@@ -346,8 +347,6 @@ public function testNotifyWithNonExistingCloverFile(): void
346347
),
347348
0
348349
));
349-
350-
$this->assertEmpty((string) $this->output);
351350
}
352351

353352
public function testNotifyWithInvalidCloverFile(): void
@@ -357,7 +356,7 @@ public function testNotifyWithInvalidCloverFile(): void
357356
->method('exit');
358357

359358
$subscriber = new ApplicationFinishedSubscriber(
360-
relativePathToCloverXml: 'tests/clover-invalid.xml',
359+
pathToCloverXml: 'tests/clover-invalid.xml',
361360
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
362361
cleanUpCloverXml: false,
363362
exitter: $this->exitter,
@@ -394,7 +393,7 @@ public function testNotifyWithCleanUpCloverFile(): void
394393
->method('exit');
395394

396395
$subscriber = new ApplicationFinishedSubscriber(
397-
relativePathToCloverXml: 'tests/clover-to-delete.xml',
396+
pathToCloverXml: 'tests/clover-to-delete.xml',
398397
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-failed-rule.php'),
399398
cleanUpCloverXml: true,
400399
exitter: $this->exitter,
@@ -431,7 +430,7 @@ public function testNotifyWithDuplicatePatterns(): void
431430
$this->expectExceptionMessage('Make sure all coverage rule patterns are unique');
432431

433432
new ApplicationFinishedSubscriber(
434-
relativePathToCloverXml: 'tests/clover.xml',
433+
pathToCloverXml: 'tests/clover.xml',
435434
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-with-duplicates.php'),
436435
cleanUpCloverXml: false,
437436
exitter: $this->exitter,
@@ -450,7 +449,7 @@ public function testNotifyWithInvalidRules(): void
450449
$this->expectExceptionMessage('MinCoverage has to be value between 0 and 100. 203 given');
451450

452451
new ApplicationFinishedSubscriber(
453-
relativePathToCloverXml: 'tests/clover.xml',
452+
pathToCloverXml: 'tests/clover.xml',
454453
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-invalid.php'),
455454
cleanUpCloverXml: false,
456455
exitter: $this->exitter,
@@ -469,7 +468,7 @@ public function testNotifyWithInvalidRuleInstances(): void
469468
$this->expectExceptionMessage('Make sure all coverage rules are of instance RobinIngelbrecht\PHPUnitCoverageTools\MinCoverage\MinCoverageRule');
470469

471470
new ApplicationFinishedSubscriber(
472-
relativePathToCloverXml: 'tests/clover.xml',
471+
pathToCloverXml: 'tests/clover.xml',
473472
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-invalid-rule-instances.php'),
474473
cleanUpCloverXml: false,
475474
exitter: $this->exitter,
@@ -482,7 +481,7 @@ public function testFromConfigurationAndParameters(): void
482481
{
483482
$this->assertEquals(
484483
new ApplicationFinishedSubscriber(
485-
relativePathToCloverXml: 'tests/clover.xml',
484+
pathToCloverXml: 'tests/clover.xml',
486485
minCoverageRules: MinCoverageRules::fromInt(90, false),
487486
cleanUpCloverXml: true,
488487
exitter: new Exitter(),
@@ -503,7 +502,7 @@ public function testFromConfigurationAndParameters2(): void
503502
{
504503
$this->assertEquals(
505504
new ApplicationFinishedSubscriber(
506-
relativePathToCloverXml: 'tests/clover.xml',
505+
pathToCloverXml: 'tests/clover.xml',
507506
minCoverageRules: MinCoverageRules::fromInt(90, true),
508507
cleanUpCloverXml: true,
509508
exitter: new Exitter(),
@@ -526,7 +525,7 @@ public function testFromConfigurationAndParametersFromFile(): void
526525
{
527526
$this->assertEquals(
528527
expected: new ApplicationFinishedSubscriber(
529-
relativePathToCloverXml: 'tests/clover.xml',
528+
pathToCloverXml: 'tests/clover.xml',
530529
minCoverageRules: MinCoverageRules::fromConfigFile('tests/Subscriber/Application/min-coverage-rules-success.php'),
531530
cleanUpCloverXml: false,
532531
exitter: new Exitter(),

0 commit comments

Comments
 (0)