Skip to content

Commit b1b9620

Browse files
committed
Merge pull request propelorm#393 from havvg/1.5
convert Faker datetime into string
2 parents 32196a6 + 7ab2b44 commit b1b9620

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

DataFixtures/Loader/YamlDataLoader.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ protected function transformDataToArray($file)
5151
$args = func_get_args();
5252
array_shift($args);
5353

54-
echo Yaml::dump(call_user_func_array(array($generator, $type), $args)) . "\n";
54+
$value = call_user_func_array(array($generator, $type), $args);
55+
if ($value instanceof \DateTime) {
56+
$value = $value->format('Y-m-d H:i:s');
57+
}
58+
59+
echo Yaml::dump($value) . "\n";
5560
};
5661
} else {
5762
$faker = function($text) {

Tests/DataFixtures/Loader/YamlDataLoaderTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,37 @@ public function testLoadWithFaker()
321321
$this->assertRegexp('#[\w ]+#', $book->getDescription());
322322
}
323323

324+
public function testLoadWithFakerDateTime()
325+
{
326+
if (!class_exists('Faker\Factory')) {
327+
$this->markTestSkipped('Faker is mandatory');
328+
}
329+
330+
$fixtures = <<<YAML
331+
Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\Book:
332+
Book_1:
333+
id: '1'
334+
name: <?php \$faker('dateTimeThisMonth'); ?>
335+
description: <?php \$faker('sentence'); ?>
336+
337+
YAML;
338+
$filename = $this->getTempFile($fixtures);
339+
$container = $this->getContainer();
340+
$container->set('faker.generator', \Faker\Factory::create());
341+
342+
$loader = new YamlDataLoader(__DIR__.'/../../Fixtures/DataFixtures/Loader', $container);
343+
$loader->load(array($filename), 'default');
344+
345+
$books = \Propel\Bundle\PropelBundle\Tests\Fixtures\DataFixtures\Loader\BookPeer::doSelect(new \Criteria(), $this->con);
346+
$this->assertCount(1, $books);
347+
348+
$book = $books[0];
349+
$this->assertRegExp('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $book->getName());
350+
351+
$datetime = new \DateTime($book->getName());
352+
$this->assertInstanceOf('DateTime', $datetime);
353+
}
354+
324355
public function testLoadWithInheritedRelationship()
325356
{
326357
$schema = <<<XML

0 commit comments

Comments
 (0)