2026-02-13 21:54:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Atlas\Tests\Unit;
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
use Atlas\Router\Router;
|
2026-02-13 21:54:36 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
use Psr\Http\Message\UriInterface;
|
|
|
|
|
|
|
|
|
|
final class ErrorHandlingTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function testMatchOrFailThrowsExceptionWhenNoRouteFound(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +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:54:36 +00:00
|
|
|
|
|
|
|
|
$uri = $this->createMock(UriInterface::class);
|
|
|
|
|
$uri->method('getPath')->willReturn('/nonexistent');
|
|
|
|
|
$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);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->expectException(\Atlas\Exception\NotFoundRouteException::class);
|
2026-02-13 21:54:36 +00:00
|
|
|
|
|
|
|
|
$router->matchOrFail($request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMatchReturnsNullWhenNoRouteFound(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +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:54:36 +00:00
|
|
|
|
|
|
|
|
$uri = $this->createMock(UriInterface::class);
|
|
|
|
|
$uri->method('getPath')->willReturn('/nonexistent');
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
|
|
$result = $router->match($request);
|
|
|
|
|
|
|
|
|
|
$this->assertNull($result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRouteChainingWithDifferentHttpMethods(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +00:00
|
|
|
'modules_path' => ['/path/to/modules']
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$result = new \Atlas\Router\Router($config)->get('/test', 'GetHandler')->post('/test', 'PostHandler');
|
2026-02-13 21:54:36 +00:00
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertTrue($result instanceof \Atlas\Router\Router);
|
|
|
|
|
$this->assertCount(2, $result->getRoutes());
|
2026-02-13 21:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMatchUsingRouteDefinition(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +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:54:36 +00:00
|
|
|
|
|
|
|
|
$router->get('/test', 'TestMethod');
|
|
|
|
|
|
|
|
|
|
$routes = $router->getRoutes();
|
|
|
|
|
$this->assertCount(1, $routes);
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $routes[0]);
|
2026-02-13 21:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCaseInsensitiveHttpMethodMatching(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +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:54:36 +00:00
|
|
|
|
|
|
|
|
$router->get('/test', 'TestHandler');
|
|
|
|
|
|
|
|
|
|
$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 testPathNormalizationLeadingSlashes(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +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:54:36 +00:00
|
|
|
|
|
|
|
|
$router->get('/test', 'TestHandler');
|
|
|
|
|
|
|
|
|
|
$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 testMatchOrFailThrowsExceptionForMultipleRoutes(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:54:36 +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:54:36 +00:00
|
|
|
|
|
|
|
|
$router->get('/test', 'TestHandler');
|
|
|
|
|
|
|
|
|
|
$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->matchOrFail($request);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $matchedRoute);
|
2026-02-13 21:54:36 +00:00
|
|
|
}
|
|
|
|
|
}
|