34 lines
1.2 KiB
PHP
34 lines
1.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Phred\Tests;
|
|
|
|
use FastRoute\Dispatcher;
|
|
use FastRoute\RouteCollector;
|
|
use Nyholm\Psr7\Factory\Psr17Factory;
|
|
use Nyholm\Psr7Server\ServerRequestCreator;
|
|
use PHPUnit\Framework\TestCase;
|
|
use Phred\Http\Router;
|
|
use function FastRoute\simpleDispatcher;
|
|
|
|
final class RouterGroupTest extends TestCase
|
|
{
|
|
public function testGroupPrefixesRoutes(): void
|
|
{
|
|
$dispatcher = simpleDispatcher(function (RouteCollector $rc): void {
|
|
$router = new Router($rc);
|
|
$router->group('/api', function (Router $r): void {
|
|
$r->get('/health', [\Phred\Http\Controllers\HealthController::class, '__invoke']);
|
|
});
|
|
});
|
|
|
|
$psr17 = new Psr17Factory();
|
|
$creator = new ServerRequestCreator($psr17, $psr17, $psr17, $psr17);
|
|
$req = $creator->fromGlobals()->withMethod('GET')->withUri($psr17->createUri('/api/health'));
|
|
|
|
$routeInfo = $dispatcher->dispatch($req->getMethod(), $req->getUri()->getPath());
|
|
$this->assertSame(Dispatcher::FOUND, $routeInfo[0] ?? null, 'Route in group should be found with prefixed path');
|
|
$this->assertIsArray($routeInfo[1] ?? null);
|
|
}
|
|
}
|