Skip to content

Commit 743f033

Browse files
committed
Update PHP badge color and coverage percentage, improve file handling
1 parent c9ecc66 commit 743f033

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

badges/php.svg

Lines changed: 3 additions & 3 deletions
Loading

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"scripts": {
3232
"lint": "phpcs",
3333
"lint:fix": "phpcbf",
34-
"test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/"
34+
"test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/",
35+
"php:badge": "bin/coverage-badge coverage/clover.xml badges/php.svg PHP"
3536
},
3637
"config": {
3738
"allow-plugins": {

src/BadgeComposer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ public function run(): void
101101
private function processFile(string $inputFile): void
102102
{
103103
try {
104-
$xml = new SimpleXMLElement(file_get_contents($inputFile));
104+
$content = file_get_contents($inputFile);
105+
if ($content === false) {
106+
throw new Exception('Error reading file: ' . $inputFile);
107+
}
108+
$xml = new SimpleXMLElement($content);
105109
$metrics = $xml->xpath('//metrics');
106110
foreach ($metrics as $metric) {
107111
$this->totalElements += (int) $metric['elements'];
@@ -112,7 +116,7 @@ private function processFile(string $inputFile): void
112116
$this->totalCoverage += (int) round($coverageRatio * 100);
113117

114118
} catch (Throwable $e) {
115-
throw new Exception('Error processing file: ' . $e->getMessage());
119+
throw new Exception($e->getMessage());
116120
}
117121
}
118122

tests/BadgeComposerTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BadgeComposerTest extends TestCase
1515

1616
private string $inputFile = __DIR__ . "/test-input1.xml";
1717
private string $inputFile2 = __DIR__ . "/test-input2.xml";
18-
private string $outputFile = "output.svg";
18+
private string $outputFile = __DIR__ . "/../badges/output.svg";
1919
private string $coverageName = "unit";
2020

2121
/**
@@ -97,4 +97,38 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void
9797
$this->assertEquals(83, $this->badgeComposer->getTotalCoverage());
9898
}
9999

100+
/**
101+
* @throws ReflectionException
102+
*/
103+
public function testItThrowsExceptionWhenFileProcessingFails(): void
104+
{
105+
$this->expectException(Throwable::class);
106+
$this->expectExceptionMessage('Error reading file: invalid_file.xml');
107+
108+
$this->processFile('invalid_file.xml');
109+
}
110+
111+
/**
112+
* @throws ReflectionException
113+
*/
114+
public function finalizeCoverage()
115+
{
116+
$method = (new ReflectionClass($this->badgeComposer))->getMethod('finalizeCoverage');
117+
118+
return $method->invoke($this->badgeComposer);
119+
}
120+
121+
/**
122+
* @throws ReflectionException
123+
*/
124+
public function testFinalizeCoverageCalculatesAverageCoverage(): void
125+
{
126+
$this->finalizeCoverage();
127+
128+
$outputFileContent = file_get_contents(__DIR__ . "/../badges/output.svg");
129+
$this->assertStringContainsString('#e05d44', $outputFileContent);
130+
131+
$this->assertStringContainsString('unit', $outputFileContent);
132+
}
133+
100134
}

0 commit comments

Comments
 (0)