Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Aug 13, 2012
1 parent 4768e06 commit ce6552b
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/DoctrineORMModule/Module.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function onBootstrap(EventInterface $e)
}); });


$config = $app->getServiceManager()->get('Config'); $config = $app->getServiceManager()->get('Config');
$app->getServiceManager()->get('doctrine.entity_resolver.orm_default');


if ( if (
isset($config['zenddevelopertools']['profiler']['enabled']) isset($config['zenddevelopertools']['profiler']['enabled'])
Expand Down
24 changes: 11 additions & 13 deletions src/DoctrineORMModule/Options/EntityResolver.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use InvalidArgumentException; use InvalidArgumentException;
use Zend\Stdlib\AbstractOptions; use Zend\Stdlib\AbstractOptions;


class EntityResolver class EntityResolver extends AbstractOptions
{ {
/** /**
* Set the configuration key for the EventManager. Event manager key * Set the configuration key for the EventManager. Event manager key
Expand All @@ -18,7 +18,7 @@ class EntityResolver


/** /**
* An array that maps a class name (or interface name) to another class * An array that maps a class name (or interface name) to another class
* name (and with an optional mapping) * name
* *
* @var array * @var array
*/ */
Expand Down Expand Up @@ -46,23 +46,21 @@ public function getEventManager()


/** /**
* @param array $resolvers * @param array $resolvers
* @throws \InvalidArgumentException * @throws InvalidArgumentException
*/ */
public function setResolvers(array $resolvers) public function setResolvers(array $resolvers)
{ {
foreach ($resolvers as $entity => $resolveParameters) { foreach ($resolvers as $old => $new) {
if (!isset($resolveParameters['resolved_entity_name'])) { if (!class_exists($new)) {
throw new InvalidArgumentException( throw new InvalidArgumentException(sprintf(
'The resolved entity name for %s has not been set', '%s is resolved to the entity %s, which does not exist',
$entity $old,
$new
)
); );
} }


if (!isset($resolveParameters['mapping'])) { $this->resolvers[$old] = $new;
$resolveParameters['mapping'] = array();
}

$this->resolvers[$entity] = $resolveParameters;
} }
} }


Expand Down
10 changes: 4 additions & 6 deletions src/DoctrineORMModule/Service/EntityResolverFactory.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace DoctrineORMModule\Service; namespace DoctrineORMModule\Service;


use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Events;
use Doctrine\ORM\Tools\ResolveTargetEntityListener; use Doctrine\ORM\Tools\ResolveTargetEntityListener;
use DoctrineModule\Service\AbstractFactory; use DoctrineModule\Service\AbstractFactory;
use Zend\ServiceManager\ServiceLocatorInterface; use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -38,14 +39,11 @@ public function createService(ServiceLocatorInterface $serviceLocator)


$targetEntityListener = new ResolveTargetEntityListener(); $targetEntityListener = new ResolveTargetEntityListener();


foreach ($resolvers as $entity => $resolveParameters) { foreach ($resolvers as $oldEntity => $newEntity) {
$resolvedEntityName = $resolveParameters['resolved_entity_name']; $targetEntityListener->addResolveTargetEntity($oldEntity, $newEntity, array());
$mapping = $resolveParameters['mapping'];

$targetEntityListener->addResolveTargetEntity($entity, $resolvedEntityName, $mapping);
} }


$eventManager->addEventListener($targetEntityListener); $eventManager->addEventListener(Events::loadClassMetadata, $targetEntityListener);


return $eventManager; return $eventManager;
} }
Expand Down
7 changes: 7 additions & 0 deletions tests/Bootstrap.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
__DIR__ . '/DoctrineORMModuleTest/Assets/Entity' __DIR__ . '/DoctrineORMModuleTest/Assets/Entity'
) )
); );
$config['doctrine']['entity_resolver']['orm_default'] = array(
'resolvers' => array(
'DoctrineORMModuleTest\Assets\Entity\TargetInterface' =>
'DoctrineORMModuleTest\Assets\Entity\TargetEntity'
)
);
$config['doctrine']['driver']['orm_default']['drivers']['DoctrineORMModuleTest\Assets\Entity'] = 'test'; $config['doctrine']['driver']['orm_default']['drivers']['DoctrineORMModuleTest\Assets\Entity'] = 'test';
$config['doctrine']['connection']['orm_default'] = array( $config['doctrine']['connection']['orm_default'] = array(
'configuration' => 'orm_default', 'configuration' => 'orm_default',
Expand All @@ -61,4 +67,5 @@
); );


$serviceManager->setService('Config', $config); $serviceManager->setService('Config', $config);
$serviceManager->get('doctrine.entity_resolver.orm_default');
TestCase::setServiceManager($serviceManager); TestCase::setServiceManager($serviceManager);
24 changes: 24 additions & 0 deletions tests/DoctrineORMModuleTest/Assets/Entity/ResolveTarget.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace DoctrineORMModuleTest\Assets\Entity;

use Doctrine\ORM\Mapping as ORM;

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

/**
* @ORM\OneToOne(targetEntity="TargetInterface")
*/
protected $target;
}

18 changes: 18 additions & 0 deletions tests/DoctrineORMModuleTest/Assets/Entity/TargetEntity.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace DoctrineORMModuleTest\Assets\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class TargetEntity implements TargetInterface
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
}
8 changes: 8 additions & 0 deletions tests/DoctrineORMModuleTest/Assets/Entity/TargetInterface.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace DoctrineORMModuleTest\Assets\Entity;

interface TargetInterface
{

}
47 changes: 47 additions & 0 deletions tests/DoctrineORMModuleTest/Service/EntityResolverFactoryTest.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace DoctrineORMModuleTest\Service;

use DoctrineORMModuleTest\Framework\TestCase as TestCase;
use DoctrineORMModule\Service\EntityResolverFactory;
use Doctrine\ORM\Configuration as ORMConfiguration;
use Zend\ServiceManager\ServiceManager;


class EntityResolverFactoryTest extends TestCase
{
/**
* {@inheritDoc}
*/
public function setUp()
{
parent::setUp();
}

public function testCanResolveTargetEntity()
{
$em = $this->getEntityManager();
$classMetadata = $em->getClassMetadata('DoctrineORMModuleTest\Assets\Entity\ResolveTarget');
$meta = $classMetadata->associationMappings;

$this->assertSame('DoctrineORMModuleTest\Assets\Entity\TargetEntity', $meta['target']['targetEntity']);
}
}

0 comments on commit ce6552b

Please sign in to comment.