Skip to content

Commit

Permalink
Merge pull request propelorm#393 from havvg/1.5
Browse files Browse the repository at this point in the history
convert Faker datetime into string
  • Loading branch information
havvg committed Jan 24, 2016
2 parents 32196a6 + 7ab2b44 commit b1b9620
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion DataFixtures/Loader/YamlDataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ protected function transformDataToArray($file)
$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) {
Expand Down
31 changes: 31 additions & 0 deletions Tests/DataFixtures/Loader/YamlDataLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,37 @@ public function testLoadWithFaker()
$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
Expand Down

0 comments on commit b1b9620

Please sign in to comment.