Permalink
Browse files

Merge branch 'hotfix/tablegateway-event-feature' of https://github.co…

…m/ralphschindler/zf2 into hotfix/2688
  • Loading branch information...
2 parents 46d786f + 989984d commit a1b5f36fa4e5d714317941e898f199165f08671c @weierophinney weierophinney committed Oct 8, 2012
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,37 +43,56 @@ 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()
+ {
+ 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);
}
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);
}
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,15 +104,13 @@ 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);
}
public function postInsert(StatementInterface $statement, ResultInterface $result)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array(
'statement' => $statement,
@@ -103,15 +121,13 @@ 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);
}
public function postUpdate(StatementInterface $statement, ResultInterface $result)
{
- $this->event->setTarget($this->tableGateway);
$this->event->setName(__FUNCTION__);
$this->event->setParams(array(
'statement' => $statement,
@@ -122,15 +138,13 @@ 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);
}
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'));
+ }
+}

0 comments on commit a1b5f36

Please sign in to comment.