Files
Woles-Framework/tests/RouterTest.php

87 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Tests;
use App\Core\Router;
/**
* Router test cases
*/
class RouterTest extends TestCase
{
private Router $router;
protected function setUp(): void
{
parent::setUp();
$this->router = new Router();
}
public function testCanRegisterGetRoute(): void
{
$this->router->get('/test', 'TestController@index');
$routes = $this->router->getRoutes();
$this->assertArrayHasKey('GET', $routes);
$this->assertCount(1, $routes['GET']);
}
public function testCanRegisterPostRoute(): void
{
$this->router->post('/test', 'TestController@store');
$routes = $this->router->getRoutes();
$this->assertArrayHasKey('POST', $routes);
$this->assertCount(1, $routes['POST']);
}
public function testCanMatchSimpleRoute(): void
{
$this->router->get('/test', 'TestController@index');
$route = $this->router->match('GET', '/test');
$this->assertNotNull($route);
$this->assertEquals('TestController@index', $route['handler']);
}
public function testCanMatchRouteWithParameters(): void
{
$this->router->get('/users/{id}', 'UserController@show');
$route = $this->router->match('GET', '/users/123');
$this->assertNotNull($route);
$this->assertEquals('UserController@show', $route['handler']);
$this->assertEquals('123', $route['params']['id']);
}
public function testReturnsNullForNonMatchingRoute(): void
{
$this->router->get('/test', 'TestController@index');
$route = $this->router->match('GET', '/nonexistent');
$this->assertNull($route);
}
public function testCanMatchMultipleRoutes(): void
{
$this->router->get('/users', 'UserController@index');
$this->router->get('/users/{id}', 'UserController@show');
$this->router->post('/users', 'UserController@store');
$indexRoute = $this->router->match('GET', '/users');
$showRoute = $this->router->match('GET', '/users/123');
$storeRoute = $this->router->match('POST', '/users');
$this->assertNotNull($indexRoute);
$this->assertNotNull($showRoute);
$this->assertNotNull($storeRoute);
$this->assertEquals('UserController@index', $indexRoute['handler']);
$this->assertEquals('UserController@show', $showRoute['handler']);
$this->assertEquals('UserController@store', $storeRoute['handler']);
}
}