monitor/api/tests/Unit/ApplicationTest.php
fds2610-bot 857fc88834 Phase 1 der Neuausrichtung: REST-API fuer Tickets/Events/Systeme/KPIs/Agenten
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>
2026-07-27 19:38:52 +02:00

95 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Monitor\Api\Tests;
use Monitor\Api\Application;
use Monitor\Api\Config;
use Monitor\Api\Db\Database;
use Monitor\Api\Http\Request;
use PHPUnit\Framework\TestCase;
final class ApplicationTest extends TestCase
{
private Application $app;
protected function setUp(): void
{
$config = new Config(['MONITOR_ADMIN_API_KEY' => 'admin-secret', 'MONITOR_STATUS_FILE' => '/does/not/exist']);
$this->app = new Application(Database::inMemory(), $config);
}
public function testUnknownRouteReturns404(): void
{
$response = $this->app->handle(Request::create('GET', '/nope'));
self::assertSame(404, $response->status);
}
public function testWrongMethodReturns405(): void
{
$response = $this->app->handle(Request::create('DELETE', '/api/v1/tickets'));
self::assertSame(405, $response->status);
}
public function testWritingWithoutAgentKeyReturns401(): void
{
$response = $this->app->handle(Request::create('POST', '/api/v1/tickets', body: ['title' => 'X']));
self::assertSame(401, $response->status);
}
public function testRegisterAgentRequiresAdminKey(): void
{
$response = $this->app->handle(Request::create('POST', '/api/v1/agents', body: [
'display_name' => 'Alice', 'role' => 'entwickler',
]));
self::assertSame(401, $response->status);
}
public function testFullFlowRegisterAgentThenCreateTicket(): void
{
$registerResponse = $this->app->handle(Request::create(
'POST',
'/api/v1/agents',
body: ['display_name' => 'Alice', 'role' => 'entwickler'],
headers: ['X-Admin-Key' => 'admin-secret']
));
self::assertSame(201, $registerResponse->status);
$apiKey = $registerResponse->body()['api_key'];
$createResponse = $this->app->handle(Request::create(
'POST',
'/api/v1/tickets',
body: ['title' => 'Neues Ticket'],
headers: ['X-Api-Key' => $apiKey]
));
self::assertSame(201, $createResponse->status);
$listResponse = $this->app->handle(Request::create('GET', '/api/v1/tickets'));
self::assertCount(1, $listResponse->body()['tickets']);
}
public function testPatchUnknownTicketReturns404(): void
{
$registerResponse = $this->app->handle(Request::create(
'POST',
'/api/v1/agents',
body: ['display_name' => 'Alice', 'role' => 'entwickler'],
headers: ['X-Admin-Key' => 'admin-secret']
));
$apiKey = $registerResponse->body()['api_key'];
$response = $this->app->handle(Request::create(
'PATCH',
'/api/v1/tickets/999',
body: ['column' => 'bereit'],
headers: ['X-Api-Key' => $apiKey]
));
self::assertSame(404, $response->status);
}
}