Skip to content

Commit

Permalink
Merge pull request #16 from codebtech/feat/coding-standards-config
Browse files Browse the repository at this point in the history
chore: Add test coverage for uncovered scenarios
  • Loading branch information
m0hanraj committed May 31, 2024
2 parents 2aa2eb8 + 2043e4a commit f85aa4b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
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);
}

}

0 comments on commit f85aa4b

Please sign in to comment.