Skip to content

Commit

Permalink
Add the functionality to configure paths from which fixtures will be …
Browse files Browse the repository at this point in the history
…loaded
  • Loading branch information
tystr committed Apr 25, 2016
1 parent 0f1a2f9 commit 71eae76
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
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')
->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';

0 comments on commit 71eae76

Please sign in to comment.