Skip to content

Commit

Permalink
add configuration for the fixture loader
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasThal authored and alcaeus committed Jan 20, 2016
1 parent 827f3a1 commit 5a1208c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Command/LoadDataFixturesDoctrineODMCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$loader = new ContainerAwareLoader($this->getContainer());
$loaderClass = $this->getContainer()->getParameter('doctrine_mongodb.odm.fixture_loader');
$loader = new $loaderClass($this->getContainer());
foreach ($paths as $path) {
if (is_dir($path)) {
$loader->loadFromDirectory($path);
Expand Down
7 changes: 7 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public function getConfigTreeBuilder()
->scalarNode('hydrator_namespace')->defaultValue('Hydrators')->end()
->scalarNode('hydrator_dir')->defaultValue('%kernel.cache_dir%/doctrine/odm/mongodb/Hydrators')->end()
->scalarNode('auto_generate_hydrator_classes')->defaultValue(false)->end()
->scalarNode('fixture_loader')
->defaultValue('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader')
->beforeNormalization()
->ifTrue(function($v) {return !($v == 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader' || in_array('Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader', class_parents($v)));})
->then(function($v) { throw new \LogicException(sprintf("The %s class is not a subclass of the ContainerAwareLoader", $v));})
->end()
->end()
->scalarNode('default_document_manager')->end()
->scalarNode('default_connection')->end()
->scalarNode('default_database')->defaultValue('default')->end()
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public function load(array $configs, ContainerBuilder $container)
// set some options as parameters and unset them
$config = $this->overrideParameters($config, $container);

// set the fixtures loader
$container->setParameter('doctrine_mongodb.odm.fixture_loader', $config['fixture_loader']);

// load the connections
$this->loadConnections($config['connections'], $container);

Expand Down
27 changes: 27 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function testDefaults()
$options = $processor->processConfiguration($configuration, array());

$defaults = array(
'fixture_loader' => 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader',
'auto_generate_hydrator_classes' => false,
'auto_generate_proxy_classes' => false,
'default_database' => 'default',
Expand All @@ -54,6 +55,7 @@ public function testFullConfiguration($config)
$options = $processor->processConfiguration($configuration, array($config));

$expected = array(
'fixture_loader' => 'Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader',
'auto_generate_hydrator_classes' => true,
'auto_generate_proxy_classes' => true,
'default_connection' => 'conn1',
Expand Down Expand Up @@ -440,4 +442,29 @@ public function testInvalidReplicaSetValue()
$configuration = new Configuration(false);
$processor->processConfiguration($configuration, array($config));
}

/**
* @dataProvider provideExceptionConfiguration
*/
public function testFixtureLoaderValidation($config)
{
$processor = new Processor();
$configuration = new Configuration(false);
$this->setExpectedException('\LogicException');
$processor->processConfiguration($configuration, array($config));
}

public function provideExceptionConfiguration()
{
$yaml = Yaml::parse(file_get_contents(__DIR__.'/Fixtures/config/yml/exception.yml'));
$yaml = $yaml['doctrine_mongodb'];

$xml = XmlUtils::loadFile(__DIR__.'/Fixtures/config/xml/exception.xml');
$xml = XmlUtils::convertDomElementToArray($xml->getElementsByTagName('config')->item(0));

return array(
array($yaml),
array($xml),
);
}
}
13 changes: 13 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/xml/exception.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:doctrine="http://symfony.com/schema/dic/doctrine/odm/mongodb"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/doctrine/odm/mongodb http://symfony.com/schema/dic/doctrine/odm/mongodb/mongodb-1.0.xsd">

<doctrine:config
fixture_loader="stdClass"
>
</doctrine:config>
</container>
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/xml/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
http://symfony.com/schema/dic/doctrine/odm/mongodb http://symfony.com/schema/dic/doctrine/odm/mongodb/mongodb-1.0.xsd">

<doctrine:config
fixture_loader="Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader"
auto-generate-hydrator-classes="true"
auto-generate-proxy-classes="true"
default-connection="conn1"
Expand Down
2 changes: 2 additions & 0 deletions Tests/DependencyInjection/Fixtures/config/yml/exception.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
doctrine_mongodb:
fixture_loader: stdClass
1 change: 1 addition & 0 deletions Tests/DependencyInjection/Fixtures/config/yml/full.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
doctrine_mongodb:
fixture_loader: Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader
auto_generate_proxy_classes: true
auto_generate_hydrator_classes: true
default_connection: conn1
Expand Down

0 comments on commit 5a1208c

Please sign in to comment.