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

Fixes #246 : Forward plugin should detach problem listeners #247

Merged
merged 4 commits into from
Nov 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

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

## 3.1.0 - 2017-05-01

Expand Down
7 changes: 7 additions & 0 deletions src/Controller/Plugin/Forward.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
if (! is_object($currentCallback)) {
continue;
}

foreach ($classArray as $class) {
if ($currentCallback instanceof $class) {
$this->detachSharedListener($id, $currentEvent, $sharedEvents);
$results[$id][$eventName][$priority] = $currentEvent;
}
}
}
}
}
Expand Down
59 changes: 59 additions & 0 deletions test/Controller/Plugin/ForwardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use ZendTest\Mvc\Controller\TestAsset\ForwardController;
use ZendTest\Mvc\Controller\TestAsset\SampleController;
use ZendTest\Mvc\Controller\TestAsset\UneventfulController;
use ZendTest\Mvc\Controller\Plugin\TestAsset\ListenerStub;

class ForwardTest extends TestCase
{
Expand Down Expand Up @@ -237,6 +238,64 @@ function ($e) {}
$this->assertEquals(['content' => 'ZendTest\Mvc\Controller\TestAsset\ForwardController::testAction'], $result);
}

public function testProblemListenersAreDetachedAndReattachedWhenPluginDispatchesRequestedController()
{
$services = $this->services;
$events = $services->get('EventManager');

$myCallback = [new ListenerStub(),'myCallback'];
$sharedEvents = $this->createMock(SharedEventManagerInterface::class);
$sharedEvents->expects($this->once())->method('detach')->with($myCallback, 'Zend\Stdlib\DispatchableInterface');
$sharedEvents
->expects($this->once())
->method('attach')
->with('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, $myCallback, -50);
$sharedEvents->expects($this->any())->method('getListeners')->will($this->returnValue([-50 => [$myCallback]]));
$events = $this->createEventManager($sharedEvents);

$application = $this->createMock(ApplicationInterface::class);
$application->expects($this->any())->method('getEventManager')->will($this->returnValue($events));
$event = $this->controller->getEvent();
$event->setApplication($application);

$this->plugin->setListenersToDetach([[
'id' => 'Zend\Stdlib\DispatchableInterface',
'event' => MvcEvent::EVENT_DISPATCH,
'class' => 'ZendTest\Mvc\Controller\Plugin\TestAsset\ListenerStub',
]]);

$result = $this->plugin->dispatch('forward');
}

public function testInvokableProblemListenersAreDetachedAndReattachedWhenPluginDispatchesRequestedController()
{
$services = $this->services;
$events = $services->get('EventManager');

$myCallback = new ListenerStub();
$sharedEvents = $this->createMock(SharedEventManagerInterface::class);
$sharedEvents->expects($this->once())->method('detach')->with($myCallback, 'Zend\Stdlib\DispatchableInterface');
$sharedEvents
->expects($this->once())
->method('attach')
->with('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, $myCallback, -50);
$sharedEvents->expects($this->any())->method('getListeners')->will($this->returnValue([-50 => [$myCallback]]));
$events = $this->createEventManager($sharedEvents);

$application = $this->createMock(ApplicationInterface::class);
$application->expects($this->any())->method('getEventManager')->will($this->returnValue($events));
$event = $this->controller->getEvent();
$event->setApplication($application);

$this->plugin->setListenersToDetach([[
'id' => 'Zend\Stdlib\DispatchableInterface',
'event' => MvcEvent::EVENT_DISPATCH,
'class' => 'ZendTest\Mvc\Controller\Plugin\TestAsset\ListenerStub',
]]);

$result = $this->plugin->dispatch('forward');
}

public function testDispatchWillSeedRouteMatchWithPassedParameters()
{
$result = $this->plugin->dispatch('forward', [
Expand Down
13 changes: 13 additions & 0 deletions test/Controller/Plugin/TestAsset/ListenerStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace ZendTest\Mvc\Controller\Plugin\TestAsset;

class ListenerStub
{
public function __invoke()
{
}

public function myCallback()
{
}
}