Merge pull request #222 from EvanDotPro/DDC-1544
DDC-1544 Unit test / assertions for ResolveTargetEntityListener
This commit is contained in:
commit
20c859611a
@ -19,7 +19,8 @@
|
|||||||
|
|
||||||
namespace Doctrine\ORM\Tools;
|
namespace Doctrine\ORM\Tools;
|
||||||
|
|
||||||
use Doctrine\ORM\Events\LoadClassMetadataEventArgs;
|
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
|
||||||
|
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ResolveTargetEntityListener
|
* ResolveTargetEntityListener
|
||||||
@ -67,24 +68,25 @@ class ResolveTargetEntityListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function remapAssocation($classMetadata, $mapping)
|
private function remapAssociation($classMetadata, $mapping)
|
||||||
{
|
{
|
||||||
$newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
|
$newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
|
||||||
|
$newMapping = array_replace_recursive($mapping, $newMapping);
|
||||||
$newMapping['fieldName'] = $mapping['fieldName'];
|
$newMapping['fieldName'] = $mapping['fieldName'];
|
||||||
unset($cm->associationMappings[$mapping['fieldName']]);
|
unset($classMetadata->associationMappings[$mapping['fieldName']]);
|
||||||
|
|
||||||
switch ($mapping['type']) {
|
switch ($mapping['type']) {
|
||||||
case ClassMetadata::MANY_TO_MANY:
|
case ClassMetadata::MANY_TO_MANY:
|
||||||
$cm->mapManyToMany($newMapping);
|
$classMetadata->mapManyToMany($newMapping);
|
||||||
break;
|
break;
|
||||||
case ClassMetadata::MANY_TO_ONE:
|
case ClassMetadata::MANY_TO_ONE:
|
||||||
$cm->mapManyToOne($newMapping);
|
$classMetadata->mapManyToOne($newMapping);
|
||||||
break;
|
break;
|
||||||
case ClassMetadata::ONE_TO_MANY:
|
case ClassMetadata::ONE_TO_MANY:
|
||||||
$cm->mapOneToMany($newMapping);
|
$classMetadata->mapOneToMany($newMapping);
|
||||||
break;
|
break;
|
||||||
case ClassMetadata::ONE_TO_ONE:
|
case ClassMetadata::ONE_TO_ONE:
|
||||||
$cm->mapOneToOne($newMapping);
|
$classMetadata->mapOneToOne($newMapping);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,129 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Doctrine\Tests\ORM\Tools;
|
||||||
|
|
||||||
|
use Doctrine\ORM\Events;
|
||||||
|
use Doctrine\ORM\Mapping\ClassMetadataFactory;
|
||||||
|
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../TestInit.php';
|
||||||
|
|
||||||
|
class ResolveTargetEntityListenerTest extends \Doctrine\Tests\OrmTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var EntityManager
|
||||||
|
*/
|
||||||
|
private $em = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ResolveTargetEntityListener
|
||||||
|
*/
|
||||||
|
private $listener = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ClassMetadataFactory
|
||||||
|
*/
|
||||||
|
private $factory = null;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$annotationDriver = $this->createAnnotationDriver();
|
||||||
|
|
||||||
|
$this->em = $this->_getTestEntityManager();
|
||||||
|
$this->em->getConfiguration()->setMetadataDriverImpl($annotationDriver);
|
||||||
|
$this->factory = new ClassMetadataFactory;
|
||||||
|
$this->factory->setEntityManager($this->em);
|
||||||
|
$this->listener = new ResolveTargetEntityListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-1544
|
||||||
|
*/
|
||||||
|
public function testResolveTargetEntityListenerCanResolveTargetEntity()
|
||||||
|
{
|
||||||
|
$evm = $this->em->getEventManager();
|
||||||
|
$this->listener->addResolveTargetEntity(
|
||||||
|
'Doctrine\Tests\ORM\Tools\ResolveTargetInterface',
|
||||||
|
'Doctrine\Tests\ORM\Tools\ResolveTargetEntity',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
$this->listener->addResolveTargetEntity(
|
||||||
|
'Doctrine\Tests\ORM\Tools\TargetInterface',
|
||||||
|
'Doctrine\Tests\ORM\Tools\TargetEntity',
|
||||||
|
array()
|
||||||
|
);
|
||||||
|
$evm->addEventListener(Events::loadClassMetadata, $this->listener);
|
||||||
|
$cm = $this->factory->getMetadataFor('Doctrine\Tests\ORM\Tools\ResolveTargetEntity');
|
||||||
|
$meta = $cm->associationMappings;
|
||||||
|
$this->assertSame('Doctrine\Tests\ORM\Tools\TargetEntity', $meta['manyToMany']['targetEntity']);
|
||||||
|
$this->assertSame('Doctrine\Tests\ORM\Tools\ResolveTargetEntity', $meta['manyToOne']['targetEntity']);
|
||||||
|
$this->assertSame('Doctrine\Tests\ORM\Tools\ResolveTargetEntity', $meta['oneToMany']['targetEntity']);
|
||||||
|
$this->assertSame('Doctrine\Tests\ORM\Tools\TargetEntity', $meta['oneToOne']['targetEntity']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ResolveTargetInterface
|
||||||
|
{
|
||||||
|
public function getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
interface TargetInterface extends ResolveTargetInterface
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Entity
|
||||||
|
*/
|
||||||
|
class ResolveTargetEntity implements ResolveTargetInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(type="integer")
|
||||||
|
* @GeneratedValue(strategy="AUTO")
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ManyToMany(targetEntity="Doctrine\Tests\ORM\Tools\TargetInterface")
|
||||||
|
*/
|
||||||
|
private $manyToMany;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ManyToOne(targetEntity="Doctrine\Tests\ORM\Tools\ResolveTargetInterface", inversedBy="oneToMany")
|
||||||
|
*/
|
||||||
|
private $manyToOne;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OneToMany(targetEntity="Doctrine\Tests\ORM\Tools\ResolveTargetInterface", mappedBy="manyToOne")
|
||||||
|
*/
|
||||||
|
private $oneToMany;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @OneToOne(targetEntity="Doctrine\Tests\ORM\Tools\TargetInterface")
|
||||||
|
* @JoinColumn(name="target_entity_id", referencedColumnName="id")
|
||||||
|
*/
|
||||||
|
private $oneToOne;
|
||||||
|
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Entity
|
||||||
|
*/
|
||||||
|
class TargetEntity implements TargetInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(type="integer")
|
||||||
|
* @GeneratedValue(strategy="AUTO")
|
||||||
|
*/
|
||||||
|
private $id;
|
||||||
|
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return $this->id;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user