Skip to content

Commit

Permalink
Add extends version unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dozsan committed Dec 19, 2024
1 parent 5d0a702 commit 6084ba2
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 2 deletions.
69 changes: 69 additions & 0 deletions tests/Entity/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace DataDog\AuditBundle\Tests\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Table()
*/
#[ORM\Entity]
#[ORM\Table]
class Account
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private int $id;

/**
* @ORM\Column(type="string", length=255)
*/
#[ORM\Column(type: Types::STRING, length: 255)]
private string $username;

/**
* @ORM\Column(type="string", length=255)
*/
#[ORM\Column(type: Types::STRING, length: 255)]
private string $password;

public function getId(): int
{
return $this->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;
}
}
111 changes: 111 additions & 0 deletions tests/EventListener/AuditListenerExtendsVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace DataDog\AuditBundle\Tests\EventListener;

use DataDog\AuditBundle\Entity\AuditLog;
use DataDog\AuditBundle\Tests\Entity\Account;
use DataDog\AuditBundle\Tests\OrmTestCase;
use DataDog\AuditBundle\Tests\TestKernel;

final class AuditListenerExtendsVersionTest extends OrmTestCase
{
protected function setUp(): void
{
}

/**
* @dataProvider dataProvider
*/
public function testExtendsVersion(
array $dataDogAuditConfig,
callable $callable,
array $expectedFields
): void {
$this->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',
],
]
];
}
}
19 changes: 19 additions & 0 deletions tests/EventListener/AuditListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
4 changes: 2 additions & 2 deletions tests/TestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 6084ba2

Please sign in to comment.