40 lines
1.4 KiB
PHP
40 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\AuditLog;
|
|
|
|
use Netig\Netslim\AuditLog\Application\AuditLogApplicationService;
|
|
use Netig\Netslim\AuditLog\Infrastructure\PdoAuditLogRepository;
|
|
use PDO;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class AuditLogServiceTest extends TestCase
|
|
{
|
|
private PDO $db;
|
|
|
|
private AuditLogApplicationService $service;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->db = new PDO('sqlite::memory:', options: [
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]);
|
|
$this->db->exec('CREATE TABLE audit_log (id INTEGER PRIMARY KEY AUTOINCREMENT, action TEXT NOT NULL, resource_type TEXT NOT NULL, resource_id TEXT NOT NULL, actor_user_id INTEGER DEFAULT NULL, context_json TEXT DEFAULT NULL, created_at TEXT NOT NULL)');
|
|
$this->service = new AuditLogApplicationService(new PdoAuditLogRepository($this->db));
|
|
}
|
|
|
|
public function testRecordsAndReadsAuditEntries(): void
|
|
{
|
|
$this->service->record('taxonomy.created', 'taxon', '12', 7, ['name' => 'PHP']);
|
|
$entries = $this->service->listRecent();
|
|
|
|
self::assertCount(1, $entries);
|
|
self::assertSame('taxonomy.created', $entries[0]->action);
|
|
self::assertSame('taxon', $entries[0]->resourceType);
|
|
self::assertSame('12', $entries[0]->resourceId);
|
|
self::assertSame(['name' => 'PHP'], $entries[0]->context);
|
|
}
|
|
}
|