Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better transaction processor/hydrator code coverage #93

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ private function hydrateWithScheduledCollectionUpdates(Transaction $transaction,
$transaction->trackAuditEvent(Transaction::DISSOCIATE, [
$collection->getOwner(),
$entity,
$this->id($entityManager, $entity),
$mapping,
]);
}
Expand All @@ -128,7 +127,6 @@ private function hydrateWithScheduledCollectionDeletions(Transaction $transactio
$transaction->trackAuditEvent(Transaction::DISSOCIATE, [
$collection->getOwner(),
$entity,
$this->id($entityManager, $entity),
$mapping,
]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private function processAssociations(Transaction $transaction, EntityManagerInte

private function processDissociations(Transaction $transaction, EntityManagerInterface $entityManager): void
{
foreach ($transaction->getDissociated() as [$source, $target, $id, $mapping]) {
foreach ($transaction->getDissociated() as [$source, $target, $mapping]) {
$this->dissociate($entityManager, $source, $target, $mapping, $transaction->getTransactionHash());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace DH\Auditor\Tests\Provider\Doctrine\Auditing\Transaction;

use DateTimeImmutable;
use DH\Auditor\Model\Transaction;
use DH\Auditor\Provider\Doctrine\Auditing\Transaction\TransactionProcessor;
use DH\Auditor\Provider\Doctrine\Model\Transaction;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
use DH\Auditor\Provider\Doctrine\Service\StorageService;
use DH\Auditor\Tests\Provider\Doctrine\Fixtures\Entity\Standard\Blog\Author;
Expand Down Expand Up @@ -580,6 +580,228 @@ public function testDissociateManyToMany(): void
], $entry->getDiffs(), 'audit entry diffs is ok.');
}

public function testProcessInsertions(): void
{
$reader = new Reader($this->provider);
$processor = new TransactionProcessor($this->provider);

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity(Author::class);
$entityManager = $storageService->getEntityManager();

$transaction = new Transaction($entityManager);

$author = new Author();
$author
->setId(1)
->setFullname('John Doe')
->setEmail('john.doe@gmail.com')
;

$transaction->trackAuditEvent(Transaction::INSERT, [
$author,
[
'fullname' => [null, 'John Doe'],
'email' => [null, 'john.doe@gmail.com'],
],
]);

$processor->process($transaction);

$audits = $reader->createQuery(Author::class)->execute();
self::assertCount(1, $audits, 'TransactionProcessor::processInsertions() creates an "'.Transaction::INSERT.'" audit entry.');
}

public function testProcessUpdates(): void
{
$reader = new Reader($this->provider);
$processor = new TransactionProcessor($this->provider);

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity(Author::class);
$entityManager = $storageService->getEntityManager();

$transaction = new Transaction($entityManager);

$author = new Author();
$author
->setId(1)
->setFullname('John Doze')
->setEmail('john.doze@gmail.com')
;

$transaction->trackAuditEvent(Transaction::UPDATE, [
$author,
[
'fullname' => ['John Doe', 'John Doze'],
'email' => ['john.doe@gmail.com', 'john.doze@gmail.com'],
],
]);

$processor->process($transaction);

$audits = $reader->createQuery(Author::class)->execute();
self::assertCount(1, $audits, 'TransactionProcessor::processUpdates() creates an "'.Transaction::UPDATE.'" audit entry.');

$transaction->reset();

$transaction->trackAuditEvent(Transaction::UPDATE, [
$author,
[],
]);

$processor->process($transaction);

$audits = $reader->createQuery(Author::class)->execute();
self::assertCount(1, $audits, 'TransactionProcessor::processUpdates() does not create an "'.Transaction::UPDATE.'" audit entry with empty diffs.');
}

public function testProcessAssociations(): void
{
$reader = new Reader($this->provider);
$processor = new TransactionProcessor($this->provider);

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity(Author::class);
$entityManager = $storageService->getEntityManager();

$transaction = new Transaction($entityManager);

$author = new Author();
$author
->setId(1)
->setFullname('John Doe')
->setEmail('john.doe@gmail.com')
;

$post = new Post();
$post
->setId(1)
->setAuthor($author)
->setTitle('First post')
->setBody('Here is the body')
->setCreatedAt(new DateTimeImmutable())
;

$mapping = [
'fieldName' => 'posts',
'mappedBy' => 'author',
'targetEntity' => Post::class,
'cascade' => [
0 => 'persist',
1 => 'remove',
],
'orphanRemoval' => false,
'fetch' => 2,
'type' => 4,
'inversedBy' => null,
'isOwningSide' => false,
'sourceEntity' => Author::class,
'isCascadeRemove' => true,
'isCascadePersist' => true,
'isCascadeRefresh' => false,
'isCascadeMerge' => false,
'isCascadeDetach' => false,
];

$transaction->trackAuditEvent(Transaction::ASSOCIATE, [
$author,
$post,
$mapping,
]);

$processor->process($transaction);

$audits = $reader->createQuery(Author::class)->execute();
self::assertCount(1, $audits, 'TransactionProcessor::processAssociations() creates an "'.Transaction::ASSOCIATE.'" audit entry.');
}

public function testProcessDissociations(): void
{
$reader = new Reader($this->provider);
$processor = new TransactionProcessor($this->provider);

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity(Author::class);
$entityManager = $storageService->getEntityManager();

$transaction = new Transaction($entityManager);

$author = new Author();
$author
->setId(1)
->setFullname('John Doe')
->setEmail('john.doe@gmail.com')
;

$post = new Post();
$post
->setId(1)
->setAuthor($author)
->setTitle('First post')
->setBody('Here is the body')
->setCreatedAt(new DateTimeImmutable())
;

$mapping = [
'fieldName' => 'posts',
'mappedBy' => 'author',
'targetEntity' => Post::class,
'cascade' => ['persist', 'remove'],
'orphanRemoval' => false,
'fetch' => 2,
'type' => 4,
'inversedBy' => null,
'isOwningSide' => false,
'sourceEntity' => Author::class,
'isCascadeRemove' => true,
'isCascadePersist' => true,
'isCascadeRefresh' => false,
'isCascadeMerge' => false,
'isCascadeDetach' => false,
];

$transaction->trackAuditEvent(Transaction::DISSOCIATE, [
$author,
$post,
$mapping,
]);

$processor->process($transaction);

$audits = $reader->createQuery(Author::class)->execute();
self::assertCount(1, $audits, 'TransactionProcessor::processDissociations() creates an "'.Transaction::DISSOCIATE.'" audit entry.');
}

public function testProcessDeletions(): void
{
$reader = new Reader($this->provider);
$processor = new TransactionProcessor($this->provider);

/** @var StorageService $storageService */
$storageService = $this->provider->getStorageServiceForEntity(Author::class);
$entityManager = $storageService->getEntityManager();

$transaction = new Transaction($entityManager);

$author = new Author();
$author
->setId(1)
->setFullname('John Doe')
->setEmail('john.doe@gmail.com')
;

$transaction->trackAuditEvent(Transaction::REMOVE, [
$author,
1,
]);

$processor->process($transaction);

$audits = $reader->createQuery(Author::class)->execute();
self::assertCount(1, $audits, 'TransactionProcessor::processDeletions() creates a "'.Transaction::REMOVE.'" audit entry.');
}

private function configureEntities(): void
{
$this->provider->getConfiguration()->setEntities([
Expand Down