update.sh meldet nach dem Neustart ein Event (POST /api/v1/events/system, Admin-Key). upsertPullRequest markiert bereits vorhandene Tickets als Pull Request, damit sie den /pulls/-Link bekommen. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
219 lines
7.4 KiB
PHP
219 lines
7.4 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\TicketRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class TicketRepositoryTest extends TestCase
|
|
{
|
|
private TicketRepository $repository;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->repository = new TicketRepository(Database::inMemory());
|
|
}
|
|
|
|
public function testCreateDefaultsToBacklogColumn(): void
|
|
{
|
|
$ticket = $this->repository->create(['title' => 'Neues Ticket']);
|
|
|
|
self::assertSame('backlog', $ticket['column_name']);
|
|
self::assertSame('Neues Ticket', $ticket['title']);
|
|
}
|
|
|
|
public function testUpsertPullRequestFlagsExistingIssueTicket(): void
|
|
{
|
|
$this->repository->create(['title' => 'PR', 'external_ref' => '107', 'repo' => 'a/b', 'source' => 'codeberg']);
|
|
|
|
$ticket = $this->repository->upsertPullRequest(['external_ref' => '107', 'repo' => 'a/b', 'title' => 'PR neu']);
|
|
|
|
self::assertSame(1, (int) $ticket['is_pull_request']);
|
|
self::assertSame(1, (int) $this->repository->findByExternalRef('107', 'a/b')['is_pull_request']);
|
|
}
|
|
|
|
public function testCreateRejectsUnknownColumn(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->repository->create(['title' => 'X', 'column' => 'unknown']);
|
|
}
|
|
|
|
public function testUpdateMovesTicketToAnotherColumn(): void
|
|
{
|
|
$ticket = $this->repository->create(['title' => 'X']);
|
|
|
|
$updated = $this->repository->update((int) $ticket['id'], ['column' => 'in_arbeit']);
|
|
|
|
self::assertSame('in_arbeit', $updated['column_name']);
|
|
}
|
|
|
|
public function testUpdateRejectsUnknownColumn(): void
|
|
{
|
|
$ticket = $this->repository->create(['title' => 'X']);
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->repository->update((int) $ticket['id'], ['column' => 'unknown']);
|
|
}
|
|
|
|
public function testUpdateThrowsNotFoundForMissingTicket(): void
|
|
{
|
|
$this->expectException(NotFoundException::class);
|
|
|
|
$this->repository->update(999, ['column' => 'bereit']);
|
|
}
|
|
|
|
public function testAllFiltersByColumn(): void
|
|
{
|
|
$this->repository->create(['title' => 'A', 'column' => 'backlog']);
|
|
$this->repository->create(['title' => 'B', 'column' => 'done']);
|
|
|
|
$backlog = $this->repository->all('backlog');
|
|
|
|
self::assertCount(1, $backlog);
|
|
self::assertSame('A', $backlog[0]['title']);
|
|
}
|
|
|
|
public function testAllOrdersByColumnThenCreatedAt(): void
|
|
{
|
|
$this->repository->create(['title' => 'first', 'column' => 'backlog']);
|
|
$this->repository->create(['title' => 'second', 'column' => 'backlog']);
|
|
|
|
$tickets = $this->repository->all();
|
|
|
|
self::assertSame('first', $tickets[0]['title']);
|
|
self::assertSame('second', $tickets[1]['title']);
|
|
}
|
|
|
|
public function testUpsertFromCodebergCreatesNewTicket(): void
|
|
{
|
|
$ticket = $this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42',
|
|
'description' => 'Body',
|
|
'column' => 'backlog',
|
|
'type' => 'bug',
|
|
'color' => '#ff0000',
|
|
]);
|
|
|
|
self::assertSame('42', $ticket['external_ref']);
|
|
self::assertSame('codeberg', $ticket['source']);
|
|
}
|
|
|
|
public function testUpsertFromCodebergUpdatesExistingBacklogTicket(): void
|
|
{
|
|
$this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42',
|
|
'column' => 'backlog',
|
|
]);
|
|
|
|
$updated = $this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42 (closed)',
|
|
'column' => 'done',
|
|
]);
|
|
|
|
self::assertSame('done', $updated['column_name']);
|
|
self::assertSame('Issue 42 (closed)', $updated['title']);
|
|
}
|
|
|
|
public function testUpsertFromCodebergDoesNotResetManuallyMovedTicket(): void
|
|
{
|
|
$this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42',
|
|
'column' => 'backlog',
|
|
]);
|
|
$ticket = $this->repository->findByExternalRef('42');
|
|
$this->repository->update((int) $ticket['id'], ['column' => 'fertig']);
|
|
|
|
$result = $this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42 geaendert',
|
|
'column' => 'backlog',
|
|
]);
|
|
|
|
self::assertSame('fertig', $result['column_name']);
|
|
}
|
|
|
|
public function testUpsertFromCodebergMovesTicketBetweenSyncManagedColumns(): void
|
|
{
|
|
$this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42',
|
|
'column' => 'bereit',
|
|
]);
|
|
|
|
$result = $this->repository->upsertFromCodeberg([
|
|
'external_ref' => '42',
|
|
'title' => 'Issue 42',
|
|
'column' => 'in_arbeit',
|
|
]);
|
|
|
|
self::assertSame('in_arbeit', $result['column_name']);
|
|
}
|
|
|
|
public function testFingerprintChangesWhenATicketIsCreated(): void
|
|
{
|
|
$before = $this->repository->fingerprint();
|
|
$this->repository->create(['title' => 'X']);
|
|
|
|
self::assertNotSame($before, $this->repository->fingerprint());
|
|
}
|
|
|
|
public function testFingerprintIsStableWithoutChanges(): void
|
|
{
|
|
$this->repository->create(['title' => 'X']);
|
|
|
|
self::assertSame($this->repository->fingerprint(), $this->repository->fingerprint());
|
|
}
|
|
|
|
public function testClearColorForReposNotInRemovesColorFromOrphanedRepo(): void
|
|
{
|
|
$kept = $this->repository->create(['title' => 'A', 'repo' => 'fds2610/monitor', 'color' => '#fbca04']);
|
|
$orphaned = $this->repository->create(['title' => 'B', 'repo' => 'fds2610/altes-projekt', 'color' => '#3b82f6']);
|
|
|
|
$this->repository->clearColorForReposNotIn(['fds2610/monitor']);
|
|
|
|
self::assertSame('#fbca04', $this->repository->find((int) $kept['id'])['color']);
|
|
self::assertNull($this->repository->find((int) $orphaned['id'])['color']);
|
|
}
|
|
|
|
public function testClearColorForReposNotInIgnoresTicketsWithoutRepo(): void
|
|
{
|
|
$manual = $this->repository->create(['title' => 'API-Ticket', 'color' => '#ffffff']);
|
|
|
|
$this->repository->clearColorForReposNotIn(['fds2610/monitor']);
|
|
|
|
self::assertSame('#ffffff', $this->repository->find((int) $manual['id'])['color']);
|
|
}
|
|
|
|
public function testClearColorForReposNotInClearsAllColorsWhenNoRepoConfigured(): void
|
|
{
|
|
$ticket = $this->repository->create(['title' => 'B', 'repo' => 'fds2610/altes-projekt', 'color' => '#3b82f6']);
|
|
|
|
$this->repository->clearColorForReposNotIn([]);
|
|
|
|
self::assertNull($this->repository->find((int) $ticket['id'])['color']);
|
|
}
|
|
|
|
public function testDeleteByRepoRemovesOnlyTicketsOfThatRepo(): void
|
|
{
|
|
$removed = $this->repository->create(['title' => 'A', 'repo' => 'fds2610/altes-projekt']);
|
|
$kept = $this->repository->create(['title' => 'B', 'repo' => 'fds2610/monitor']);
|
|
$manual = $this->repository->create(['title' => 'C']);
|
|
|
|
$this->repository->deleteByRepo('fds2610/altes-projekt');
|
|
|
|
self::assertNull($this->repository->find((int) $removed['id']));
|
|
self::assertNotNull($this->repository->find((int) $kept['id']));
|
|
self::assertNotNull($this->repository->find((int) $manual['id']));
|
|
}
|
|
}
|