2026-02-13 21:07:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Atlas\Tests\Unit;
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
use Atlas\Router\Router;
|
2026-02-13 21:07:59 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
|
|
|
|
|
|
|
|
final class RouteMatcherTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function testReturnsRouteOnSuccessfulMatch(): void
|
|
|
|
|
{
|
2026-02-14 23:28:28 +00:00
|
|
|
$config = new \Atlas\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
|
|
|
|
$router->get('/hello', 'HelloWorldHandler');
|
|
|
|
|
|
|
|
|
|
$uri = $this->createMock(UriInterface::class);
|
|
|
|
|
$uri->method('getPath')->willReturn('/hello');
|
|
|
|
|
$uri->method('getScheme')->willReturn('http');
|
|
|
|
|
$uri->method('getHost')->willReturn('localhost');
|
|
|
|
|
$uri->method('getPort')->willReturn(80);
|
|
|
|
|
|
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
|
$request->method('getMethod')->willReturn('GET');
|
|
|
|
|
$request->method('getUri')->willReturn($uri);
|
|
|
|
|
|
|
|
|
|
$matchedRoute = $router->match($request);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $matchedRoute);
|
2026-02-13 21:07:59 +00:00
|
|
|
$this->assertSame('/hello', $matchedRoute->getPath());
|
|
|
|
|
$this->assertSame('HelloWorldHandler', $matchedRoute->getHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testReturnsNullOnNoMatch(): void
|
|
|
|
|
{
|
2026-02-14 23:28:28 +00:00
|
|
|
$config = new \Atlas\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
|
|
|
|
$router->get('/test', 'Handler');
|
|
|
|
|
|
|
|
|
|
$uri = $this->createMock(UriInterface::class);
|
|
|
|
|
$uri->method('getPath')->willReturn('/other');
|
|
|
|
|
$uri->method('getScheme')->willReturn('http');
|
|
|
|
|
$uri->method('getHost')->willReturn('localhost');
|
|
|
|
|
$uri->method('getPort')->willReturn(80);
|
|
|
|
|
|
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
|
$request->method('getMethod')->willReturn('GET');
|
|
|
|
|
$request->method('getUri')->willReturn($uri);
|
|
|
|
|
|
|
|
|
|
$matchedRoute = $router->match($request);
|
|
|
|
|
|
|
|
|
|
$this->assertNull($matchedRoute);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
public function testCaseInsensitiveHttpMethodMatching(): void
|
2026-02-13 21:07:59 +00:00
|
|
|
{
|
2026-02-14 23:28:28 +00:00
|
|
|
$config = new \Atlas\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
|
|
|
|
$router->get('/test', 'Handler');
|
|
|
|
|
|
|
|
|
|
$uri = $this->createMock(UriInterface::class);
|
|
|
|
|
$uri->method('getPath')->willReturn('/test');
|
|
|
|
|
$uri->method('getScheme')->willReturn('http');
|
|
|
|
|
$uri->method('getHost')->willReturn('localhost');
|
|
|
|
|
$uri->method('getPort')->willReturn(80);
|
|
|
|
|
|
|
|
|
|
$request = $this->createMock(ServerRequestInterface::class);
|
|
|
|
|
$request->method('getMethod')->willReturn('get');
|
|
|
|
|
$request->method('getUri')->willReturn($uri);
|
|
|
|
|
|
|
|
|
|
$matchedRoute = $router->match($request);
|
|
|
|
|
|
|
|
|
|
$this->assertNotNull($matchedRoute);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRouteCollectionIteratesCorrectly(): void
|
|
|
|
|
{
|
2026-02-14 23:28:28 +00:00
|
|
|
$config = new \Atlas\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
|
|
|
|
$router->get('/route1', 'Handler1');
|
|
|
|
|
$router->post('/route2', 'Handler2');
|
|
|
|
|
|
|
|
|
|
$routes = $router->getRoutes();
|
|
|
|
|
$this->assertIsIterable($routes);
|
|
|
|
|
|
|
|
|
|
$routeArray = iterator_to_array($routes);
|
|
|
|
|
$this->assertCount(2, $routeArray);
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $routeArray[0]);
|
|
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $routeArray[1]);
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUrlGenerationWithNamedRoute(): void
|
|
|
|
|
{
|
2026-02-14 23:28:28 +00:00
|
|
|
$config = new \Atlas\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
|
|
|
|
$router->get('/users', 'UserListHandler', 'user_list');
|
|
|
|
|
|
|
|
|
|
$url = $router->url('user_list');
|
2026-02-13 23:26:31 +00:00
|
|
|
|
2026-02-13 21:07:59 +00:00
|
|
|
$this->assertSame('/users', $url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHttpMethodsReturnSameInstanceForChaining(): void
|
|
|
|
|
{
|
2026-02-14 23:28:28 +00:00
|
|
|
$config = new \Atlas\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
2026-02-14 23:28:28 +00:00
|
|
|
$router->get('/get', 'Handler');
|
|
|
|
|
$router->post('/post', 'Handler');
|
|
|
|
|
$router->put('/put', 'Handler');
|
|
|
|
|
$router->patch('/patch', 'Handler');
|
|
|
|
|
$router->delete('/delete', 'Handler');
|
2026-02-13 21:07:59 +00:00
|
|
|
|
2026-02-14 23:28:28 +00:00
|
|
|
$this->assertCount(5, $router->getRoutes());
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
}
|