diff --git a/src/Context/Doctrine/AbstractAliceContext.php b/src/Context/Doctrine/AbstractAliceContext.php index f002daa..ccece28 100644 --- a/src/Context/Doctrine/AbstractAliceContext.php +++ b/src/Context/Doctrine/AbstractAliceContext.php @@ -34,31 +34,11 @@ abstract class AbstractAliceContext implements KernelAwareContext, AliceContextI */ protected $basePath; - /** - * @var string[] - */ - protected $classes; - - /** - * @var FixturesFinderInterface - */ - protected $fixturesFinder; - /** * @var KernelInterface */ protected $kernel; - /** - * @var LoaderInterface - */ - protected $loader; - - /** - * @var PersisterInterface - */ - protected $persister; - /** * @param string|null $basePath */ @@ -68,36 +48,13 @@ public function __construct($basePath = null) } /** - * @param KernelInterface $kernel - * @param FixturesFinderInterface $fixturesFinder - * @param LoaderInterface $loader - * @param PersisterInterface|ObjectManager $persister - * @param string $basePath + * {@inheritdoc} */ - final public function init( - KernelInterface $kernel, - FixturesFinderInterface $fixturesFinder, - LoaderInterface $loader, - PersisterInterface $persister, - $basePath = null - ) { + public function setKernel(KernelInterface $kernel) + { $this->kernel = $kernel; - $this->fixturesFinder = $fixturesFinder; - $this->loader = $loader; - $this->persister = $persister; - - if (null !== $basePath) { - $this->basePath = $basePath; - } } - /** - * {@inheritdoc} - * - * @return $this - */ - abstract public function setKernel(KernelInterface $kernel); - /** * {@inheritdoc} */ @@ -167,7 +124,7 @@ public function thereAreSeveralFixtures(TableNode $fixturesFileRows, $persister private function loadFixtures($fixturesFiles, $persister = null) { if (null === $persister) { - $persister = $this->persister; + $persister = $this->getPersister(); } if (true === is_string($persister)) { @@ -202,20 +159,20 @@ private function loadFixtures($fixturesFiles, $persister = null) if (false === empty($fixtureBundles)) { $fixturesFiles = array_merge( $fixturesFiles, - $this->fixturesFinder->getFixtures($this->kernel, $fixtureBundles, $this->kernel->getEnvironment()) + $this->getFixturesFinder()->getFixtures($this->kernel, $fixtureBundles, $this->kernel->getEnvironment()) ); } if (false === empty($fixtureDirectories)) { $fixturesFiles = array_merge( $fixturesFiles, - $this->fixturesFinder->getFixturesFromDirectory($fixtureDirectories) + $this->getFixturesFinder()->getFixturesFromDirectory($fixtureDirectories) ); } - $this->loader->load( + $this->getLoader()->load( $persister, - $this->fixturesFinder->resolveFixtures($this->kernel, $fixturesFiles) + $this->getFixturesFinder()->resolveFixtures($this->kernel, $fixturesFiles) ); } @@ -229,7 +186,7 @@ private function loadFixtures($fixturesFiles, $persister = null) final protected function resolvePersister($persister) { if (null === $persister) { - return $this->persister; + return $this->getPersister(); } switch (true) { @@ -245,4 +202,22 @@ final protected function resolvePersister($persister) )); } } + + /** + * @return LoaderInterface + */ + protected function getLoader() + { + return $this->kernel->getContainer()->get('hautelook_alice.fixtures.loader'); + } + + /** + * @return PersisterInterface + */ + abstract protected function getPersister(); + + /** + * @return FixturesFinderInterface + */ + abstract protected function getFixturesFinder(); } diff --git a/src/Context/Doctrine/AliceODMContext.php b/src/Context/Doctrine/AliceODMContext.php index b16483b..c8707b4 100644 --- a/src/Context/Doctrine/AliceODMContext.php +++ b/src/Context/Doctrine/AliceODMContext.php @@ -11,8 +11,8 @@ namespace Fidry\AliceBundleExtension\Context\Doctrine; +use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\SchemaManager; -use Symfony\Component\HttpKernel\KernelInterface; /** * Context to load fixtures files with Alice loader. @@ -22,51 +22,65 @@ class AliceODMContext extends AbstractAliceContext { /** - * @var SchemaManager + * {@inheritdoc} */ - private $schemaManager; + public function createSchema() + { + $schemaManager = $this->getSchemaManager(); + + $schemaManager->createCollections(); + $schemaManager->ensureIndexes(); + } /** * {@inheritdoc} */ - public function setKernel(KernelInterface $kernel) + public function dropSchema() { - $this->init( - $kernel, - $kernel->getContainer()->get('hautelook_alice.doctrine.mongodb.fixtures_finder'), - $kernel->getContainer()->get('hautelook_alice.fixtures.loader'), - $this->resolvePersister($kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager')) - ); - - $this->schemaManager = $kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager')->getSchemaManager(); + $schemaManager = $this->getSchemaManager(); - return $this; + $schemaManager->deleteIndexes(); + $schemaManager->dropCollections(); } /** * {@inheritdoc} */ - public function createSchema() + public function emptyDatabase() { - $this->schemaManager->createCollections(); - $this->schemaManager->ensureIndexes(); + $this->dropSchema(); + $this->createSchema(); } /** * {@inheritdoc} */ - public function dropSchema() + protected function getPersister() { - $this->schemaManager->deleteIndexes(); - $this->schemaManager->dropCollections(); + return $this->resolvePersister($this->getDocumentManager()); } /** * {@inheritdoc} */ - public function emptyDatabase() + protected function getFixturesFinder() { - $this->dropSchema(); - $this->createSchema(); + return $this->kernel->getContainer()->get('hautelook_alice.doctrine.mongodb.fixtures_finder'); + } + + /** + * @return SchemaManager + */ + private function getSchemaManager() + { + return $this->getDocumentManager()->getSchemaManager(); + } + + /** + * @return DocumentManager + */ + private function getDocumentManager() + { + return $this->kernel->getContainer()->get('doctrine_mongodb.odm.default_document_manager'); } } diff --git a/src/Context/Doctrine/AliceORMContext.php b/src/Context/Doctrine/AliceORMContext.php index 1c04ba7..fffee7f 100644 --- a/src/Context/Doctrine/AliceORMContext.php +++ b/src/Context/Doctrine/AliceORMContext.php @@ -11,8 +11,9 @@ namespace Fidry\AliceBundleExtension\Context\Doctrine; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Tools\SchemaTool; -use Symfony\Component\HttpKernel\KernelInterface; /** * Context to load fixtures files with Alice loader. @@ -22,53 +23,67 @@ class AliceORMContext extends AbstractAliceContext { /** - * @var SchemaTool + * {@inheritdoc} */ - private $schemaTool; + public function createSchema() + { + $this->getSchemaTool()->createSchema($this->getAllMetadatas()); + } /** * {@inheritdoc} */ - public function setKernel(KernelInterface $kernel) + public function dropSchema() { - $this->init( - $kernel, - $kernel->getContainer()->get('hautelook_alice.doctrine.orm.fixtures_finder'), - $kernel->getContainer()->get('hautelook_alice.fixtures.loader'), - $this->resolvePersister($kernel->getContainer()->get('doctrine.orm.entity_manager')) - ); - - /** @var \Doctrine\ORM\EntityManager $entityManager */ - $entityManager = $kernel->getContainer()->get('doctrine.orm.default_entity_manager'); - - $this->schemaTool = new SchemaTool($entityManager); - $this->classes = $entityManager->getMetadataFactory()->getAllMetadata(); - - return $this; + $this->getSchemaTool()->dropSchema($this->getAllMetadatas()); } /** * {@inheritdoc} */ - public function createSchema() + public function emptyDatabase() { - $this->schemaTool->createSchema($this->classes); + $this->dropSchema(); + $this->createSchema(); } /** * {@inheritdoc} */ - public function dropSchema() + protected function getPersister() { - $this->schemaTool->dropSchema($this->classes); + return $this->resolvePersister($this->getEntityManager()); } /** * {@inheritdoc} */ - public function emptyDatabase() + protected function getFixturesFinder() { - $this->dropSchema(); - $this->createSchema(); + return $this->kernel->getContainer()->get('hautelook_alice.doctrine.orm.fixtures_finder'); + } + + /** + * @return SchemaTool + */ + private function getSchemaTool() + { + return new SchemaTool($this->getEntityManager()); + } + + /** + * @return ClassMetadata[] + */ + private function getAllMetadatas() + { + return $this->getEntityManager()->getMetadataFactory()->getAllMetadata(); + } + + /** + * @return EntityManagerInterface + */ + private function getEntityManager() + { + return $this->kernel->getContainer()->get('doctrine.orm.entity_manager'); } }