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

Add full namespace identifier for controller events #282

Merged
merged 2 commits into from
Feb 17, 2019
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ All notable changes to this project will be documented in this file, in reverse
### Added

- [#297](https://github.com/zendframework/zend-mvc/pull/297) adds support for PHP 7.3.
- [#282](https://github.com/zendframework/zend-mvc/pull/282) Adds a full
controller namespace as additional event manager identifier for
implementations of AbstractController

### Deprecated

Expand Down
18 changes: 12 additions & 6 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,19 @@ public function setEventManager(EventManagerInterface $events)
{
$className = get_class($this);

$nsPos = strpos($className, '\\') ?: 0;
$identifiers = [
__CLASS__,
$className,
];

$rightmostNsPos = strrpos($className, '\\');
if ($rightmostNsPos) {
$identifiers[] = strstr($className, '\\', true); // top namespace
$identifiers[] = substr($className, 0, $rightmostNsPos); // full namespace
}

$events->setIdentifiers(array_merge(
[
__CLASS__,
$className,
substr($className, 0, $nsPos)
],
$identifiers,
array_values(class_implements($className)),
(array) $this->eventIdentifier
));
Expand Down
21 changes: 20 additions & 1 deletion test/Controller/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Zend\Mvc\Controller\AbstractController;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Stdlib\DispatchableInterface;
use ZendTest\Mvc\Controller\TestAsset\AbstractControllerStub;

/**
* @covers \Zend\Mvc\Controller\AbstractController
Expand All @@ -30,7 +31,7 @@ class AbstractControllerTest extends TestCase
*/
protected function setUp()
{
$this->controller = $this->getMockForAbstractClass(AbstractController::class);
$this->controller = new AbstractControllerStub();
}

/**
Expand Down Expand Up @@ -107,4 +108,22 @@ public function testSetEventManagerWithDefaultIdentifiersIncludesImplementedInte

$this->controller->setEventManager($eventManager);
}

public function testSetEventManagerWithDefaultIdentifiersIncludesExtendingClassNameAndNamespace()
{
/* @var $eventManager EventManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
$eventManager = $this->createMock(EventManagerInterface::class);

$eventManager
->expects($this->once())
->method('setIdentifiers')
->with($this->logicalAnd(
$this->contains(AbstractController::class),
$this->contains(AbstractControllerStub::class),
$this->contains('ZendTest'),
$this->contains('ZendTest\\Mvc\\Controller\\TestAsset')
));

$this->controller->setEventManager($eventManager);
}
}
19 changes: 19 additions & 0 deletions test/Controller/TestAsset/AbstractControllerStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\Mvc\Controller\TestAsset;

use Zend\Mvc\Controller\AbstractController;
use Zend\Mvc\MvcEvent;

class AbstractControllerStub extends AbstractController
{
public function onDispatch(MvcEvent $e)
{
// noop
}
}