This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
Hotfix db features with eventfeature #2363
Closed
blanchonvincent
wants to merge
5
commits into
zendframework:master
from
blanchonvincent:hotfix-db-eventfeature
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
34107fc
Fix params in constructor
blanchonvincent 94302a6
Add unit tests to test fix
blanchonvincent eb4705f
Fix interface and event manager identifiers
blanchonvincent 3471d53
Wrap the event manager in construct params
blanchonvincent 48e4e9c
fix CS with PSR2
blanchonvincent File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
|
||
public function preInitialize() | ||
|
@@ -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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
50
tests/ZendTest/Db/TableGateway/Feature/EventFeatureTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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