Skip to content

Commit

Permalink
Coverage Threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferror committed Jan 3, 2024
1 parent 0995e91 commit 978e928
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ return [
php bin/console ferror:check-openapi-coverage
```

> You can also specify the --threshold or (--t) option to define coverage level below which the command will fail.
```bash
php bin/console ferror:check-openapi-coverage --threshold 0.70
```

## Example Result

```
Expand Down
15 changes: 15 additions & 0 deletions src/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@

namespace Ferror\OpenapiCoverage;

use InvalidArgumentException;

final readonly class Coverage
{
public function __construct(public float $value)
{
if ($value < 0) {
throw new InvalidArgumentException('Coverage value cannot be negative number');
}
}

public function asPercentage(): float
{
return round($this->value * 100, 2);
}

public function isGreater(self $self): bool
{
return $this->value > $self->value;
}

public function isLower(self $self): bool
{
return $this->value < $self->value;
}
}
12 changes: 11 additions & 1 deletion src/Symfony/Console/CheckCoverageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ferror\OpenapiCoverage\Symfony\Console;

use Ferror\OpenapiCoverage\Coverage;
use Ferror\OpenapiCoverage\RouteCollection;
use Ferror\OpenapiCoverage\CoverageCalculator;
use Ferror\OpenapiCoverage\Route;
Expand All @@ -12,6 +13,7 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\RouterInterface;

Expand All @@ -25,6 +27,7 @@ public function __construct(
private readonly string $prefix = '/v1',
) {
parent::__construct('ferror:check-openapi-coverage');
$this->addOption('threshold', 't', InputOption::VALUE_OPTIONAL);
}

public function execute(InputInterface $input, OutputInterface $output): int
Expand Down Expand Up @@ -78,8 +81,9 @@ public function execute(InputInterface $input, OutputInterface $output): int
}

$coverageCalculator = new CoverageCalculator($paths->count(), $openApiPaths->count() - $notExistingPaths->count());
$coverage = $coverageCalculator->calculate();

$output->writeln('Open API coverage: ' . $coverageCalculator->calculate()->asPercentage() . '%');
$output->writeln('Open API coverage: ' . $coverage->asPercentage() . '%');

if ($missingPaths->count() === 0) {
$output->writeln('OpenAPI schema covers all Symfony routes. Good job!');
Expand All @@ -95,6 +99,12 @@ public function execute(InputInterface $input, OutputInterface $output): int
$table->render();
}

if ($threshold = $input->getOption('threshold')) {
if ($coverage->isLower(new Coverage($threshold))) {
return Command::FAILURE;
}
}

return Command::SUCCESS;
}
}
25 changes: 25 additions & 0 deletions tests/Integration/CheckCoverageCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;

class CheckCoverageCommandTest extends KernelTestCase
Expand Down Expand Up @@ -35,4 +36,28 @@ public function testExecuteClass(): void

$this->assertEquals($expectedDisplay, $display);
}

public function testPositiveThreshold(): void
{
$kernel = self::bootKernel();
$application = new Application($kernel);

$command = $application->find('ferror:check-openapi-coverage');
$commandTester = new CommandTester($command);
$commandTester->execute(['--threshold' => 0.70]);

$commandTester->assertCommandIsSuccessful();
}

public function testNegativeThreshold(): void
{
$kernel = self::bootKernel();
$application = new Application($kernel);

$command = $application->find('ferror:check-openapi-coverage');
$commandTester = new CommandTester($command);
$commandTester->execute(['--threshold' => 0.80]);

$this->assertEquals(Command::FAILURE, $commandTester->getStatusCode());
}
}
43 changes: 43 additions & 0 deletions tests/Unit/CoverageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Ferror\OpenapiCoverage\Unit;

use Ferror\OpenapiCoverage\Coverage;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

final class CoverageTest extends TestCase
{
public function testThrowsException(): void
{
$this->expectException(InvalidArgumentException::class);

new Coverage(-1);
}

public function testIsGreater(): void
{
$coverage = new Coverage(50.0);

$this->assertTrue($coverage->isGreater(new Coverage(40.0)));
$this->assertTrue($coverage->isGreater(new Coverage(10.0)));

$this->assertFalse($coverage->isGreater(new Coverage(50.0)));
$this->assertFalse($coverage->isGreater(new Coverage(60.0)));
$this->assertFalse($coverage->isGreater(new Coverage(100.0)));
}

public function testIsLower(): void
{
$coverage = new Coverage(50.0);

$this->assertFalse($coverage->isLower(new Coverage(40.0)));
$this->assertFalse($coverage->isLower(new Coverage(10.0)));
$this->assertFalse($coverage->isLower(new Coverage(50.0)));

$this->assertTrue($coverage->isLower(new Coverage(60.0)));
$this->assertTrue($coverage->isLower(new Coverage(100.0)));
}
}

0 comments on commit 978e928

Please sign in to comment.