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

79 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Monitor\Api\Tests\Controller;
use Monitor\Api\Controller\TicketController;
use Monitor\Api\Db\Database;
use Monitor\Api\Http\NotFoundException;
use Monitor\Api\Http\Request;
use Monitor\Api\Repository\TicketRepository;
use PHPUnit\Framework\TestCase;
final class TicketControllerTest extends TestCase
{
private TicketController $controller;
private TicketRepository $repository;
protected function setUp(): void
{
$this->repository = new TicketRepository(Database::inMemory());
$this->controller = new TicketController($this->repository);
}
public function testIndexReturnsAllTickets(): void
{
$this->repository->create(['title' => 'A']);
$response = $this->controller->index(Request::create('GET', '/api/v1/tickets'));
self::assertSame(200, $response->status);
self::assertCount(1, $response->body()['tickets']);
}
public function testCreateRejectsMissingTitle(): void
{
$response = $this->controller->create(Request::create('POST', '/api/v1/tickets', body: []));
self::assertSame(422, $response->status);
}
public function testCreateReturns201WithTicket(): void
{
$response = $this->controller->create(Request::create('POST', '/api/v1/tickets', body: ['title' => 'Neu']));
self::assertSame(201, $response->status);
self::assertSame('Neu', $response->body()['ticket']['title']);
}
public function testCreateRejectsInvalidColumnWith422(): void
{
$response = $this->controller->create(Request::create('POST', '/api/v1/tickets', body: ['title' => 'X', 'column' => 'invalid']));
self::assertSame(422, $response->status);
}
public function testUpdateMovesTicket(): void
{
$ticket = $this->repository->create(['title' => 'X']);
$response = $this->controller->update(
Request::create('PATCH', '/api/v1/tickets/' . $ticket['id'], body: ['column' => 'bereit']),
['id' => (string) $ticket['id']]
);
self::assertSame(200, $response->status);
self::assertSame('bereit', $response->body()['ticket']['column_name']);
}
public function testUpdatePropagatesNotFoundForUnknownTicket(): void
{
$this->expectException(NotFoundException::class);
$this->controller->update(
Request::create('PATCH', '/api/v1/tickets/999', body: ['column' => 'bereit']),
['id' => '999']
);
}
}