Tests
Check the entries that your code creates with AuditLogAssertionTrait.
Test configuration
The real AuditLogger runs; only the output changes. In the test environment, enable the in_memory output and disable your other outputs:
symfonycorp_audit_log:
silent_failures: false
outputs:
dbal: false # one line for each output of your main config
in_memory: ~Write a test
use SymfonyCorp\AuditLog\Test\AuditLogAssertionTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
final class OrderServiceTest extends KernelTestCase
{
use AuditLogAssertionTrait;
public function testPlaceOrderCreatesAnAuditEntry(): void
{
self::getContainer()->get(OrderService::class)->place(new Order('O-42'));
$this->assertAuditEntryCount(1);
$this->assertAuditEntryMatches(['source' => 'order', 'action' => 'PLACE']);
$this->assertAuditEntryContextContains('order_id', 'O-42');
}
}Assertions
| Assertion | Checks |
|---|---|
assertAuditEntryCount($expected) | The number of entries |
assertNoAuditEntry() | No entry |
assertAuditEntryExists($message, $source, $action) | An entry with this message (and source, action) exists |
assertAuditEntryMatches($expected, $index) | Some properties of one entry. context must match in full |
assertAuditEntryContextContains($key, $value, $index) | One context value of one entry |
Tips
- Keep
silent_failures: false. Without it, a failing output only logs the failure and the test passes.in_memorythen rejects a context thatdbalcannot encode. - Fixtures also create entries. Call
$this->getAuditLogOutput()->reset()after you load them. - Each new request starts with no entry. Assert after each request.
- With
dbal: false, the audit table is not in the test schema. If your test database has it, exclude it in the test environment only:doctrine.dbal.schema_filter: '~^(?!audit_log(_id_seq)?$)~'.