Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Hotfix/tablegateway event feature #2688

Closed
wants to merge 2 commits into from

2 participants

@ralphschindler
Collaborator

This supersedes: #2363

This pull-request fixes the EventFeature to work better with various EventManager workflows, it also adds a unit test for this feature.

For more discussion see #2363

ralphschindler added some commits
@ralphschindler ralphschindler Zend\Db\TableGateway:
* Updated EventFeature to optionally take a EventManager
* Added identifiers support to the EventFeature
* Updated TableGatewayEvent to not prefix the event name with the identifier
97449d0
@ralphschindler ralphschindler Zend\Db\TableGateway
* Added unit test for EventFeature
989984d
@weierophinney weierophinney commented on the diff
library/Zend/Db/TableGateway/Feature/EventFeature.php
((14 lines not shown))
$this->event = ($tableGatewayEvent) ?: new EventFeature\TableGatewayEvent();
}
+ public function getEventManager()
@weierophinney Owner

Let's at least implement EventsCapableInterface, as it only defines this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#2688] CS fixes
- explicitly implement EventsCapableInterface (already implemented by
  defining getEventManager())
- docblocks for all methods in EventFeature
- class and file docblocks for EventFeatureTest
0f6e254
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2688' into develop
Forward port #2688
59fcb3a
@weierophinney weierophinney was assigned
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#2688] CS fixes
- explicitly implement EventsCapableInterface (already implemented by
  defining getEventManager())
- docblocks for all methods in EventFeature
- class and file docblocks for EventFeatureTest
14b918c
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2688'
Close #2688
7fe3fd2
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/2688' into develop
Forward port #2688
8a449c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 2, 2012
  1. @ralphschindler

    Zend\Db\TableGateway:

    ralphschindler authored
    * Updated EventFeature to optionally take a EventManager
    * Added identifiers support to the EventFeature
    * Updated TableGatewayEvent to not prefix the event name with the identifier
Commits on Oct 5, 2012
  1. @ralphschindler

    Zend\Db\TableGateway

    ralphschindler authored
    * Added unit test for EventFeature
This page is out of date. Refresh to see the latest.
View
36 library/Zend/Db/TableGateway/Feature/EventFeature.php
@@ -18,6 +18,7 @@
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Update;
use Zend\Db\TableGateway\Exception;
+use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerInterface;
/**
@@ -42,14 +43,36 @@ class EventFeature extends AbstractFeature
* @param EventManagerInterface $eventManager
* @param EventFeature\TableGatewayEvent $tableGatewayEvent
*/
- public function __construct(EventManagerInterface $eventManager, EventFeature\TableGatewayEvent $tableGatewayEvent)
+ public function __construct(EventManagerInterface $eventManager = null, EventFeature\TableGatewayEvent $tableGatewayEvent = null)
{
- $this->eventManager = $eventManager;
+ $this->eventManager = ($eventManager instanceof EventManagerInterface) ? $eventManager : new EventManager;
+
+ $this->eventManager->setIdentifiers(array(
+ 'Zend\Db\TableGateway\TableGateway',
+ ));
+
$this->event = ($tableGatewayEvent) ?: new EventFeature\TableGatewayEvent();
}
+ public function getEventManager()
@weierophinney Owner

