Docs / Advanced / Tests

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:

config/packages/test/symfonycorp_audit_log.yaml
symfonycorp_audit_log:
    silent_failures: false
    outputs:
        dbal: false     # one line for each output of your main config
        in_memory: ~

Write a test

tests/OrderServiceTest.php
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

AssertionChecks
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