1. AgentRepository::updateActivity() schrieb current_ticket_id ungeprueft. Eine
unbekannte Ticket-ID lief in die FOREIGN-KEY-Constraint und kam als
ungefangene PDOException samt Stacktrace und Dateipfaden beim Client an.
Jetzt wird die Existenz vorab geprueft (422). Zusaetzlich faengt
Application::handle() unerwartete Throwables ab und antwortet mit einem
generischen 500 - Details gehen nur noch ins Log.
2. Die Normalisierung der Codeberg-Label-Farbe ("#" davor) lag im
CurlCodebergClient statt im CodebergSyncService. Damit haetten andere
Client-Implementierungen unterschiedliche Farbformate ins Ticket geschrieben.
Verschoben in die Domaenenschicht, leere Farben ergeben jetzt null.
3. Config::get() griff selbst auf getenv() zurueck, wodurch ein in der Umgebung
gesetzter CODEBERG_TOKEN in eine explizit gebaute Config durchschlug - im
Testlauf landete der Token im Klartext in der PHPUnit-Ausgabe. Die Umgebung
wird jetzt ausschliesslich in fromEnvironment() eingesammelt.
Regressionstests fuer alle drei Punkte ergaenzt: 104 Tests, 181 Assertions.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
115 lines
3.6 KiB
PHP
115 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Monitor\Api\Tests\Repository;
|
|
|
|
use InvalidArgumentException;
|
|
use Monitor\Api\Db\Database;
|
|
use Monitor\Api\Http\NotFoundException;
|
|
use Monitor\Api\Repository\AgentRepository;
|
|
use Monitor\Api\Repository\TicketRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class AgentRepositoryTest extends TestCase
|
|
{
|
|
private AgentRepository $repository;
|
|
private TicketRepository $tickets;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$db = Database::inMemory();
|
|
$this->repository = new AgentRepository($db, deskCount: 2);
|
|
$this->tickets = new TicketRepository($db);
|
|
}
|
|
|
|
public function testRegisterAssignsLowestFreeDesk(): void
|
|
{
|
|
$first = $this->repository->register('Alice', 'entwickler');
|
|
$second = $this->repository->register('Bob', 'qa');
|
|
|
|
self::assertSame(0, $first['agent']['desk_index']);
|
|
self::assertSame(1, $second['agent']['desk_index']);
|
|
self::assertNotSame($first['api_key'], $second['api_key']);
|
|
self::assertArrayNotHasKey('api_key_hash', $first['agent']);
|
|
}
|
|
|
|
public function testUserRoleNeverGetsADesk(): void
|
|
{
|
|
$result = $this->repository->register('Carol', 'user');
|
|
|
|
self::assertNull($result['agent']['desk_index']);
|
|
}
|
|
|
|
public function testOverflowAgentGetsNoDeskWhenAllOccupied(): void
|
|
{
|
|
$this->repository->register('Alice', 'entwickler');
|
|
$this->repository->register('Bob', 'qa');
|
|
$third = $this->repository->register('Dan', 'product_owner');
|
|
|
|
self::assertNull($third['agent']['desk_index']);
|
|
}
|
|
|
|
public function testRegisterRejectsUnknownRole(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->repository->register('Eve', 'ceo');
|
|
}
|
|
|
|
public function testFindByApiKeyReturnsMatchingAgent(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
|
|
$found = $this->repository->findByApiKey($result['api_key']);
|
|
|
|
self::assertNotNull($found);
|
|
self::assertSame($result['agent']['id'], $found['id']);
|
|
}
|
|
|
|
public function testFindByApiKeyReturnsNullForUnknownKey(): void
|
|
{
|
|
self::assertNull($this->repository->findByApiKey('does-not-exist'));
|
|
}
|
|
|
|
public function testUpdateActivityChangesStateAndTicket(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
$id = (int) $result['agent']['id'];
|
|
$ticketId = (int) $this->tickets->create(['title' => 'Testticket'])['id'];
|
|
|
|
$updated = $this->repository->updateActivity($id, 'working', $ticketId);
|
|
|
|
self::assertSame('working', $updated['state']);
|
|
self::assertSame($ticketId, $updated['current_ticket_id']);
|
|
}
|
|
|
|
public function testUpdateActivityRejectsUnknownTicket(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->repository->updateActivity((int) $result['agent']['id'], 'working', 999);
|
|
}
|
|
|
|
public function testUpdateActivityThrowsForUnknownAgent(): void
|
|
{
|
|
$this->expectException(NotFoundException::class);
|
|
|
|
$this->repository->updateActivity(999, 'working', null);
|
|
}
|
|
|
|
public function testAllListsAgentsWithoutApiKeyHash(): void
|
|
{
|
|
$this->repository->register('Alice', 'entwickler');
|
|
$this->repository->register('Bob', 'qa');
|
|
|
|
$all = $this->repository->all();
|
|
|
|
self::assertCount(2, $all);
|
|
foreach ($all as $agent) {
|
|
self::assertArrayNotHasKey('api_key_hash', $agent);
|
|
}
|
|
}
|
|
}
|