Skip to content

Commit

Permalink
Fallback to native entity if no revision found for properties
Browse files Browse the repository at this point in the history
If an audited entity has relational properties, the reader will
try to find them as the same version, or previous.

But if the targeted class does not has any revision at all, this
produces a NoRevisionFound exception.

This should be catched and should fallback to a classic doctrine find query.
  • Loading branch information
soullivaneuh committed Jan 6, 2017
1 parent b5be02a commit 30cf7c7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/SimpleThings/EntityAudit/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ private function createEntity($className, array $columnMap, array $data, $revisi
$value = $this->find($targetClass->name, $pk, $revision, array('threatDeletionsAsExceptions' => true));
} catch (DeletedException $e) {
$value = null;
} catch (NoRevisionFoundException $e) {
// The entity does not have any revision yet. So let's get the actual state of it.
$value = $this->em->find($targetClass->name, $pk);
}

$class->reflFields[$field]->setValue($entity, $value);
Expand Down
65 changes: 62 additions & 3 deletions tests/SimpleThings/Tests/EntityAudit/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,65 @@ public function testFindEntitesChangedAtRevision()
$this->assertInstanceOf('SimpleThings\EntityAudit\Tests\Fixtures\Core\UserAudit', $changedEntities[1]->getEntity());
}

public function testNotVersionedRelationFind()
{
// Insert user without the manager to skip revision registering.
$this->em->getConnection()->insert(
$this->em->getClassMetadata('SimpleThings\EntityAudit\Tests\Fixtures\Core\UserAudit')->getTableName(),
array(
'name' => 'beberlei',
)
);

$article = new ArticleAudit(
"test",
"yadda!",
$this->em->getRepository('SimpleThings\EntityAudit\Tests\Fixtures\Core\UserAudit')->find(1),
'text'
);

$this->em->persist($article);
$this->em->flush();

$reader = $this->auditManager->createAuditReader($this->em);

$this->assertSame('beberlei', $reader->find(get_class($article), 1, 1)->getAuthor()->getName());
}

public function testNotExistingRelationFind()
{
// Insert user without the manager to skip revision registering.
$this->em->getConnection()->insert(
$this->em->getClassMetadata('SimpleThings\EntityAudit\Tests\Fixtures\Core\UserAudit')->getTableName(),
array(
'name' => 'beberlei',
)
);

$article = new ArticleAudit(
"test",
"yadda!",
$this->em->getRepository('SimpleThings\EntityAudit\Tests\Fixtures\Core\UserAudit')->find(1),
'text'
);

$this->em->persist($article);
$this->em->flush();

// Then, remove the user
$this->em->getConnection()->delete(
$this->em->getClassMetadata('SimpleThings\EntityAudit\Tests\Fixtures\Core\UserAudit')->getTableName(),
array(
'name' => 'beberlei',
)
);
$this->em->clear();

$reader = $this->auditManager->createAuditReader($this->em);

$this->assertNull($reader->find(get_class($article), 1, 1)->getAuthor());
}

public function testFindRevisions()
{
$user = new UserAudit("beberlei");
Expand Down Expand Up @@ -341,14 +400,14 @@ public function testUsernameResolvingIsDynamic()
$this->auditManager->getConfiguration()->setUsernameCallable(function () {
return 'user: ' . uniqid();
});

$user = new UserAudit('beberlei');
$this->em->persist($user);
$this->em->flush();

$user->setName('b.eberlei');
$this->em->flush();

$reader = $this->auditManager->createAuditReader($this->em);
$revisions = $reader->findRevisionHistory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function getId()
return $this->id;
}

public function getAuthor()
{
return $this->author;
}

public function setText($text)
{
$this->text = $text;
Expand Down

0 comments on commit 30cf7c7

Please sign in to comment.