1
0
mirror of synced 2024-12-14 07:06:04 +03:00

Merge pull request #222 from EvanDotPro/DDC-1544

DDC-1544 Unit test / assertions for ResolveTargetEntityListener
This commit is contained in:
Benjamin Eberlei 2011-12-17 14:03:33 -08:00
commit 20c859611a
2 changed files with 138 additions and 7 deletions

View File

@ -19,7 +19,8 @@
namespace Doctrine\ORM\Tools;
use Doctrine\ORM\Events\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
/**
* ResolveTargetEntityListener
@ -67,24 +68,25 @@ class ResolveTargetEntityListener
}
}
private function remapAssocation($classMetadata, $mapping)
private function remapAssociation($classMetadata, $mapping)
{
$newMapping = $this->resolveTargetEntities[$mapping['targetEntity']];
$newMapping = array_replace_recursive($mapping, $newMapping);
$newMapping['fieldName'] = $mapping['fieldName'];
unset($cm->associationMappings[$mapping['fieldName']]);
unset($classMetadata->associationMappings[$mapping['fieldName']]);
switch ($mapping['type']) {
case ClassMetadata::MANY_TO_MANY:
$cm->mapManyToMany($newMapping);
$classMetadata->mapManyToMany($newMapping);
break;
case ClassMetadata::MANY_TO_ONE:
$cm->mapManyToOne($newMapping);
$classMetadata->mapManyToOne($newMapping);
break;
case ClassMetadata::ONE_TO_MANY:
$cm->mapOneToMany($newMapping);
$classMetadata->mapOneToMany($newMapping);
break;
case ClassMetadata::ONE_TO_ONE:
$cm->mapOneToOne($newMapping);
$classMetadata->mapOneToOne($newMapping);
break;
}
}

View File

@ -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;
}
}