Let's at least implement EventsCapableInterface, as it only defines this method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ {
+ return $this->eventManager;
+ }
+
+ public function getEvent()
+ {
+ return $this->event;
+ }
+
+ /**
+ * Ensured to only run once
+ */
public function preInitialize()
{
+ if (get_class($this->tableGateway) != 'Zend\Db\TableGateway\TableGateway') {
+ $this->eventManager->addIdentifiers(get_class($this->tableGateway));
+ }
+
$this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->eventManager->trigger($this->event);
@@ -57,14 +80,12 @@ public function preInitialize()
public function postInitialize()
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->eventManager->trigger($this->event);
}
public function preSelect(Select $select)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array('select' => $select));
$this->eventManager->trigger($this->event);
@@ -72,7 +93,6 @@ public function preSelect(Select $select)
public function postSelect(StatementInterface $statement, ResultInterface $result, ResultSetInterface $resultSet)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array(
'statement' => $statement,
@@ -84,7 +104,6 @@ public function postSelect(StatementInterface $statement, ResultInterface $resul
public function preInsert(Insert $insert)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array('insert' => $insert));
$this->eventManager->trigger($this->event);
@@ -92,7 +111,6 @@ public function preInsert(Insert $insert)
public function postInsert(StatementInterface $statement, ResultInterface $result)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array(
'statement' => $statement,
@@ -103,7 +121,6 @@ public function postInsert(StatementInterface $statement, ResultInterface $resul
public function preUpdate(Update $update)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array('update' => $update));
$this->eventManager->trigger($this->event);
@@ -111,7 +128,6 @@ public function preUpdate(Update $update)
public function postUpdate(StatementInterface $statement, ResultInterface $result)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array(
'statement' => $statement,
@@ -122,7 +138,6 @@ public function postUpdate(StatementInterface $statement, ResultInterface $resul
public function preDelete(Delete $delete)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array('delete' => $delete));
$this->eventManager->trigger($this->event);
@@ -130,7 +145,6 @@ public function preDelete(Delete $delete)
public function postDelete(StatementInterface $statement, ResultInterface $result)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array(
'statement' => $statement,
View
2  library/Zend/Db/TableGateway/Feature/EventFeature/TableGatewayEvent.php
@@ -44,7 +44,7 @@ class TableGatewayEvent implements EventInterface
*/
public function getName()
{
- return get_class($this->target) . '.' . $this->name;
+ return $this->name;
}
/**
View
239 tests/ZendTest/Db/TableGateway/Feature/EventFeatureTest.php
@@ -0,0 +1,239 @@
+<?php
+
+namespace ZendTest\Db\TableGateway\Feature;
+
+use PHPUnit_Framework_TestCase;
+use Zend\EventManager\EventManager;
+use Zend\Db\TableGateway\Feature\EventFeature;
+
+class EventFeatureTest extends PHPUnit_Framework_TestCase
+{
+ /** @var EventManager */
+ protected $eventManager = null;
+
+ /** @var EventFeature */
+ protected $feature = null;
+
+ protected $event = null;
+
+ /** @var \Zend\Db\TableGateway\TableGateway */
+ protected $tableGateway = null;
+
+ public function setup()
+ {
+ $this->eventManager = new EventManager;
+ $this->event = new EventFeature\TableGatewayEvent();
+ $this->feature = new EventFeature($this->eventManager, $this->event);
+ $this->tableGateway = $this->getMockForAbstractClass('Zend\Db\TableGateway\TableGateway', array(), '', false);
+ $this->feature->setTableGateway($this->tableGateway);
+
+ // typically runs before everything else
+ $this->feature->preInitialize();
+ }
+
+ public function testGetEventManager()
+ {
+ $this->assertSame($this->eventManager, $this->feature->getEventManager());
+ }
+
+ public function testGetEvent()
+ {
+ $this->assertSame($this->event, $this->feature->getEvent());
+ }
+
+ public function testPreInitialize()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('preInitialize', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->preInitialize();
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('preInitialize', $event->getName());
+ }
+
+ public function testPostInitialize()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('postInitialize', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->postInitialize();
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('postInitialize', $event->getName());
+ }
+
+ public function testPreSelect()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('preSelect', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->preSelect($select = $this->getMock('Zend\Db\Sql\Select'));
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('preSelect', $event->getName());
+ $this->assertSame($select, $event->getParam('select'));
+ }
+
+ public function testPostSelect()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('postSelect', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->postSelect(
+ ($stmt = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface')),
+ ($result = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface')),
+ ($resultset = $this->getMock('Zend\Db\ResultSet\ResultSet'))
+ );
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('postSelect', $event->getName());
+ $this->assertSame($stmt, $event->getParam('statement'));
+ $this->assertSame($result, $event->getParam('result'));
+ $this->assertSame($resultset, $event->getParam('result_set'));
+ }
+
+ public function testPreInsert()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('preInsert', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->preInsert($insert = $this->getMock('Zend\Db\Sql\Insert'));
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('preInsert', $event->getName());
+ $this->assertSame($insert, $event->getParam('insert'));
+ }
+
+ public function testPostInsert()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('postInsert', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->postInsert(
+ ($stmt = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface')),
+ ($result = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'))
+ );
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('postInsert', $event->getName());
+ $this->assertSame($stmt, $event->getParam('statement'));
+ $this->assertSame($result, $event->getParam('result'));
+ }
+
+ public function testPreUpdate()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('preUpdate', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->preUpdate($update = $this->getMock('Zend\Db\Sql\Update'));
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('preUpdate', $event->getName());
+ $this->assertSame($update, $event->getParam('update'));
+ }
+
+ public function testPostUpdate()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('postUpdate', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->postUpdate(
+ ($stmt = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface')),
+ ($result = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'))
+ );
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('postUpdate', $event->getName());
+ $this->assertSame($stmt, $event->getParam('statement'));
+ $this->assertSame($result, $event->getParam('result'));
+ }
+
+ public function testPreDelete()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('preDelete', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->preDelete($delete = $this->getMock('Zend\Db\Sql\Delete'));
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('preDelete', $event->getName());
+ $this->assertSame($delete, $event->getParam('delete'));
+ }
+
+ public function testPostDelete()
+ {
+ $closureHasRun = false;
+
+ /** @var $event EventFeature\TableGatewayEvent */
+ $event = null;
+ $this->eventManager->attach('postDelete', function ($e) use (&$closureHasRun, &$event) {
+ $event = $e;
+ $closureHasRun = true;
+ });
+
+ $this->feature->postDelete(
+ ($stmt = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface')),
+ ($result = $this->getMock('Zend\Db\Adapter\Driver\ResultInterface'))
+ );
+ $this->assertTrue($closureHasRun);
+ $this->assertInstanceOf('Zend\Db\TableGateway\TableGateway', $event->getTarget());
+ $this->assertEquals('postDelete', $event->getName());
+ $this->assertSame($stmt, $event->getParam('statement'));
+ $this->assertSame($result, $event->getParam('result'));
+ }
+}
Something went wrong with that request. Please try again.