Skip to content

Commit

Permalink
Resets transaction once processed by the onFlush event subscriber. F…
Browse files Browse the repository at this point in the history
…ixes #44 (#48)
  • Loading branch information
DamienHarper committed Sep 27, 2021
1 parent 6843e88 commit 6b4be4a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Model/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ public function getDissociated(): array
return $this->dissociated;
}

public function reset(): void
{
$this->transaction_hash = null;
$this->inserted = [];
$this->updated = [];
$this->removed = [];
$this->associated = [];
$this->dissociated = [];
}

public function trackAuditEvent(string $type, array $data): void
{
switch ($type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function onFlush(OnFlushEventArgs $args): void
// flushes pending data
$entityManager->getConnection()->getConfiguration()->setSQLLogger($this->loggerBackup);
$this->transactionManager->process($transaction);
$transaction->reset();
});

// Initialize a new LoggerChain with the new AuditLogger + the existing SQLLoggers.
Expand Down
47 changes: 47 additions & 0 deletions tests/Provider/Doctrine/Issues/Issue44Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace DH\Auditor\Tests\Provider\Doctrine\Issues;

use DH\Auditor\Model\Transaction;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\DummyEntity;
use DH\Auditor\Tests\Provider\Doctrine\Traits\ReaderTrait;
use DH\Auditor\Tests\Provider\Doctrine\Traits\Schema\SchemaSetupTrait;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
final class Issue44Test extends TestCase
{
use ReaderTrait;
use SchemaSetupTrait;

public function testIssue44(): void
{
$reader = $this->createReader();

$em = $this->provider->getStorageServiceForEntity(DummyEntity::class)->getEntityManager();
$em->beginTransaction();
$entity = new DummyEntity();
$entity->setLabel('entity1');
$em->persist($entity);
$em->flush();
$em->flush();
$em->commit();
for ($n = 1; $n <= 10; ++$n) {
$em->beginTransaction();
$em->commit();
}

$audits = $reader->createQuery(DummyEntity::class)->execute();
self::assertCount(1, $audits, 'results count ok.');
self::assertSame(Transaction::INSERT, $audits[0]->getType(), 'Reader::INSERT operation.');
}

private function configureEntities(): void
{
$this->provider->getConfiguration()->setEntities([
DummyEntity::class => ['enabled' => true],
]);
}
}

0 comments on commit 6b4be4a

Please sign in to comment.