convert Faker datetime into string

This fixes #221.

Faker returns a \DateTime object, but the YAML format requires a string.
This commit is contained in:
Toni Uebernickel 2016-01-24 12:10:37 +01:00
parent 32196a6c08
commit 7ab2b44b23
2 changed files with 37 additions and 1 deletions

View file

@ -51,7 +51,12 @@ class YamlDataLoader extends AbstractDataLoader
$args = func_get_args();
array_shift($args);
echo Yaml::dump(call_user_func_array(array($generator, $type), $args)) . "\n";
$value = call_user_func_array(array($generator, $type), $args);
if ($value instanceof \DateTime) {
$value = $value->format('Y-m-d H:i:s');
}
echo Yaml::dump($value) . "\n";
};
} else {
$faker = function($text) {

View file

@ -321,6 +321,37 @@ YAML;
$this->assertRegexp('#[\w ]+#', $book->getDescription());
}
public function testLoadWithFakerDateTime()
{
if (!class_exists('Faker\Factory')) {
$this->markTestSkipped('Faker is mandatory');
}
$fixtures = <<<YAML
Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book:
Book_1:
id: '1'
name: <?php \$faker('dateTimeThisMonth'); ?>
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\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
$this->assertCount(1, $books);
$book = $books[0];
$this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $book->getName());
$datetime = new \DateTime($book->getName());
$this->assertInstanceOf('DateTime', $datetime);
}
public function testLoadWithInheritedRelationship()
{
$schema = <<<XML