Skip to content

Commit

Permalink
[#308] Update AuditedCollection in order to respect Collection in…
Browse files Browse the repository at this point in the history
…terface
  • Loading branch information
phansys committed Apr 11, 2018
1 parent 79ba3f4 commit c415142
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/SimpleThings/EntityAudit/Collection/AuditedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace SimpleThings\EntityAudit\Collection;

use Closure;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use SimpleThings\EntityAudit\AuditConfiguration;
Expand Down Expand Up @@ -283,7 +284,7 @@ public function filter(Closure $p)
{
$this->forceLoad();

return array_filter($this->entities, $p);
return new ArrayCollection(array_filter($this->entities, $p));
}

/**
Expand All @@ -309,7 +310,7 @@ public function map(Closure $func)
{
$this->forceLoad();

return array_map($func, $this->entities);
return new ArrayCollection(array_map($func, $this->entities));
}

/**
Expand All @@ -329,7 +330,7 @@ public function partition(Closure $p)
}
}

return array($true, $false);
return new ArrayCollection(array($true, $false));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace SimpleThings\EntityAudit\Tests\Fixtures\Issue;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class Issue308User
{
/** @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue(strategy="AUTO") */
private $id;

/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Issue308User", mappedBy="parent")
*/
private $children;

/**
* @var Issue308User
*
* @ORM\ManyToOne(targetEntity="Issue308User", inversedBy="children")
*/
private $parent;

public function __construct()
{
$this->children = new ArrayCollection();
}

public function getId()
{
return $this->id;
}

/**
* @transient
*
* @return bool
*/
public function isActive()
{
return false;
}

/**
* @param Issue308User $child
*/
public function addChild(Issue308User $child)
{
$this->children->add($child);
}

/**
* @return ArrayCollection
*/
public function getChildren()
{
$activeChildren = $this->children->filter(function (Issue308User $user) {
return $user->isActive();
});

return $activeChildren;
}

/**
* @return Issue308User
*/
public function getParent()
{
return $this->parent;
}
}
26 changes: 25 additions & 1 deletion tests/SimpleThings/Tests/EntityAudit/IssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue156ContactTelephoneNumber;
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue156Client;
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue196Entity;
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue308User;
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue31Reve;
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue31User;
use SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue318User;
Expand Down Expand Up @@ -69,6 +70,7 @@ class IssueTest extends BaseTest
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue198Car',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue198Owner',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue196Entity',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue308User',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue318User',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\ConvertToPHPEntity',
);
Expand All @@ -94,6 +96,7 @@ class IssueTest extends BaseTest
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue196Entity',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue198Car',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue198Owner',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue308User',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\Issue318User',
'SimpleThings\EntityAudit\Tests\Fixtures\Issue\ConvertToPHPEntity',
);
Expand Down Expand Up @@ -337,12 +340,33 @@ public function testIssue318()
$schema = $this->getSchemaTool()->getSchemaFromMetadata($classes);
$schemaName = $schema->getName();
$config = $this->getAuditManager()->getConfiguration();
$entityTableUser = $schema->getTable(sprintf('%s.issue318user', $schemaName));
$revisionsTableUser = $schema->getTable(sprintf('%s.%sissue318user%s', $schemaName, $config->getTablePrefix(), $config->getTableSuffix()));
$userNotNullColumnName = 'alias';
$userIdColumnName = 'id';

$this->assertFalse($revisionsTableUser->getColumn($userNotNullColumnName)->getNotnull());
$this->assertFalse($revisionsTableUser->getColumn($userIdColumnName)->getAutoincrement());
}

public function testIssue308()
{
$user = new Issue308User();
$child1 = new Issue308User();
$user->addChild($child1);
$this->em->persist($child1);
$this->em->persist($user);
$this->em->flush();

$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $user->getChildren());

$auditReader = $this->auditManager->createAuditReader($this->em);
$auditReader->setLoadAuditedCollections(true);
$userClass = get_class($user);
$revisions = $auditReader->findRevisions($userClass, $user->getId());
$this->assertCount(1, $revisions);
$revision = reset($revisions);
$auditedUser = $auditReader->find($userClass, array('id' => $user->getId()), $revision->getRev());

$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $auditedUser->getChildren());
}
}

0 comments on commit c415142

Please sign in to comment.