Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Hotfix db features with eventfeature #2363

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions library/Zend/Db/TableGateway/Feature/EventFeature.php
Expand Up @@ -42,10 +42,10 @@ class EventFeature extends AbstractFeature
* @param EventManagerInterface $eventManager
* @param EventFeature\TableGatewayEvent $tableGatewayEvent
*/
public function __construct(EventManagerInterface $eventManager, EventFeature\TableGatewayEvent $tableGatewayEvent)
public function __construct(EventManagerInterface $eventManager, EventFeature\TableGatewayEvent $tableGatewayEvent = null)
{
$this->eventManager = $eventManager;
$this->event = ($tableGatewayEvent) ?: new EventFeature\TableGatewayEvent();
$this->event = ($tableGatewayEvent) ? $tableGatewayEvent : new EventFeature\TableGatewayEvent();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the original version of this line was fine; it was simply using the ternary shortcut. If $tableGatewayEvent evaluated to null, it would create a new instance; otherwise, it wouldn't.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I thought that it would be more clear

}

public function preInitialize()
Expand Down Expand Up @@ -138,5 +138,15 @@ public function postDelete(StatementInterface $statement, ResultInterface $resul
));
$this->eventManager->trigger($this->event);
}


public function getEventManager()
{
return $this->eventManager;
}

public function setEventManager(EventManagerInterface $eventManager)
{
$this->eventManager = $eventManager;
return $this;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to implement these, you should likely also implement the EventManagerAwareInterface, which would make it possible to get this auto-injected via the service manager.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, in setEventManager, you should set identifiers:

$eventManager->setIdentifiers(array(
    __CLASS__,
    get_class($this),
));
$this->eventManager = $eventManager
return $this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used "addIdentifiers" to keep existing identifiers.

}
50 changes: 50 additions & 0 deletions tests/ZendTest/Db/TableGateway/Feature/EventFeatureTest.php
@@ -0,0 +1,50 @@
<?php

namespace ZendTest\Db\TableGateway\Feature;

use PHPUnit_Framework_TestCase;
use Zend\Db\TableGateway\Feature\EventFeature;
use Zend\EventManager\EventManager;
use Zend\Db\TableGateway\Feature\EventFeature\TableGatewayEvent;

class EventFeatureTest extends PHPUnit_Framework_TestCase
{

public function testConstructWithEventManager()
{
$eventManager = new EventManager();
$eventManager->setIdentifiers(array('foo', 'bar'));
$feature = new EventFeature($eventManager);
$em = $feature->getEventManager();
$this->assertEquals($em->getIdentifiers(), array('foo', 'bar'));
}

public function testConstructWithEventManagerAndEvent()
{
$eventManager = new EventManager();
$eventManager->setIdentifiers(array('foo', 'bar'));

$tableGatewayMock = $this->getMockForAbstractClass('Zend\Db\TableGateway\AbstractTableGateway');
$event = new TableGatewayEvent();
$event->setParam('foo', 'bar');

$feature = new EventFeature($eventManager, $event);
$feature->setTableGateway($tableGatewayMock);

// test event with eveant feature
$eventManager->attach(get_class($tableGatewayMock) . '.preInitialize', function ($e) {
$e->setParam('foo', 'baz');
});
$feature->preInitialize();
$this->assertEquals($event->getParam('foo'), 'baz');

// test events with new event manager
$newEventManager = new EventManager();
$newEventManager->attach(get_class($tableGatewayMock) . '.preInitialize', function ($e) {
$e->setParam('foo', 'bar');
});
$feature->setEventManager($newEventManager);
$feature->preInitialize();
$this->assertEquals($event->getParam('foo'), 'bar');
}
}