forked from PHP/Router
Unit tests
This commit is contained in:
parent
c4759ee09e
commit
a90b93b33f
|
@ -15,5 +15,8 @@
|
||||||
"email": "email@sharkk.net"
|
"email": "email@sharkk.net"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"minimum-stability": "stable"
|
"minimum-stability": "stable",
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^11.5"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
84
tests/SegmentRouterTest.php
Normal file
84
tests/SegmentRouterTest.php
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Sharkk\Router\SegmentRouter;
|
||||||
|
|
||||||
|
final class SegmentRouterTest extends TestCase
|
||||||
|
{
|
||||||
|
private SegmentRouter $router;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->router = new SegmentRouter();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddAndLookupRoute(): void
|
||||||
|
{
|
||||||
|
$routes = [];
|
||||||
|
SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) {
|
||||||
|
return "Blog post $id";
|
||||||
|
});
|
||||||
|
|
||||||
|
$result = SegmentRouter::lookup($routes, 'GET', '/blog/123');
|
||||||
|
$this->assertEquals(200, $result['code']);
|
||||||
|
$this->assertIsCallable($result['handler']);
|
||||||
|
$this->assertEquals(['123'], $result['params']);
|
||||||
|
$this->assertEquals("Blog post 123", $result['handler'](...$result['params']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testLookupNotFound(): void
|
||||||
|
{
|
||||||
|
$routes = [];
|
||||||
|
SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) {
|
||||||
|
return "Blog post $id";
|
||||||
|
});
|
||||||
|
|
||||||
|
$result = SegmentRouter::lookup($routes, 'GET', '/nonexistent');
|
||||||
|
$this->assertEquals(404, $result['code']);
|
||||||
|
$this->assertNull($result['handler']);
|
||||||
|
$this->assertEmpty($result['params']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMethodNotAllowed(): void
|
||||||
|
{
|
||||||
|
$routes = [];
|
||||||
|
SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) {
|
||||||
|
return "Blog post $id";
|
||||||
|
});
|
||||||
|
|
||||||
|
$result = SegmentRouter::lookup($routes, 'POST', '/blog/123');
|
||||||
|
$this->assertEquals(405, $result['code']);
|
||||||
|
$this->assertNull($result['handler']);
|
||||||
|
$this->assertEmpty($result['params']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testClearRoutes(): void
|
||||||
|
{
|
||||||
|
$routes = [];
|
||||||
|
SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) {
|
||||||
|
return "Blog post $id";
|
||||||
|
});
|
||||||
|
|
||||||
|
SegmentRouter::clear($routes);
|
||||||
|
$this->assertEmpty($routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAddMultipleMethods(): void
|
||||||
|
{
|
||||||
|
$routes = [];
|
||||||
|
SegmentRouter::add($routes, 'GET', '/blog/:id', function($id) {
|
||||||
|
return "GET Blog post $id";
|
||||||
|
});
|
||||||
|
SegmentRouter::add($routes, 'POST', '/blog/:id', function($id) {
|
||||||
|
return "POST Blog post $id";
|
||||||
|
});
|
||||||
|
|
||||||
|
$resultGet = SegmentRouter::lookup($routes, 'GET', '/blog/123');
|
||||||
|
$this->assertEquals(200, $resultGet['code']);
|
||||||
|
$this->assertEquals("GET Blog post 123", $resultGet['handler'](...$resultGet['params']));
|
||||||
|
|
||||||
|
$resultPost = SegmentRouter::lookup($routes, 'POST', '/blog/123');
|
||||||
|
$this->assertEquals(200, $resultPost['code']);
|
||||||
|
$this->assertEquals("POST Blog post 123", $resultPost['handler'](...$resultPost['params']));
|
||||||
|
}
|
||||||
|
}
|
|
@ -70,7 +70,11 @@ $testRoutes = [
|
||||||
}],
|
}],
|
||||||
];
|
];
|
||||||
|
|
||||||
array_walk($testRoutes, fn($route) => SegmentRouter::add($routes, $route[0], $route[1], $route[2]));
|
$routes = array_map(function($route) use (&$routes) {
|
||||||
|
[$method, $path, $handler] = $route;
|
||||||
|
SegmentRouter::add($routes, $method, $path, $handler);
|
||||||
|
return [$method, $path, $handler];
|
||||||
|
}, $testRoutes);
|
||||||
|
|
||||||
|
|
||||||
for ($i = 0; $i < 10; $i++) {
|
for ($i = 0; $i < 10; $i++) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user