diff --git a/Command/FixturesLoadCommand.php b/Command/FixturesLoadCommand.php index 6aaebef..bb90605 100644 --- a/Command/FixturesLoadCommand.php +++ b/Command/FixturesLoadCommand.php @@ -181,7 +181,7 @@ EOT list($name, $defaultConfig) = $this->getConnection($input, $output); if ('yml' === $type) { - $loader = new YamlDataLoader($this->getApplication()->getKernel()->getRootDir()); + $loader = new YamlDataLoader($this->getApplication()->getKernel()->getRootDir(), $this->getContainer()); } elseif ('xml' === $type) { $loader = new XmlDataLoader($this->getApplication()->getKernel()->getRootDir()); } else { diff --git a/DataFixtures/Loader/YamlDataLoader.php b/DataFixtures/Loader/YamlDataLoader.php index b21138f..69cb3e3 100644 --- a/DataFixtures/Loader/YamlDataLoader.php +++ b/DataFixtures/Loader/YamlDataLoader.php @@ -10,6 +10,8 @@ namespace Propel\PropelBundle\DataFixtures\Loader; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\Yaml\ParseException; use Symfony\Component\Yaml\Yaml; /** @@ -19,14 +21,19 @@ use Symfony\Component\Yaml\Yaml; */ class YamlDataLoader extends AbstractDataLoader { + /** + * @var \Symfony\Component\DependencyInjection\ContainerInterface + */ + private $container; + /** * {@inheritdoc} */ - public function __construct($rootDir) + public function __construct($rootDir, ContainerInterface $container = null) { parent::__construct($rootDir); - Yaml::enablePhpParsing(true); + $this->container = $container; } /** @@ -34,6 +41,38 @@ class YamlDataLoader extends AbstractDataLoader */ protected function transformDataToArray($file) { + if (strpos($file, "\n") === false && is_file($file)) { + if (false === is_readable($file)) { + throw new ParseException(sprintf('Unable to parse "%s" as the file is not readable.', $file)); + } + + if (null !== $this->container && $this->container->has('faker.generator')) { + $generator = $this->container->get('faker.generator'); + $faker = function($type) use ($generator) { + $args = func_get_args(); + array_shift($args); + + echo Yaml::dump(call_user_func_array(array($generator, $type), $args)) . "\n"; + }; + } else { + $faker = function($text) { + echo $text . "\n"; + }; + } + + ob_start(); + $retval = include($file); + $content = ob_get_clean(); + + // if an array is returned by the config file assume it's in plain php form else in YAML + $file = is_array($retval) ? $retval : $content; + + // if an array is returned by the config file assume it's in plain php form else in YAML + if (is_array($file)) { + return $file; + } + } + return Yaml::parse($file); } } diff --git a/Resources/doc/README.markdown b/Resources/doc/README.markdown index 34fe1a2..ea47237 100644 --- a/Resources/doc/README.markdown +++ b/Resources/doc/README.markdown @@ -298,6 +298,19 @@ is a timestamp. Once done, you will be able to load this files by using the `propel:fixtures:load` command. +#### Use Faker in YAML fixtures #### + +If you use [Faker](https://github.com/fzaninotto/Faker) with its [Symfony2 integration](https://github.com/willdurand/BazingaFakerBundle), +then the PropelBundle offers a facility to use the Faker generator in your YAML files. + +``` yml +My\Bundle\Model\MyClass: + mc1: + name: "Awesome Feature" + description: +``` + + ### Graphviz ### You can generate **Graphviz** file for your project by using the following command line: diff --git a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php index 954ead9..d1645b3 100644 --- a/Tests/DataFixtures/Loader/YamlDataLoaderTest.php +++ b/Tests/DataFixtures/Loader/YamlDataLoaderTest.php @@ -176,4 +176,57 @@ YAML; $author = $authors[0]; $this->assertEquals('to be announced', $author->getName()); } + + public function testLoadWithoutFaker() + { + $fixtures = << + +YAML; + $filename = $this->getTempFile($fixtures); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader'); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con); + $this->assertCount(0, $books); + + $authors = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthorPeer::doSelect(new \Criteria(), $this->con); + $this->assertCount(1, $authors); + + $author = $authors[0]; + $this->assertEquals('word', $author->getName()); + } + + public function testLoadWithFaker() + { + $fixtures = << + description: + +YAML; + $filename = $this->getTempFile($fixtures); + $container = $this->getContainer(); + $container->set('faker.generator', \Faker\Factory::create()); + + $loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $container); + $loader->load(array($filename), 'default'); + + $books = \Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con); + $this->assertCount(1, $books); + + $book = $books[0]; + $this->assertNotNull($book->getName()); + $this->assertNotEquals('null', strtolower($book->getName())); + $this->assertRegexp('#[a-z]+#', $book->getName()); + $this->assertNotNull($book->getDescription()); + $this->assertNotEquals('null', strtolower($book->getDescription())); + $this->assertRegexp('#[\w ]+#', $book->getDescription()); + } } diff --git a/Tests/DataFixtures/TestCase.php b/Tests/DataFixtures/TestCase.php index 08c8508..3637cf9 100644 --- a/Tests/DataFixtures/TestCase.php +++ b/Tests/DataFixtures/TestCase.php @@ -40,6 +40,7 @@ class TestCase extends BaseTestCase + diff --git a/composer.json b/composer.json index 3299c19..530fa0e 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ "propel/propel1": "1.6.*" }, "require-dev": { - "sensio/framework-extra-bundle": "dev-master" + "sensio/framework-extra-bundle": "dev-master", + "fzaninotto/faker": "dev-master" }, "autoload": { "psr-0": { "Propel\\PropelBundle": "" }