ldc-arcadia-api/tests/Integration/Infra/Adapters/Directories/EloquentClientAuditLogDirectoryTest.php

70 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Integration\Infra\Adapters\Directories;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Infra\Adapters\Directories\EloquentClientAuditLogDirectory;
use Infra\Database\Models\ClientAuditLog;
use Ramsey\Uuid\Uuid;
use Tests\TestCase;
uses(TestCase::class, RefreshDatabase::class);
covers(EloquentClientAuditLogDirectory::class);
describe('EloquentClientAuditLogDirectory Integration Tests', function (): void {
beforeEach(function (): void {
ClientAuditLog::query()->delete();
});
it('retrieves logs filtered by date range, client and event type', function (): void {
// GIVEN
$clientId1 = Uuid::uuid7()->toString();
$clientId2 = Uuid::uuid7()->toString();
$clientIdentifier1 = 'arcadia_id_' . str_repeat('1', 32);
$clientIdentifier2 = 'arcadia_id_' . str_repeat('2', 32);
ClientAuditLog::query()->create([
'id' => Uuid::uuid7(new \DateTimeImmutable('2026-08-01 10:00:00 UTC'))->toString(),
'client_id' => $clientId1,
'client_identifier' => $clientIdentifier1,
'event_type' => 'client_secret_generated',
'details' => null,
]);
ClientAuditLog::query()->create([
'id' => Uuid::uuid7(new \DateTimeImmutable('2026-08-10 14:00:00 UTC'))->toString(),
'client_id' => $clientId1,
'client_identifier' => $clientIdentifier1,
'event_type' => 'client_status_toggled',
'details' => ['is_active' => 'false'],
]);
ClientAuditLog::query()->create([
'id' => Uuid::uuid7(new \DateTimeImmutable('2026-08-15 12:00:00 UTC'))->toString(),
'client_id' => $clientId2,
'client_identifier' => $clientIdentifier2,
'event_type' => 'client_secret_generated',
'details' => null,
]);
$directory = new EloquentClientAuditLogDirectory();
// WHEN (Filter by date range and client1)
$since = new \DateTimeImmutable('2026-08-05 00:00:00');
$to = new \DateTimeImmutable('2026-08-20 23:59:59');
$results = $directory->findLogs(
since: $since,
to: $to,
clientIdentifier: $clientIdentifier1,
);
// THEN
expect($results)->toHaveCount(1)
->and($results[0]->clientIdentifier)->toBe($clientIdentifier1)
->and($results[0]->eventType)->toBe('client_status_toggled')
->and($results[0]->details->getValues())->toBe(['is_active' => 'false']);
});
});