Skip to content
This repository has been archived by the owner on Aug 28, 2018. It is now read-only.

Commit

Permalink
Add management of multiple databases
Browse files Browse the repository at this point in the history
  • Loading branch information
Moulino committed Nov 6, 2014
1 parent a4ffd81 commit 926513c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
12 changes: 11 additions & 1 deletion Command/FixturesLoadCommand.php
Expand Up @@ -25,6 +25,12 @@ protected function configure()
InputOption::VALUE_NONE,
'If set, will purge the database before importing new fixtures'
)
->addOption(
'database-name',
null,
InputArgument::OPTIONAL,
'If set, will purge the database specified. If not set, will purge all databases. (require purge option)'
)
->addOption(
'purge-mongodb',
null,
Expand All @@ -46,11 +52,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($input->getOption('purge-orm')) {
$this->getContainer()->get('khepin.yaml_loader')->purgeDatabase(
'orm',
$input->getOption('database-name'),
$input->getOption('purge-with-truncate')
);
}
if ($input->getOption('purge-mongodb')) {
$this->getContainer()->get('khepin.yaml_loader')->purgeDatabase('mongodb');
$this->getContainer()->get('khepin.yaml_loader')->purgeDatabase(
'mongodb',
$input->getOption('database-name')
);
}

$this->getContainer()->get('khepin.yaml_loader')->loadFixtures($context);
Expand Down
42 changes: 25 additions & 17 deletions Loader/YamlLoader.php
Expand Up @@ -107,9 +107,13 @@ public function loadFixtures()
$fixture_data = Yaml::parse($file);
// if nothing is specified, we use doctrine orm for persistence
$persistence = isset($fixture_data['persistence']) ? $fixture_data['persistence'] : 'orm';

$persister = $this->getPersister($persistence);
$manager = $persister->getManagerForClass($fixture_data['model']);

$fixture = $this->getFixtureClass($persistence);
$fixture = new $fixture($fixture_data, $this, $file);
$fixture->load($this->getManager($persistence), func_get_args());
$fixture->load($manager, func_get_args());
}

if (!is_null($this->acl_manager)) {
Expand All @@ -123,7 +127,7 @@ public function loadFixtures()
/**
* Remove all fixtures from the database
*/
public function purgeDatabase($persistence, $withTruncate = false)
public function purgeDatabase($persistence, $databaseName = null, $withTruncate = false)
{
$purgetools = array(
'orm' => array(
Expand All @@ -138,32 +142,36 @@ public function purgeDatabase($persistence, $withTruncate = false)
// Retrieve the correct purger and executor
$purge_class = $purgetools[$persistence]['purger'];
$executor_class = $purgetools[$persistence]['executor'];
// Instanciate purger and executor
$purger = new $purge_class($this->getManager($persistence));

// Check if the purger supports setting the purge mode
if ($withTruncate && $purger instanceof ORMPurger) {
$purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE);
// Instanciate purger and executor
$persister = $this->getPersister($persistence);
$entityManagers = ($databaseName)
? array($persister->getManager($databaseName))
: $persister->getManagers();

foreach($entityManagers as $entityManager) {
$purger = new $purge_class($entityManager);
if ($withTruncate && $purger instanceof ORMPurger) {
$purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE);
}
$executor = new $executor_class($entityManager, $purger);
// purge
$executor->purge();
}

$executor = new $executor_class($this->getManager($persistence), $purger);
// purge
$executor->purge();
}

/**
* Returns a doctrine object manager for the given persistence layer
* @return ObjectManager
/*
* Returns the doctrine persister for the given persistence layer
* @return ManagerRegistry
*/
public function getManager($persistence)
public function getPersister($persistence)
{
$managers = array(
'orm' => 'doctrine',
'mongodb' => 'doctrine_mongodb',
);

return $this->kernel->getContainer()
->get($managers[$persistence])->getManager();
return $this->kernel->getContainer()->get($managers[$persistence]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Khepin/Tests/YamlFixtureTest.php
Expand Up @@ -124,7 +124,7 @@ public function testPurgeWithTruncate()
$firstCar = $cars[0];
$firstCarIdBefore = $firstCar->getId();

$loader->purgeDatabase('orm', true);
$loader->purgeDatabase('orm', null, true);

$cars = $this->doctrine->getEntityManager()->getRepository('Khepin\Fixture\Entity\Car')->findAll();
$this->assertEmpty($cars);
Expand Down
6 changes: 5 additions & 1 deletion tests/Khepin/Utils/BaseTestCaseMongo.php
Expand Up @@ -62,7 +62,11 @@ protected function getDoctrine()
$config = $this->getMockAnnotatedConfig();
$dm = \Doctrine\ODM\MongoDB\DocumentManager::create(null, $config);

return $this->doctrine = m::mock(array('getManager' => $dm));
return $this->doctrine = m::mock(array(
'getManager' => $dm,
'getManagers' => array($dm),
'getManagerForClass' => $dm
));

// $conn = array(
// 'driver' => 'pdo_sqlite',
Expand Down
8 changes: 5 additions & 3 deletions tests/Khepin/Utils/BaseTestCaseOrm.php
Expand Up @@ -99,7 +99,7 @@ protected function getDoctrine()
$conn = array(
'driver' => 'pdo_sqlite',
'memory' => true,
// 'path' => __DIR__.'/../db.sqlite',
//'path' => __DIR__.'/../db.sqlite',
);

$config = $this->getMockAnnotatedConfig();
Expand All @@ -120,8 +120,10 @@ protected function getDoctrine()
$schemaTool->createSchema($schema);

return $this->doctrine = m::mock(array(
'getEntityManager' => $em,
'getManager' => $em,
'getEntityManager' => $em,
'getManager' => $em,
'getManagers' => array($em),
'getManagerForClass' => $em
)
);
}
Expand Down

0 comments on commit 926513c

Please sign in to comment.