Skip to content

Commit

Permalink
Merge pull request #40 from gmorel/feature/behat3_multiple_entity_man…
Browse files Browse the repository at this point in the history
…agers

Now work with several entity managers (Behat3)
  • Loading branch information
tyx committed Jan 12, 2016
2 parents 4900914 + d1cfa08 commit c6d706f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Rezzza\AliceExtension\Adapter\ORM;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Fixture\Persistence\ManagerRegistryEventSubscriber;
use Nelmio\Alice\ORM\Doctrine as ORMPersister;
use Rezzza\AliceExtension\Doctrine\ORMPurger;

Expand Down
70 changes: 70 additions & 0 deletions src/Rezzza/AliceExtension/Doctrine/ORMPersister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Rezzza\AliceExtension\Doctrine;

use Doctrine\Common\Persistence\ManagerRegistry;
use Nelmio\Alice\ORM\Doctrine;

class ORMPersister extends Doctrine
{
protected $registry;
protected $flush;

public function __construct(ManagerRegistry $registry, $doFlush = true)
{
$this->registry = $registry;
$this->flush = $doFlush;
}

/**
* {@inheritDoc}
*/
public function persist(array $objects)
{
$managersToFlush = [];

foreach ($objects as $object) {
$managerName = $this->findManagerForClass(get_class($object));
$this->registry->getManager($managerName)->persist($object);

$managersToFlush[$managerName] = $managerName;
}

if ($this->flush) {
foreach ($managersToFlush as $managerName) {
$this->registry->getManager($managerName)->flush();
}
}
}

/**
* {@inheritDoc}
*/
public function find($class, $id)
{
$managerName = $this->findManagerForClass($class);
$entity = $this->registry->getManager($managerName)->find($class, $id);

if (!$entity) {
throw new \UnexpectedValueException('Entity with Id ' . $id . ' and Class ' . $class . ' not found');
}

return $entity;
}

/**
* @param string $entityClass
* @return string
* @throws \Doctrine\Common\Persistence\Mapping\MappingException
*/
private function findManagerForClass($entityClass)
{
foreach ($this->registry->getManagers() as $name => $manager) {
if ($manager->getMetadataFactory()->hasMetadataFor($entityClass)) {
return $name;
}
}

throw new \Doctrine\Common\Persistence\Mapping\MappingException(sprintf('Entity class "%s" not found in managers.', $entityClass));
}
}
53 changes: 18 additions & 35 deletions src/Rezzza/AliceExtension/Doctrine/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

namespace Rezzza\AliceExtension\Doctrine;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Internal\CommitOrderCalculator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Platforms\MySqlPlatform;

/**
* Class responsible for purging databases of data before reloading data fixtures.
Expand All @@ -35,8 +35,8 @@ class ORMPurger
const PURGE_MODE_DELETE = 1;
const PURGE_MODE_TRUNCATE = 2;

/** EntityManager instance used for persistence. */
private $em;
/** ManagerRegistry instance used for persistence. */
private $registry;

/**
* If the purge should be done through DELETE or TRUNCATE statements
Expand All @@ -50,9 +50,9 @@ class ORMPurger
*
* @param EntityManager $em EntityManager instance used for persistence.
*/
public function __construct(EntityManager $em = null)
public function __construct(ManagerRegistry $registry)
{
$this->em = $em;
$this->registry = $registry;
}

/**
Expand All @@ -76,38 +76,21 @@ public function getPurgeMode()
return $this->purgeMode;
}

/**
* Set the EntityManager instance this purger instance should use.
*
* @param EntityManager $em
*/
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}

/**
* Retrieve the EntityManager instance this purger instance is using.
*
* @return \Doctrine\ORM\EntityManager
*/
public function getObjectManager()
/** @inheritDoc */
public function purge()
{
return $this->em;
foreach ($this->registry->getManagers() as $manager) {
$this->purgeManager($manager);
}
}

/** @inheritDoc */
public function purge()
private function purgeManager(EntityManager $entityManager)
{
$metadatas = $this->em->getMetadataFactory()->getAllMetadata();
$platform = $this->em->getConnection()->getDatabasePlatform();
$metadatas = $entityManager->getMetadataFactory()->getAllMetadata();
$platform = $entityManager->getConnection()->getDatabasePlatform();

$tables = array();
foreach ($metadatas as $metadata) {
if (true === $metadata->isEmbeddedClass) {
continue;
}

if (!$metadata->isMappedSuperclass) {
$tables[] = $metadata->getQuotedTableName($platform);
}
Expand All @@ -121,20 +104,20 @@ public function purge()

// implements hack for Mysql
if ($platform instanceof MySqlPlatform) {
$this->em->getConnection()->exec('SET foreign_key_checks = 0;');
$entityManager->getConnection()->exec('SET foreign_key_checks = 0;');
}

foreach ($tables as $tbl) {
if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$this->em->getConnection()->executeUpdate("DELETE IGNORE FROM " . $tbl);
$entityManager->getConnection()->executeUpdate("DELETE IGNORE FROM " . $tbl);
} else {
$this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
$entityManager->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
}
}

// implements hack for Mysql
if ($platform instanceof MySqlPlatform) {
$this->em->getConnection()->exec('SET foreign_key_checks = 1;');
$entityManager->getConnection()->exec('SET foreign_key_checks = 1;');
}
}
}
10 changes: 3 additions & 7 deletions src/Rezzza/AliceExtension/Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parameter key="behat.alice.subscriber_factory.registry.class">Rezzza\AliceExtension\Adapter\SubscriberFactoryRegistry</parameter>
<parameter key="behat.alice.elastica_subscriber.factory.class">Rezzza\AliceExtension\Adapter\Elastica\ElasticaSubscriberFactory</parameter>
<parameter key="behat.alice.elastica.persister.class">Rezzza\AliceExtension\Adapter\Elastica\Persister</parameter>
<parameter key="behat.alice.orm.persister.class">Nelmio\Alice\ORM\Doctrine</parameter>
<parameter key="behat.alice.orm.persister.class">Rezzza\AliceExtension\Doctrine\ORMPersister</parameter>
<parameter key="behat.alice.orm.purger.class">Rezzza\AliceExtension\Doctrine\ORMPurger</parameter>
</parameters>

Expand Down Expand Up @@ -61,10 +61,6 @@
<argument>alice_extension.processor.registry</argument>
</service>

<service id="sf2.doctrine.orm.entity_manager" class="%sf2.doctrine.orm.entity_manager.class%" factory-service="behat.alice.container_proxy" factory-method="get">
<argument>doctrine.orm.default_entity_manager</argument>
</service>

<service id="sf2.doctrine" class="%sf2.doctrine.class%" factory-service="behat.alice.container_proxy" factory-method="get">
<argument>doctrine</argument>
</service>
Expand Down Expand Up @@ -110,11 +106,11 @@
</service>

<service id="behat.alice.orm.persister" class="%behat.alice.orm.persister.class%">
<argument type="service" id="sf2.doctrine.orm.entity_manager" />
<argument type="service" id="sf2.doctrine" />
</service>

<service id="behat.alice.orm.purger" class="%behat.alice.orm.purger.class%">
<argument type="service" id="sf2.doctrine.orm.entity_manager" />
<argument type="service" id="sf2.doctrine" />
</service>
</services>
</container>

0 comments on commit c6d706f

Please sign in to comment.