Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fixture load paths to be configured in the bundle configuration #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions Command/LoadDataFixturesDoctrineCommand.php
Expand Up @@ -95,6 +95,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if ($this->getContainer()->hasParameter('doctrine_fixtures.paths')) {
$paths = array_merge(
$paths,
$this->getContainer()->getParameter('doctrine_fixtures.paths')
);
}

$loader = new DataFixturesLoader($this->getContainer());
foreach ($paths as $path) {
if (is_dir($path)) {
Expand Down
40 changes: 40 additions & 0 deletions DependencyInjection/Configuration.php
@@ -0,0 +1,40 @@
<?php

namespace Doctrine\Bundle\FixturesBundle\DependencyInjection;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;

/**
* @author Tyler Stroud <tyler@tylerstroud.com>
*
*/
class Configuration implements ConfigurationInterface
{
/**
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('doctrine_fixtures', 'array');

$this->addPathsConfiguration($rootNode);

return $treeBuilder;
}

/**
* @param NodeDefinition $root
*/
protected function addPathsConfiguration(NodeDefinition $root)
{
$root
->children()
->arrayNode('paths')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss a brief info() section here explaining the expected values. Example:

->arrayNode('paths')
    ->info('An array of relative or absolute paths of the dirs where fixtures are defined. Directory bundle notation is supported too.')

->info('An array of paths to where your fixtures are located.')
->prototype('scalar')->end()
->end();
}
}
27 changes: 27 additions & 0 deletions DependencyInjection/DoctrineFixturesExtension.php
@@ -0,0 +1,27 @@
<?php

namespace Doctrine\Bundle\FixturesBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;

/**
* @author Tyler Stroud <tyler@tylerstroud.com>
*/
class DoctrineFixturesExtension extends Extension
{
/**
* @param array $configs
* @param ContainerBuilder $container
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

if (isset($config['paths'])) {
$container->setParameter('doctrine_fixtures.paths', $config['paths']);
}
}
}

19 changes: 17 additions & 2 deletions Resources/doc/index.rst
Expand Up @@ -54,13 +54,28 @@ bundle only for the ``dev`` and ``test`` environments:
// ...
}

Step 3 (Optional): Configure Fixture Paths
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If your fixtures live in a directory outside of the default bundle location,
you will need to specify the path to them:

.. code-block:: yaml

// app/config/config.yml

doctrine_fixtures:
paths:
- /path/to/fixtures
- /path/to/more/fixtures

Writing Simple Fixtures
-----------------------

Doctrine2 fixtures are PHP classes where you can create objects and persist
them to the database. Like all classes in Symfony, fixtures should live inside
one of your application bundles.
them to the database.

If you did not configure a custom location for your fixtures, they are expected to be inside your bundles.
For a bundle located at ``src/AppBundle``, the fixture classes should live inside
``src/AppBundle/DataFixtures/ORM`` or ``src/AppBundle/DataFixtures/MongoDB``
respectively for the ORM and ODM. This tutorial assumes that you are using the ORM,
Expand Down
36 changes: 36 additions & 0 deletions Tests/DependencyInjection/DoctrineFixturesExtensionTest.php
@@ -0,0 +1,36 @@
<?php

namespace Doctrine\Bundle\FixturesBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Tyler Stroud <tyler@tylerstroud.com>
*/
class DoctrineFixturesExtensionTest extends \PHPUnit_Framework_TestCase
{
protected $container;
protected $extension;

public function setUp()
{
$this->container = new ContainerBuilder();
$this->extension = new DoctrineFixturesExtension();
}

public function testPathsConfiguration()
{
$config = [
'doctrine_fixtures' => [
'paths' => [
'/path1/',
'/path2/'
]
]
];

$this->extension->load($config, $this->container);
$this->assertTrue($this->container->hasParameter('doctrine_fixtures.paths'));
$this->assertEquals(['/path1/', '/path2/'], $this->container->getParameter('doctrine_fixtures.paths'));
}
}
3 changes: 3 additions & 0 deletions Tests/bootstrap.php
@@ -0,0 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';