-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ferror\OpenapiCoverage; | ||
|
||
final readonly class Collection | ||
{ | ||
public static function create(array $items): self | ||
{ | ||
return new self($items); | ||
} | ||
|
||
public function __construct(public array $items) | ||
{ | ||
} | ||
|
||
public function filter(callable $callable): self | ||
{ | ||
return new self(array_filter($this->items, $callable)); | ||
} | ||
|
||
public function map(callable $callable): self | ||
{ | ||
return new self(array_map($callable, $this->items)); | ||
} | ||
|
||
public function diff(array $items): self | ||
{ | ||
return new self(array_values(array_diff($this->items, $items))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ferror\OpenapiCoverage; | ||
|
||
final readonly class Coverage | ||
{ | ||
public function __construct(public float $value) | ||
{ | ||
} | ||
|
||
public function asPercentage(): float | ||
{ | ||
return round($this->value * 100, 2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ferror\OpenapiCoverage; | ||
|
||
use InvalidArgumentException; | ||
|
||
final readonly class CoverageCalculator | ||
{ | ||
public function __construct( | ||
private int $numberOfPaths, | ||
private int $numberOfDocumentedPaths, | ||
) { | ||
if ($numberOfPaths < 0 || $numberOfDocumentedPaths < 0) { | ||
throw new InvalidArgumentException(); | ||
} | ||
} | ||
|
||
public function calculate(): Coverage | ||
{ | ||
if ($this->numberOfPaths <= 0) { | ||
return new Coverage(0.0); | ||
} | ||
|
||
return new Coverage($this->numberOfDocumentedPaths / $this->numberOfPaths); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ferror\OpenapiCoverage; | ||
|
||
final readonly class Route | ||
{ | ||
public function __construct( | ||
public string $path, | ||
public string $method, | ||
) { | ||
} | ||
|
||
public function equals(self $self): bool | ||
{ | ||
return $this->path === $self->path && $this->method && $self->method; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
rest_product_get: | ||
path: /products | ||
path: /v1/products | ||
methods: GET | ||
|
||
rest_product_post: | ||
path: /products | ||
path: /v1/products | ||
methods: POST | ||
|
||
rest_product_delete: | ||
path: /products/:id | ||
path: /v1/products/:id | ||
methods: DELETE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ferror\OpenapiCoverage\Unit; | ||
|
||
use Ferror\OpenapiCoverage\Collection; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class CollectionTest extends TestCase | ||
{ | ||
public function testFilter(): void | ||
{ | ||
$collection = new Collection(['item', 'not-item']); | ||
|
||
$collection = $collection->filter(fn (string $item) => $item === 'item'); | ||
|
||
$this->assertEquals(['item'], $collection->items); | ||
} | ||
|
||
public function testDiff(): void | ||
{ | ||
$collection = new Collection(['item-1', 'item-2']); | ||
|
||
$collection = $collection->diff(['item-1']); | ||
|
||
$this->assertEquals(['item-2'], $collection->items); | ||
} | ||
|
||
public function testMap(): void | ||
{ | ||
$collection = new Collection(['item', 'item']); | ||
|
||
$collection = $collection->map(fn (string $item) => $item . '-not'); | ||
|
||
$this->assertEquals(['item-not', 'item-not'], $collection->items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ferror\OpenapiCoverage\Unit; | ||
|
||
use Ferror\OpenapiCoverage\Coverage; | ||
use Ferror\OpenapiCoverage\CoverageCalculator; | ||
use InvalidArgumentException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class CoverageCalculatorTest extends TestCase | ||
{ | ||
public function testCalculate(): void | ||
{ | ||
$calculator = new CoverageCalculator(1, 1); | ||
|
||
$this->assertEquals(new Coverage(1), $calculator->calculate()); | ||
} | ||
|
||
public function testThrowsExceptionOnNegativePaths(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
new CoverageCalculator(-1, 1); | ||
} | ||
|
||
public function testThrowsExceptionOnNegativeDocumentedPaths(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
new CoverageCalculator(1, -1); | ||
} | ||
} |