Skip to content

Commit

Permalink
Merge pull request #67 from gsteel/v4/duplicate-route-coverage
Browse files Browse the repository at this point in the history
Add test coverage for the duplicate route detector
  • Loading branch information
gsteel authored Jul 18, 2024
2 parents be5d911 + e9ddae5 commit ed003a2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/DuplicateRouteDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use function implode;
use function sprintf;

/** @internal */
final class DuplicateRouteDetector
{
private const ROUTE_SEARCH_ANY = 'any';
Expand All @@ -15,9 +16,9 @@ final class DuplicateRouteDetector
/**
* List of all routes indexed by name
*
* @var Route[]
* @var array<string, Route>
*/
private $routeNames = [];
private array $routeNames = [];

/**
* Search structure for duplicate path-method detection
Expand All @@ -34,7 +35,7 @@ final class DuplicateRouteDetector
*
* @var array<string, array{methods?: array<string, Route>, any?: Route}>
*/
private $routePaths = [];
private array $routePaths = [];

/**
* Determine if the route is duplicated in the current list.
Expand Down
77 changes: 77 additions & 0 deletions test/DuplicateRouteDetectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace MezzioTest\Router;

use Mezzio\Router\DuplicateRouteDetector;
use Mezzio\Router\Exception\DuplicateRouteException;
use Mezzio\Router\Route;
use MezzioTest\Router\Asset\NoOpMiddleware;
use PHPUnit\Framework\TestCase;

/** @psalm-suppress InternalClass,InternalMethod */
class DuplicateRouteDetectorTest extends TestCase
{
private DuplicateRouteDetector $detector;

protected function setUp(): void
{
$this->detector = new DuplicateRouteDetector();
}

public function test2RoutesWithTheSameNameAreExceptional(): void
{
$a = new Route('/a', new NoOpMiddleware(), Route::HTTP_METHOD_ANY, 'name');
$b = new Route('/b', new NoOpMiddleware(), Route::HTTP_METHOD_ANY, 'name');

$this->detector->detectDuplicate($a);

$this->expectException(DuplicateRouteException::class);

$this->detector->detectDuplicate($b);
}

public function testRoutesOnTheSamePathButWithDifferentMethodsAreNotDuplicates(): void
{
$a = new Route('/a', new NoOpMiddleware(), ['GET'], 'a');
$b = new Route('/a', new NoOpMiddleware(), ['POST'], 'b');
$this->detector->detectDuplicate($a);
$this->detector->detectDuplicate($b);

$this->expectNotToPerformAssertions();
}

public function testRoutesOnTheSamePathAreExceptional(): void
{
$a = new Route('/a', new NoOpMiddleware(), ['GET'], 'a');
$b = new Route('/a', new NoOpMiddleware(), ['GET'], 'b');
$this->detector->detectDuplicate($a);

$this->expectException(DuplicateRouteException::class);

$this->detector->detectDuplicate($b);
}

public function testDuplicateRoutesOnTheSamePathWithMethodIntersectionIsExceptional(): void
{
$a = new Route('/a', new NoOpMiddleware(), ['GET'], 'a');
$b = new Route('/a', new NoOpMiddleware(), Route::HTTP_METHOD_ANY, 'b');
$this->detector->detectDuplicate($a);

$this->expectException(DuplicateRouteException::class);

$this->detector->detectDuplicate($b);
}

public function testDuplicateRoutesOnTheSamePathBothWithAnyMethodAreExceptional(): void
{
$a = new Route('/a', new NoOpMiddleware(), Route::HTTP_METHOD_ANY, 'a');
$b = new Route('/a', new NoOpMiddleware(), Route::HTTP_METHOD_ANY, 'b');
$this->detector->detectDuplicate($a);

$this->expectException(DuplicateRouteException::class);

$this->detector->detectDuplicate($b);
}
}

0 comments on commit ed003a2

Please sign in to comment.