Skip to content

Commit

Permalink
Ensure re-Faked values always - closes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Mar 29, 2015
1 parent 7fa6490 commit 4f1b183
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
39 changes: 35 additions & 4 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Laracasts\TestDummy;

use Illuminate\Support\Collection;
use Faker\Factory as Faker;
use Closure;

class Builder
{
Expand All @@ -26,7 +28,14 @@ class Builder
*
* @var IsPersistable
*/
private $model;
protected $model;

/**
* The Faker instance.
*
* @var Faker
*/
protected $faker;

/**
* Create a new Builder instance.
Expand Down Expand Up @@ -192,11 +201,19 @@ protected function getFixture($name)
/**
* Apply Faker dummy values to the attributes.
*
* @param array $attributes
* @param object|array $attributes
* @return array
*/
protected function triggerFakerOnAttributes(array $attributes)
protected function triggerFakerOnAttributes($attributes)
{
// If $attributes is a closure, then we need to call it
// and fetch the returned array. This way, we ensure
// that we always fetch unique faked values.

if ($attributes instanceof Closure) {
$attributes = $attributes($this->faker());
}

// To ensure that we don't use the same Faker value for every
// single factory of the same name, all Faker properties are
// wrapped in closures.
Expand All @@ -205,7 +222,7 @@ protected function triggerFakerOnAttributes(array $attributes)
// closures, which will generate the proper Faker values.

return array_map(function ($attribute) {
if ($attribute instanceof \Closure) {
if ($attribute instanceof Closure) {
$attribute = $attribute();
}

Expand All @@ -216,6 +233,20 @@ protected function triggerFakerOnAttributes(array $attributes)
}, $attributes);
}

/**
* Get a Faker instance.
*
* @return Faker
*/
protected function faker()
{
if ( ! $this->faker) {
$this->faker = Faker::create();
}

return $this->faker;
}

/**
* Prepare and assign any applicable relationships.
*
Expand Down
34 changes: 1 addition & 33 deletions src/FactoriesLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function load($basePath)
include($file);
}

return $this->normalizeDefinitions($designer);
return $designer->definitions();
}

/**
Expand All @@ -46,36 +46,4 @@ private function assertThatFactoriesDirectoryExists($basePath)
);
}
}

/**
* Normalize factory calls, so that the user may provide either
* an array or closure to define the makeup of the entity.
*
* @param Designer $designer
* @return array
*/
private function normalizeDefinitions(Designer $designer)
{
$faker = Faker::create();

return array_map(function ($definition) use ($faker) {

// If the user provided a closure, then we need to trigger
// it, and fetch the returned array.

if (is_callable($definition->attributes)) {
$definition->attributes = call_user_func($definition->attributes, $faker);

// And if the user didn't return an array from that closure, well
// we don't exactly know how to proceed. So we'll abort.

if ( ! is_array($definition->attributes)) {
throw new TestDummyException("Factory closure must return an array of attributes.");
}
}

return $definition;
}, $designer->definitions());
}

}
9 changes: 6 additions & 3 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ public function it_accepts_a_short_name_identifier_instead_of_the_model_class()
/** @test */
public function it_allows_a_closure_to_be_used_for_defining_factories()
{
$foo = TestDummy::build('Foo');
$comments = TestDummy::times(2)->create('Comment');

assertInstanceOf('Foo', $foo);
assertInternalType('string', $foo->name);
assertInstanceOf('Comment', $comments[0]);
assertInternalType('string', $comments[0]->body);

// Faker should produce a unique value for each generation.
assertNotEquals($comments[0]->body, $comments[1]->body);
}

/** @test */
Expand Down
11 changes: 7 additions & 4 deletions tests/support/factories/factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
'title' => 'Post Title'
]);

$factory('Comment', [
'post_id' => 'factory:Post',
'body' => $faker->word
]);
$factory('Comment', function($faker) {

return [
'post_id' => 'factory:Post',
'body' => $faker->word
];
});

$factory('Comment', 'comment', []);

Expand Down

0 comments on commit 4f1b183

Please sign in to comment.