Permalink
Browse files

Merge pull request #151 from willdurand/add-faker-support

Added faker support
  • Loading branch information...
2 parents 6a648ed + ca45e9c commit c5d23ec44a8a0ac70861b5f49e1d7542c8178532 @willdurand willdurand committed May 9, 2012
@@ -181,7 +181,7 @@ protected function loadFixtures(InputInterface $input, OutputInterface $output,
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 {
@@ -10,6 +10,8 @@
namespace Propel\PropelBundle\DataFixtures\Loader;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Yaml\ParseException;
use Symfony\Component\Yaml\Yaml;
/**
@@ -20,20 +22,57 @@
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;
}
/**
* {@inheritdoc}
*/
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);
}
}
@@ -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: <?php $faker('text', 500); ?>
+```
+
+
### Graphviz ###
You can generate **Graphviz** file for your project by using the following command line:
@@ -176,4 +176,57 @@ public function testLoaderWithPhp()
$author = $authors[0];
$this->assertEquals('to be announced', $author->getName());
}
+
+ public function testLoadWithoutFaker()
+ {
+ $fixtures = <<<YAML
+Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookAuthor:
+ BookAuthor_1:
+ id: '1'
+ name: <?php echo \$faker('word'); ?>
+
+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 = <<<YAML
+Propel\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book:
+ Book_1:
+ id: '1'
+ name: <?php \$faker('word'); ?>
+ description: <?php \$faker('sentence'); ?>
+
+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());
+ }
}
@@ -40,6 +40,7 @@ protected function setUp()
<table name="book">
<column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" />
+ <column name="description" type="varchar" />
<column name="author_id" type="integer" required="false" defaultValue="null" />
<foreign-key foreignTable="book_author" onDelete="RESTRICT" onUpdate="CASCADE">
View
@@ -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": "" }

0 comments on commit c5d23ec

Please sign in to comment.