Skip to content

Commit

Permalink
[new] ReferencesListener expects ManagerRegistry array instead of Obj…
Browse files Browse the repository at this point in the history
…ectManager array
  • Loading branch information
Daniel Bojdo committed May 18, 2015
1 parent 3940af1 commit 54b19d5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
46 changes: 34 additions & 12 deletions lib/Gedmo/References/ReferencesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\EventArgs;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Gedmo\Exception\RuntimeException;
use Gedmo\Mapping\MappedEventSubscriber;

/**
Expand All @@ -17,11 +19,14 @@
*/
class ReferencesListener extends MappedEventSubscriber
{
private $managers;
/**
* @var ManagerRegistry[]
*/
private $registries;

public function __construct(array $managers = array())
public function __construct(array $registries = array())
{
$this->managers = $managers;
$this->registries = $registries;
}

public function loadClassMetadata(EventArgs $eventArgs)
Expand All @@ -48,7 +53,7 @@ public function postLoad(EventArgs $eventArgs)
$property->setValue(
$object,
$ea->getSingleReference(
$this->getManager($mapping['type']),
$this->getManager($mapping['type'], $mapping['class']),
$mapping['class'],
$referencedObjectId
)
Expand All @@ -62,7 +67,7 @@ public function postLoad(EventArgs $eventArgs)
$property->setAccessible(true);
if (isset($mapping['mappedBy'])) {
$id = $ea->extractIdentifier($om, $object);
$manager = $this->getManager($mapping['type']);
$manager = $this->getManager($mapping['type'], $mapping['class']);
$class = $mapping['class'];
$refMeta = $manager->getClassMetadata($class);
$refConfig = $this->getConfiguration($manager, $refMeta->name);
Expand Down Expand Up @@ -110,19 +115,36 @@ public function getSubscribedEvents()
);
}

public function registerManager($type, $manager)
/**
* @param string $type
* @param ManagerRegistry $registry
*/
public function setRegistry($type, ManagerRegistry $registry)
{
$this->managers[$type] = $manager;
$this->registries[$type] = $registry;
}

/**
* @param string $type
*
* @param string $class
* @return ObjectManager
*/
public function getManager($type)
public function getManager($type, $class)
{
return $this->managers[$type];
if (!isset($this->registries[$type])) {
throw new RuntimeException(
sprintf('Could not find Registry with required type "%s".', $type)
);
}

$manager = $this->registries[$type]->getManagerForClass($class);
if (!$manager) {
throw new RuntimeException(
sprintf('Could not find Manager type "%s" for class "%s".', $type, $class)
);
}

return $manager;
}

protected function getNamespace()
Expand All @@ -147,7 +169,7 @@ private function updateReferences(EventArgs $eventArgs)
$object,
$mapping['identifier'],
$ea->getIdentifier(
$this->getManager($mapping['type']),
$this->getManager($mapping['type'], $mapping['class']),
$referencedObject
)
);
Expand All @@ -170,7 +192,7 @@ public function updateManyEmbedReferences(EventArgs $eventArgs)
$property->setAccessible(true);

$id = $ea->extractIdentifier($om, $object);
$manager = $this->getManager('document');
$manager = $this->getManager('document', $mapping['class']);

$class = $mapping['class'];
$refMeta = $manager->getClassMetadata($class);
Expand Down
20 changes: 17 additions & 3 deletions tests/Gedmo/References/ReferencesListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

class ReferencesListenerTest extends BaseTestCaseOM
{
private $em;
private $dm;
private $em;

protected function setUp()
{
Expand All @@ -26,10 +26,12 @@ protected function setUp()

$reader = new AnnotationReader();

$documentRegistry = $this->getMockRegistry();
$this->dm = $this->getMockDocumentManager('test', new MongoDBAnnotationDriver($reader, __DIR__.'/Fixture/ODM/MongoDB'));
$documentRegistry->expects($this->any())->method('getManagerForClass')->willReturn($this->dm);

$listener = new ReferencesListener(array(
'document' => $this->dm,
'document' => $documentRegistry,
));

$this->evm->addEventSubscriber($listener);
Expand All @@ -43,7 +45,11 @@ protected function setUp()
),
new ORMAnnotationDriver($reader, __DIR__.'/Fixture/ORM')
);
$listener->registerManager('entity', $this->em);

$entityRegistry = $this->getMockRegistry();
$entityRegistry->expects($this->any())->method('getManagerForClass')->willReturn($this->em);

$listener->setRegistry('entity', $entityRegistry);
}

public function testShouldPersistReferencedIdentifiersIntoIdentifierField()
Expand Down Expand Up @@ -180,4 +186,12 @@ public function testShouldPopulateReferenceManyEmbedWithLazyCollectionInstance()
$this->assertInstanceOf(get_class($samsungTV), $last);
$this->assertEquals('Samsung TV', $last->getName());
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|\Doctrine\Common\Persistence\ManagerRegistry
*/
private function getMockRegistry()
{
return $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
}
}

0 comments on commit 54b19d5

Please sign in to comment.