Skip to content

Commit

Permalink
Added faker support
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed May 8, 2012
1 parent 6a648ed commit ca45e9c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Command/FixturesLoadCommand.php
Expand Up @@ -181,7 +181,7 @@ protected function loadFixtures(InputInterface $input, OutputInterface $output,
list($name, $defaultConfig) = $this->getConnection($input, $output); list($name, $defaultConfig) = $this->getConnection($input, $output);


if ('yml' === $type) { if ('yml' === $type) {
$loader = new YamlDataLoader($this->getApplication()->getKernel()->getRootDir()); $loader = new YamlDataLoader($this->getApplication()->getKernel()->getRootDir(), $this->getContainer());
} elseif ('xml' === $type) { } elseif ('xml' === $type) {
$loader = new XmlDataLoader($this->getApplication()->getKernel()->getRootDir()); $loader = new XmlDataLoader($this->getApplication()->getKernel()->getRootDir());
} else { } else {
Expand Down
43 changes: 41 additions & 2 deletions DataFixtures/Loader/YamlDataLoader.php
Expand Up @@ -10,6 +10,8 @@


namespace Propel\PropelBundle\DataFixtures\Loader; namespace Propel\PropelBundle\DataFixtures\Loader;


use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Yaml\ParseException;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;


/** /**
Expand All @@ -19,21 +21,58 @@
*/ */
class YamlDataLoader extends AbstractDataLoader class YamlDataLoader extends AbstractDataLoader
{ {
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;

/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function __construct($rootDir) public function __construct($rootDir, ContainerInterface $container = null)
{ {
parent::__construct($rootDir); parent::__construct($rootDir);


Yaml::enablePhpParsing(true); $this->container = $container;
} }


/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function transformDataToArray($file) 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); return Yaml::parse($file);
} }
} }
13 changes: 13 additions & 0 deletions Resources/doc/README.markdown
Expand Up @@ -298,6 +298,19 @@ is a timestamp.
Once done, you will be able to load this files by using the `propel:fixtures:load` command. 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 ### ### Graphviz ###


You can generate **Graphviz** file for your project by using the following command line: You can generate **Graphviz** file for your project by using the following command line:
Expand Down
53 changes: 53 additions & 0 deletions Tests/DataFixtures/Loader/YamlDataLoaderTest.php
Expand Up @@ -176,4 +176,57 @@ public function testLoaderWithPhp()
$author = $authors[0]; $author = $authors[0];
$this->assertEquals('to be announced', $author->getName()); $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());
}
} }
1 change: 1 addition & 0 deletions Tests/DataFixtures/TestCase.php
Expand Up @@ -40,6 +40,7 @@ protected function setUp()
<table name="book"> <table name="book">
<column name="id" type="integer" primaryKey="true" /> <column name="id" type="integer" primaryKey="true" />
<column name="name" type="varchar" size="255" /> <column name="name" type="varchar" size="255" />
<column name="description" type="varchar" />
<column name="author_id" type="integer" required="false" defaultValue="null" /> <column name="author_id" type="integer" required="false" defaultValue="null" />
<foreign-key foreignTable="book_author" onDelete="RESTRICT" onUpdate="CASCADE"> <foreign-key foreignTable="book_author" onDelete="RESTRICT" onUpdate="CASCADE">
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -13,7 +13,8 @@
"propel/propel1": "1.6.*" "propel/propel1": "1.6.*"
}, },
"require-dev": { "require-dev": {
"sensio/framework-extra-bundle": "dev-master" "sensio/framework-extra-bundle": "dev-master",
"fzaninotto/faker": "dev-master"
}, },
"autoload": { "autoload": {
"psr-0": { "Propel\\PropelBundle": "" } "psr-0": { "Propel\\PropelBundle": "" }
Expand Down

0 comments on commit ca45e9c

Please sign in to comment.