Skip to content

chore: Add test coverage for uncovered scenarios #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions badges/php.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"scripts": {
"lint": "phpcs",
"lint:fix": "phpcbf",
"test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/"
"test": "phpunit --testdox --coverage-clover coverage/clover.xml --coverage-html coverage --coverage-filter src/",
"php:badge": "bin/coverage-badge coverage/clover.xml badges/php.svg PHP"
},
"config": {
"allow-plugins": {
Expand Down
8 changes: 6 additions & 2 deletions src/BadgeComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ public function run(): void
private function processFile(string $inputFile): void
{
try {
$xml = new SimpleXMLElement(file_get_contents($inputFile));
$content = file_get_contents($inputFile);
if ($content === false) {
throw new Exception('Error reading file: ' . $inputFile);
}
$xml = new SimpleXMLElement($content);
$metrics = $xml->xpath('//metrics');
foreach ($metrics as $metric) {
$this->totalElements += (int) $metric['elements'];
Expand All @@ -112,7 +116,7 @@ private function processFile(string $inputFile): void
$this->totalCoverage += (int) round($coverageRatio * 100);

} catch (Throwable $e) {
throw new Exception('Error processing file: ' . $e->getMessage());
throw new Exception($e->getMessage());
}
}

Expand Down
37 changes: 36 additions & 1 deletion tests/BadgeComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
use ReflectionClass;
use ReflectionException;
use Throwable;
use function file_get_contents;

class BadgeComposerTest extends TestCase
{
private BadgeComposer $badgeComposer;

private string $inputFile = __DIR__ . "/test-input1.xml";
private string $inputFile2 = __DIR__ . "/test-input2.xml";
private string $outputFile = "output.svg";
private string $outputFile = __DIR__ . "/../badges/output.svg";
private string $coverageName = "unit";

/**
Expand Down Expand Up @@ -97,4 +98,38 @@ public function testProcessMultipleCloverFilesAndCalculateTheCoverage(): void
$this->assertEquals(83, $this->badgeComposer->getTotalCoverage());
}

/**
* @throws ReflectionException
*/
public function testItThrowsExceptionWhenFileProcessingFails(): void
{
$this->expectException(Throwable::class);
$this->expectExceptionMessage('Error reading file: invalid_file.xml');

$this->processFile('invalid_file.xml');
}

/**
* @throws ReflectionException
*/
public function finalizeCoverage()
{
$method = (new ReflectionClass($this->badgeComposer))->getMethod('finalizeCoverage');

return $method->invoke($this->badgeComposer);
}

/**
* @throws ReflectionException
*/
public function testFinalizeCoverageCalculatesAverageCoverage(): void
{
$this->finalizeCoverage();

$outputFileContent = file_get_contents(__DIR__ . "/../badges/output.svg");
$this->assertStringContainsString('#e05d44', $outputFileContent);

$this->assertStringContainsString('unit', $outputFileContent);
}

}