Skip to content

Commit

Permalink
Merge pull request willdurand#7 from K-Phoen/post-hydrate
Browse files Browse the repository at this point in the history
Added a post_hydrate hook
  • Loading branch information
willdurand committed Dec 19, 2013
2 parents 0d76e7b + debe531 commit 572bfc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/EventDispatcherObjectBuilderModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function objectAttributes($builder)
$events = array();
foreach (array(
'construct',
'post_hydrate',
'pre_save', 'post_save',
'pre_update', 'post_update',
'pre_insert', 'post_insert',
Expand Down Expand Up @@ -71,6 +72,13 @@ public function addConstructHook()
)) . ' ';
}

public function postHydrate()
{
return $this->behavior->renderTemplate('objectHook', array(
'eventName' => $this->getEventName('post_hydrate'),
));
}

public function preSave()
{
return $this->behavior->renderTemplate('objectHook', array(
Expand Down
12 changes: 12 additions & 0 deletions tests/EventDispatcherBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function testObjectMethods()
$this->assertTrue(defined('Post::EVENT_PRE_DELETE'));
$this->assertTrue(defined('Post::EVENT_POST_DELETE'));
$this->assertTrue(defined('Post::EVENT_CONSTRUCT'));
$this->assertTrue(defined('Post::EVENT_POST_HYDRATE'));
}

public function testGetDispatcher()
Expand All @@ -76,10 +77,18 @@ public function testFireEvent()
$preSaveFired = false;
$postSaveFired = false;
$postConstructFired = false;
$postHydrateFired = false;
$threadConstructFired = false;

$that = $this;

Post::getEventDispatcher()->addListener(Post::EVENT_POST_HYDRATE, function (Event $event) use (& $postHydrateFired, $that) {
$postHydrateFired = true;

$that->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
$that->assertInstanceOf('Post', $event->getSubject());
});

Post::getEventDispatcher()->addListener(Post::EVENT_CONSTRUCT, function (Event $event) use (& $postConstructFired, $that) {
$postConstructFired = true;

Expand Down Expand Up @@ -111,12 +120,15 @@ public function testFireEvent()
new Thread();
$this->assertTrue($threadConstructFired);


$post = new Post();
$this->assertTrue($postConstructFired);

$post->setName('a-name');
$post->save();
$post->reload();

$this->assertTrue($postHydrateFired);
$this->assertTrue($preSaveFired);
$this->assertTrue($postSaveFired);
}
Expand Down

0 comments on commit 572bfc4

Please sign in to comment.