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

Commit 860e140

Browse files
NathanSNathanS
NathanS
authored and
NathanS
committed
Fixes #246 : Forward plugin should detach problem listeners
1 parent 4c4a3a9 commit 860e140

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/Controller/Plugin/Forward.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
194194
if (! is_object($currentCallback)) {
195195
continue;
196196
}
197+
198+
foreach ($classArray as $class) {
199+
if ($currentCallback instanceof $class) {
200+
// Pass $currentEvent; when using zend-eventmanager v2,
201+
// this is the CallbackHandler, while in v3 it's
202+
// the actual listener.
203+
$this->detachSharedListener($id, $currentEvent, $sharedEvents);
204+
$results[$id][$eventName][$priority] = $currentEvent;
205+
}
206+
}
197207
}
198208
}
199209
}

test/Controller/Plugin/ForwardTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use ZendTest\Mvc\Controller\TestAsset\ForwardController;
3434
use ZendTest\Mvc\Controller\TestAsset\SampleController;
3535
use ZendTest\Mvc\Controller\TestAsset\UneventfulController;
36+
use ZendTest\Mvc\Controller\Plugin\TestAsset\ListenerStub;
3637

3738
class ForwardTest extends TestCase
3839
{
@@ -237,6 +238,35 @@ function ($e) {}
237238
$this->assertEquals(['content' => 'ZendTest\Mvc\Controller\TestAsset\ForwardController::testAction'], $result);
238239
}
239240

241+
public function testProblemListenersAreDetachedAndReattachedWhenPluginDispatchsRequestedController()
242+
{
243+
$services = $this->services;
244+
$events = $services->get('EventManager');
245+
246+
$myCallback = [new ListenerStub(),'myCallback'];
247+
$sharedEvents = $this->createMock(SharedEventManagerInterface::class);
248+
$sharedEvents->expects($this->once())->method('detach')->with($myCallback, 'Zend\Stdlib\DispatchableInterface');
249+
$sharedEvents
250+
->expects($this->once())
251+
->method('attach')
252+
->with('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, $myCallback, -50);
253+
$sharedEvents->expects($this->any())->method('getListeners')->will($this->returnValue([-50 => [$myCallback]]));
254+
$events = $this->createEventManager($sharedEvents);
255+
256+
$application = $this->createMock(ApplicationInterface::class);
257+
$application->expects($this->any())->method('getEventManager')->will($this->returnValue($events));
258+
$event = $this->controller->getEvent();
259+
$event->setApplication($application);
260+
261+
$this->plugin->setListenersToDetach([[
262+
'id' => 'Zend\Stdlib\DispatchableInterface',
263+
'event' => MvcEvent::EVENT_DISPATCH,
264+
'class' => 'ZendTest\Mvc\Controller\Plugin\TestAsset\ListenerStub',
265+
]]);
266+
267+
$result = $this->plugin->dispatch('forward');
268+
}
269+
240270
public function testDispatchWillSeedRouteMatchWithPassedParameters()
241271
{
242272
$result = $this->plugin->dispatch('forward', [
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
3+
4+
class ListenerStub
5+
{
6+
public function myCallback()
7+
{
8+
}
9+
}

0 commit comments

Comments
 (0)