159 lines
4.8 KiB
PHP
159 lines
4.8 KiB
PHP
<?php
|
|
// RouterTest.php
|
|
// Created: 2022-01-20
|
|
// Updated: 2023-09-07
|
|
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use Index\Routing\IRouteHandler;
|
|
use Index\Routing\Route;
|
|
use Index\Routing\RouteHandler;
|
|
use Index\Routing\RouteHandlerTrait;
|
|
use Index\Routing\Router;
|
|
|
|
/**
|
|
* @covers Router
|
|
*/
|
|
final class RouterTest extends TestCase {
|
|
public function testRouter(): void {
|
|
$router1 = new Router;
|
|
|
|
$router1->get('/', function() {
|
|
return 'get';
|
|
});
|
|
$router1->post('/', function() {
|
|
return 'post';
|
|
});
|
|
$router1->delete('/', function() {
|
|
return 'delete';
|
|
});
|
|
$router1->patch('/', function() {
|
|
return 'patch';
|
|
});
|
|
$router1->put('/', function() {
|
|
return 'put';
|
|
});
|
|
$router1->add('custom', '/', function() {
|
|
return 'wacky';
|
|
});
|
|
|
|
$this->assertEquals(['get'], $router1->resolve('GET', '/')->runAll());
|
|
$this->assertEquals(['wacky'], $router1->resolve('CUSTOM', '/')->runAll());
|
|
|
|
$router1->use('/', function() {
|
|
return 'warioware';
|
|
});
|
|
$router1->use('/deep', function() {
|
|
return 'deep';
|
|
});
|
|
|
|
$this->assertEquals(['warioware', 'post'], $router1->resolve('POST', '/')->runAll());
|
|
|
|
$router1->use('/user/:user/below', function(string $user) {
|
|
return 'warioware below ' . $user;
|
|
});
|
|
|
|
$router1->get('/user/static', function() {
|
|
return 'the static one';
|
|
});
|
|
$router1->get('/user/static/below', function() {
|
|
return 'below the static one';
|
|
});
|
|
$router1->get('/user/:user', function(string $user) {
|
|
return $user;
|
|
});
|
|
$router1->get('/user/:user/below', function(string $user) {
|
|
return 'below ' . $user;
|
|
});
|
|
|
|
$this->assertEquals(
|
|
['warioware', 'below the static one'],
|
|
$router1->resolve('GET', '/user/static/below')->runAll()
|
|
);
|
|
$this->assertEquals(
|
|
['warioware', 'warioware below flashwave', 'below flashwave'],
|
|
$router1->resolve('GET', '/user/flashwave/below')->runAll()
|
|
);
|
|
|
|
$router2 = new Router;
|
|
$router2->use('/', function() {
|
|
return 'meow';
|
|
});
|
|
$router2->get('/rules', function() {
|
|
return 'rules page';
|
|
});
|
|
$router2->get('/contact', function() {
|
|
return 'contact page';
|
|
});
|
|
$router2->get('/25252', function() {
|
|
return 'numeric test';
|
|
});
|
|
|
|
$this->assertEquals(['meow', 'rules page'], $router2->resolve('GET', '/rules')->runAll());
|
|
|
|
$router1->merge('/info', $router2);
|
|
|
|
$this->assertEquals(['warioware', 'meow', 'rules page'], $router1->resolve('GET', '/info/rules')->runAll());
|
|
|
|
$this->assertEquals(['meow', 'numeric test'], $router2->resolve('GET', '/25252')->runAll());
|
|
|
|
$router3 = new Router;
|
|
$router3->get('/static', function() {
|
|
return 'wrong';
|
|
});
|
|
$router3->get('/static/0', function() {
|
|
return 'correct';
|
|
});
|
|
$router3->get('/variable', function() {
|
|
return 'wrong';
|
|
});
|
|
$router3->get('/variable/:num', function(string $num) {
|
|
return $num === '0' ? 'correct' : 'VERY wrong';
|
|
});
|
|
|
|
$this->assertEquals('correct', $router3->resolve('GET', '/static/0')->run());
|
|
$this->assertEquals('correct', $router3->resolve('GET', '/variable/0')->run());
|
|
}
|
|
|
|
public function testAttribute(): void {
|
|
$router = new Router;
|
|
$handler = new class implements IRouteHandler {
|
|
use RouteHandlerTrait;
|
|
|
|
#[Route('GET', '/')]
|
|
public function getIndex() {
|
|
return 'index';
|
|
}
|
|
|
|
#[Route('POST', '/avatar')]
|
|
public function postAvatar() {
|
|
return 'avatar';
|
|
}
|
|
|
|
#[Route('PUT', '/static')]
|
|
public static function putStatic() {
|
|
return 'static';
|
|
}
|
|
|
|
public function hasNoAttr() {
|
|
return 'meow';
|
|
}
|
|
};
|
|
|
|
$router->register($handler);
|
|
$this->assertEquals('index', $router->resolve('GET', '/')->run());
|
|
$this->assertEquals('avatar', $router->resolve('POST', '/avatar')->run());
|
|
$this->assertEquals('static', $router->resolve('PUT', '/static')->run());
|
|
|
|
$badHandler = new class extends RouteHandler {
|
|
#[Route('GET', '/')]
|
|
#[Route('POST', '/meow')]
|
|
public function getPostBad() {
|
|
return 'this is bad';
|
|
}
|
|
};
|
|
$this->expectException(UnexpectedValueException::class);
|
|
$router->register($badHandler);
|
|
}
|
|
}
|