Skip to content

Commit f85aa4b

Browse files
authored
Merge pull request #16 from codebtech/feat/coding-standards-config
chore: Add test coverage for uncovered scenarios
2 parents 2aa2eb8 + 2043e4a commit f85aa4b

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

badges/php.svg

+3-3
Loading

composer.json

+2-1
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

+6-2
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

+36-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
use ReflectionClass;
99
use ReflectionException;
1010
use Throwable;
11+
use function file_get_contents;
1112

1213
class BadgeComposerTest extends TestCase
1314
{
1415
private BadgeComposer $badgeComposer;
1516

1617
private string $inputFile = __DIR__ . "/test-input1.xml";
1718
private string $inputFile2 = __DIR__ . "/test-input2.xml";
18-
private string $outputFile = "output.svg";
19+
private string $outputFile = __DIR__ . "/../badges/output.svg";
1920
private string $coverageName = "unit";
2021

2122
/**
@@ -97,4 +98,38 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void
9798
$this->assertEquals(83, $this->badgeComposer->getTotalCoverage());
9899
}
99100

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

0 commit comments

Comments
 (0)