Skip to content

Commit

Permalink
Merge pull request #326 from GrahamCampbell/2.1-callback
Browse files Browse the repository at this point in the history
[2.1] Fixed Callbacks
  • Loading branch information
GrahamCampbell committed Sep 18, 2014
2 parents c9ebae5 + 64fdaad commit 3d502c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public function create($model, array $attr = array())

$this->persist($object);

if ($this->triggerCallback($object)) {
if ($this->triggerCallback($object, $model)) {
$this->persist($object);
}

Expand Down Expand Up @@ -293,13 +293,12 @@ private function persist($object)
* Trigger the callback if we have one.
*
* @param object $object The model instance.
* @param string $model The model class name.
*
* @return bool
*/
private function triggerCallback($object)
private function triggerCallback($object, $model)
{
$model = get_class($object);

if ($callback = Arr::get($this->callbacks, $model)) {
$saved = $this->isPendingOrSaved($object);
$callback($object, $saved);
Expand Down Expand Up @@ -559,7 +558,7 @@ public function instance($model, array $attr = array())
{
$object = $this->make($model, $attr, false);

$this->triggerCallback($object);
$this->triggerCallback($object, $model);

return $object;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ public function testGroupDefineOverwrite()
$this->assertContains('@', $user->email);
}

public function testGroupCallback()
{
$user = FactoryMuffin::create('callbackgroup:UserModelStub');

$this->assertInstanceOf('UserModelStub', $user);
$this->assertEquals('bar', $user->test);
$this->assertInternalType('string', $user->name);
$this->assertInternalType('boolean', $user->active);
$this->assertContains('@', $user->email);
}

/**
* @expectedException \League\FactoryMuffin\Exceptions\NoDefinedFactoryException
*/
Expand Down
4 changes: 4 additions & 0 deletions tests/factories/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
'active' => 'false',
));

FactoryMuffin::define('callbackgroup:UserModelStub', array(), function($obj) {
$obj->test = 'bar';
});

FactoryMuffin::define('foo:DogModelStub', array(
'name' => 'firstNameMale',
'age' => 'numberBetween|1;15',
Expand Down

0 comments on commit 3d502c5

Please sign in to comment.