diff --git a/tests/Entity/Account.php b/tests/Entity/Account.php new file mode 100644 index 0000000..27ad00d --- /dev/null +++ b/tests/Entity/Account.php @@ -0,0 +1,69 @@ +id; + } + + public function setId(int $id): void + { + $this->id = $id; + } + + public function getUsername(): string + { + return $this->username; + } + + public function setUsername(string $username): void + { + $this->username = $username; + } + + public function getPassword(): string + { + return $this->password; + } + + public function setPassword(string $password): void + { + $this->password = $password; + } +} diff --git a/tests/EventListener/AuditListenerExtendsVersionTest.php b/tests/EventListener/AuditListenerExtendsVersionTest.php new file mode 100644 index 0000000..7bad536 --- /dev/null +++ b/tests/EventListener/AuditListenerExtendsVersionTest.php @@ -0,0 +1,111 @@ +kernel = new TestKernel([ + 'entities' => [ + Account::class => $dataDogAuditConfig, + ], + ]); + $this->kernel->boot(); + + $this->loadFixtures(); + + $em = $this->getDoctrine()->getManager(); + + $account = new Account(); + $callable($account); + + $em->persist($account); + $em->flush(); + + $auditLogs = $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult(); + $this->assertCount(1, $auditLogs); + + foreach ($expectedFields as $field) { + $this->assertArrayHasKey($field, $auditLogs[0]->getDiff()); + } + } + + public static function dataProvider(): array + { + return [ + 'exclude-password-field' => [ + [ + 'mode' => 'exclude', + 'fields' => [ + 'password', + ], + ], + function (Account $account): void { + $account->setUsername('username'); + $account->setPassword('password'); + }, + [ + 'username', + ], + ], + 'exclude-all' => [ + [ + 'mode' => 'exclude', + 'fields' => null, + ], + function (Account $account): void { + $account->setUsername('username'); + $account->setPassword('password'); + }, + [], + ], + 'include-password-field' => [ + [ + 'mode' => 'include', + 'fields' => [ + 'password', + ], + ], + function (Account $account): void { + $account->setUsername('username'); + $account->setPassword('password'); + }, + [ + 'password', + ], + ], + 'include-all' => [ + [ + 'mode' => 'include', + 'fields' => null, + ], + function (Account $account): void { + $account->setUsername('username'); + $account->setPassword('password'); + }, + [ + 'username', + 'password', + ], + ] + ]; + } +} diff --git a/tests/EventListener/AuditListenerTest.php b/tests/EventListener/AuditListenerTest.php index b084e20..4c5b62e 100644 --- a/tests/EventListener/AuditListenerTest.php +++ b/tests/EventListener/AuditListenerTest.php @@ -120,4 +120,23 @@ public function testEntityRelationUpdate(): void $this->assertCount(6, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); } + + public function testExcludeField(): void + { + $this->resetDatabase(); + + $em = $this->getDoctrine()->getManager(); + + $tag = new Tag(); + $tag->setName('Books'); + + $em->persist($tag); + $em->flush(); + + $tag->setName('Movies'); + + $em->flush(); + + $this->assertCount(2, $em->createQuery('SELECT l FROM '.AuditLog::class.' l')->getResult()); + } } diff --git a/tests/TestKernel.php b/tests/TestKernel.php index 518affe..7b36887 100644 --- a/tests/TestKernel.php +++ b/tests/TestKernel.php @@ -17,7 +17,7 @@ class TestKernel extends Kernel { private ?string $projectDir = null; - public function __construct() + public function __construct(private readonly array $dataDogAuditConfig = []) { parent::__construct('test', true); } @@ -55,7 +55,7 @@ public function registerContainerConfiguration(LoaderInterface $loader): void ], ], ]); - $container->loadFromExtension('data_dog_audit'); + $container->loadFromExtension('data_dog_audit', $this->dataDogAuditConfig); $container->register('logger', NullLogger::class); });