move_detect/cloud/tests/RouterTest.php
2026-07-12 00:49:52 +09:00

25 lines
991 B
PHP

<?php
declare(strict_types=1);
namespace App\Tests;
use App\Http\{Router, Request, Response};
use PHPUnit\Framework\TestCase;
final class RouterTest extends TestCase
{
public function testMatchAndParams(): void {
$r = new Router();
$r->add('GET', '/events/{id}', fn(Request $req, array $p) => Response::json(['id' => $p['id']]));
$res = $r->dispatch(new Request('GET', '/events/42', [], '', [], []));
$this->assertSame(200, $res->status());
$this->assertSame(['id' => '42'], json_decode($res->body(), true));
}
public function testUnmatchedIs404(): void {
$r = new Router();
$this->assertSame(404, $r->dispatch(new Request('GET', '/nope', [], '', [], []))->status());
}
public function testMethodMismatchIs404(): void {
$r = new Router();
$r->add('POST', '/x', fn() => Response::text('ok'));
$this->assertSame(404, $r->dispatch(new Request('GET', '/x', [], '', [], []))->status());
}
}