monitor/api/tests/Unit/Controller/SystemControllerTest.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

67 lines
2 KiB
PHP

<?php
declare(strict_types=1);
namespace Monitor\Api\Tests\Controller;
use Monitor\Api\Controller\SystemController;
use Monitor\Api\Db\Database;
use Monitor\Api\Http\Request;
use Monitor\Api\Repository\LegacyStatusFileReader;
use Monitor\Api\Repository\SystemRepository;
use PHPUnit\Framework\TestCase;
final class SystemControllerTest extends TestCase
{
private SystemController $controller;
private string $tmpFile;
protected function setUp(): void
{
$this->tmpFile = tempnam(sys_get_temp_dir(), 'status_');
file_put_contents($this->tmpFile, "Server1 1.2.3.4 online 2026-07-27 10:00\n");
$this->controller = new SystemController(
new SystemRepository(Database::inMemory()),
new LegacyStatusFileReader(),
$this->tmpFile
);
}
protected function tearDown(): void
{
if (is_file($this->tmpFile)) {
unlink($this->tmpFile);
}
}
public function testIndexImportsLegacyStatusFile(): void
{
$response = $this->controller->index(Request::create('GET', '/api/v1/systems'));
self::assertSame(200, $response->status);
self::assertCount(1, $response->body()['systems']);
self::assertSame('Server1', $response->body()['systems'][0]['id']);
}
public function testReportStatusRejectsInvalidStatus(): void
{
$response = $this->controller->reportStatus(
Request::create('POST', '/api/v1/systems/srv1/status', body: ['status' => 'weird']),
['id' => 'srv1']
);
self::assertSame(422, $response->status);
}
public function testReportStatusReturnsUpdatedSystem(): void
{
$response = $this->controller->reportStatus(
Request::create('POST', '/api/v1/systems/srv1/status', body: ['status' => 'ok']),
['id' => 'srv1']
);
self::assertSame(200, $response->status);
self::assertSame('ok', $response->body()['system']['status']);
}
}