Setzt Phase 1 aus Visualization.md um: Vanilla-PHP-REST-API mit SQLite-Persistenz, statischen API-Keys pro Agent, Codeberg-Sync fuer Backlog/Done, PHPUnit-Tests und vollstaendiger API.md-Dokumentation - Vorgaben aus Detailantworten.md (F1-F16) umgesetzt. Entwicklung/Build/Test laufen komplett in Docker-Compose-Containern (PHP 8.2 + SQLite + Composer + PHPUnit/pcov), analog zum NC-Gantt-Projekt ist auf dem Host kein PHP/Composer noetig. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
62 lines
1.7 KiB
PHP
62 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Monitor\Api\Tests\Http;
|
|
|
|
use Monitor\Api\Http\MethodNotAllowedException;
|
|
use Monitor\Api\Http\Request;
|
|
use Monitor\Api\Http\Router;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class RouterTest extends TestCase
|
|
{
|
|
public function testMatchesStaticRoute(): void
|
|
{
|
|
$router = new Router();
|
|
$router->add('GET', '/api/v1/tickets', fn () => 'tickets');
|
|
|
|
$match = $router->match(Request::create('GET', '/api/v1/tickets'));
|
|
|
|
self::assertNotNull($match);
|
|
self::assertSame('tickets', ($match->handler)());
|
|
}
|
|
|
|
public function testExtractsPathParams(): void
|
|
{
|
|
$router = new Router();
|
|
$router->add('PATCH', '/api/v1/tickets/{id}', fn () => null);
|
|
|
|
$match = $router->match(Request::create('PATCH', '/api/v1/tickets/42'));
|
|
|
|
self::assertSame(['id' => '42'], $match->params);
|
|
}
|
|
|
|
public function testReturnsNullForUnknownPath(): void
|
|
{
|
|
$router = new Router();
|
|
$router->add('GET', '/api/v1/tickets', fn () => null);
|
|
|
|
self::assertNull($router->match(Request::create('GET', '/does/not/exist')));
|
|
}
|
|
|
|
public function testThrowsMethodNotAllowedForKnownPathWrongMethod(): void
|
|
{
|
|
$router = new Router();
|
|
$router->add('GET', '/api/v1/tickets', fn () => null);
|
|
|
|
$this->expectException(MethodNotAllowedException::class);
|
|
|
|
$router->match(Request::create('DELETE', '/api/v1/tickets'));
|
|
}
|
|
|
|
public function testCarriesAuthRequirement(): void
|
|
{
|
|
$router = new Router();
|
|
$router->add('POST', '/api/v1/tickets', fn () => null, 'agent');
|
|
|
|
$match = $router->match(Request::create('POST', '/api/v1/tickets'));
|
|
|
|
self::assertSame('agent', $match->auth);
|
|
}
|
|
}
|