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

Commit e67b7fd

Browse files
committed
Merge branch 'hotfix/247'
Close #247 Fixes #246
2 parents 831db33 + 705ba86 commit e67b7fd

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#247](https://github.com/zendframework/zend-mvc/pull/247) fixes bug in
22+
controller plugin Forward, introduced in 2.1.0, where problem listeners were
23+
not detached for forwarded controller dispatch
2224

2325
## 3.1.0 - 2017-05-01
2426

src/Controller/Plugin/Forward.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ 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+
$this->detachSharedListener($id, $currentEvent, $sharedEvents);
201+
$results[$id][$eventName][$priority] = $currentEvent;
202+
}
203+
}
197204
}
198205
}
199206
}

test/Controller/Plugin/ForwardTest.php

Lines changed: 59 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,64 @@ function ($e) {}
237238
$this->assertEquals(['content' => 'ZendTest\Mvc\Controller\TestAsset\ForwardController::testAction'], $result);
238239
}
239240

241+
public function testProblemListenersAreDetachedAndReattachedWhenPluginDispatchesRequestedController()
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+
270+
public function testInvokableProblemListenersAreDetachedAndReattachedWhenPluginDispatchesRequestedController()
271+
{
272+
$services = $this->services;
273+
$events = $services->get('EventManager');
274+
275+
$myCallback = new ListenerStub();
276+
$sharedEvents = $this->createMock(SharedEventManagerInterface::class);
277+
$sharedEvents->expects($this->once())->method('detach')->with($myCallback, 'Zend\Stdlib\DispatchableInterface');
278+
$sharedEvents
279+
->expects($this->once())
280+
->method('attach')
281+
->with('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, $myCallback, -50);
282+
$sharedEvents->expects($this->any())->method('getListeners')->will($this->returnValue([-50 => [$myCallback]]));
283+
$events = $this->createEventManager($sharedEvents);
284+
285+
$application = $this->createMock(ApplicationInterface::class);
286+
$application->expects($this->any())->method('getEventManager')->will($this->returnValue($events));
287+
$event = $this->controller->getEvent();
288+
$event->setApplication($application);
289+
290+
$this->plugin->setListenersToDetach([[
291+
'id' => 'Zend\Stdlib\DispatchableInterface',
292+
'event' => MvcEvent::EVENT_DISPATCH,
293+
'class' => 'ZendTest\Mvc\Controller\Plugin\TestAsset\ListenerStub',
294+
]]);
295+
296+
$result = $this->plugin->dispatch('forward');
297+
}
298+
240299
public function testDispatchWillSeedRouteMatchWithPassedParameters()
241300
{
242301
$result = $this->plugin->dispatch('forward', [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace ZendTest\Mvc\Controller\Plugin\TestAsset;
3+
4+
class ListenerStub
5+
{
6+
public function __invoke()
7+
{
8+
}
9+
10+
public function myCallback()
11+
{
12+
}
13+
}

0 commit comments

Comments
 (0)