monitor/api/tests/Unit/Repository/KpiRepositoryTest.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

61 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Monitor\Api\Tests\Repository;
use Monitor\Api\Db\Database;
use Monitor\Api\Repository\KpiRepository;
use PHPUnit\Framework\TestCase;
final class KpiRepositoryTest extends TestCase
{
private KpiRepository $repository;
protected function setUp(): void
{
$this->repository = new KpiRepository(Database::inMemory(), historyLimit: 3);
}
public function testReportCreatesKpiWithCurrentValue(): void
{
$kpi = $this->repository->report('open_tickets', 5.0, 'Offene Tickets', 'count', '2026-01-01T00:00:00Z');
self::assertSame('open_tickets', $kpi['key']);
self::assertSame(5.0, $kpi['current_value']);
self::assertCount(1, $kpi['history']);
}
public function testReportAppendsHistoryAndUpdatesCurrentValue(): void
{
$this->repository->report('open_tickets', 5.0, null, null, '2026-01-01T00:00:00Z');
$kpi = $this->repository->report('open_tickets', 7.0, null, null, '2026-01-02T00:00:00Z');
self::assertSame(7.0, $kpi['current_value']);
self::assertCount(2, $kpi['history']);
}
public function testHistoryIsLimited(): void
{
for ($i = 0; $i < 5; $i++) {
$this->repository->report('kpi', (float) $i, null, null, sprintf('2026-01-0%dT00:00:00Z', $i + 1));
}
$kpi = $this->repository->find('kpi');
self::assertCount(3, $kpi['history']);
}
public function testFindReturnsNullForUnknownKey(): void
{
self::assertNull($this->repository->find('unknown'));
}
public function testAllListsAllKpis(): void
{
$this->repository->report('a', 1.0, null, null, '2026-01-01T00:00:00Z');
$this->repository->report('b', 2.0, null, null, '2026-01-01T00:00:00Z');
self::assertCount(2, $this->repository->all());
}
}