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;
|
|
|
|
|
|
|
|
|
|
class RouterBasicTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function testRouterCanBeCreatedWithValidConfig(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\Config\Config([
|
2026-02-13 21:07:59 +00:00
|
|
|
'modules_path' => ['/path/to/modules'],
|
|
|
|
|
'routes_file' => 'routes.php'
|
|
|
|
|
]);
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$router = new \Atlas\Router\Router($config);
|
2026-02-13 21:07:59 +00:00
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\Router::class, $router);
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRouterCanCreateSimpleRoute(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\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', function() {
|
|
|
|
|
return 'Hello World';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $router->getRoutes());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRouterReturnsSameInstanceForChaining(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\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
|
|
|
|
|
|
|
|
$result = $router->get('/get', 'handler')->post('/post', 'handler');
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertTrue($result instanceof \Atlas\Router\Router);
|
2026-02-13 21:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRouteHasCorrectProperties(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\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', 'test_handler', 'test_route');
|
|
|
|
|
|
|
|
|
|
$routes = $router->getRoutes();
|
|
|
|
|
$route = $routes[0] ?? null;
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $route);
|
2026-02-13 21:07:59 +00:00
|
|
|
$this->assertSame('GET', $route->getMethod());
|
|
|
|
|
$this->assertSame('/test', $route->getPath());
|
|
|
|
|
$this->assertSame('test_handler', $route->getHandler());
|
|
|
|
|
$this->assertSame('test_route', $route->getName());
|
|
|
|
|
$this->assertEmpty($route->getMiddleware());
|
|
|
|
|
$this->assertEmpty($route->getValidation());
|
|
|
|
|
$this->assertEmpty($route->getDefaults());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRouteNormalizesPath(): void
|
|
|
|
|
{
|
2026-02-13 23:26:31 +00:00
|
|
|
$config = new \Atlas\Tests\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('/api/test', 'handler');
|
|
|
|
|
|
|
|
|
|
$routes = $router->getRoutes();
|
|
|
|
|
$route = $routes[0] ?? null;
|
|
|
|
|
|
2026-02-13 23:26:31 +00:00
|
|
|
$this->assertInstanceOf(\Atlas\Router\RouteDefinition::class, $route);
|
2026-02-13 21:07:59 +00:00
|
|
|
$this->assertSame('/api/test', $route->getPath());
|
|
|
|
|
}
|
|
|
|
|
}
|