From b6c3c6780611ae7544fdc067a229e9d66507ce47 Mon Sep 17 00:00:00 2001 From: Aleksei Khudiakov Date: Thu, 17 May 2018 10:00:07 +1000 Subject: [PATCH 1/2] Add full namespace identifier for controller events --- src/Controller/AbstractController.php | 18 ++++++++++------ test/Controller/AbstractControllerTest.php | 21 ++++++++++++++++++- .../TestAsset/AbstractControllerStub.php | 19 +++++++++++++++++ 3 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 test/Controller/TestAsset/AbstractControllerStub.php diff --git a/src/Controller/AbstractController.php b/src/Controller/AbstractController.php index da634c5c6..be52dcdf1 100644 --- a/src/Controller/AbstractController.php +++ b/src/Controller/AbstractController.php @@ -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 )); diff --git a/test/Controller/AbstractControllerTest.php b/test/Controller/AbstractControllerTest.php index c7a2f2867..1c287de1a 100644 --- a/test/Controller/AbstractControllerTest.php +++ b/test/Controller/AbstractControllerTest.php @@ -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 @@ -30,7 +31,7 @@ class AbstractControllerTest extends TestCase */ protected function setUp() { - $this->controller = $this->getMockForAbstractClass(AbstractController::class); + $this->controller = new AbstractControllerStub(); } /** @@ -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); + } } diff --git a/test/Controller/TestAsset/AbstractControllerStub.php b/test/Controller/TestAsset/AbstractControllerStub.php new file mode 100644 index 000000000..aa3b59397 --- /dev/null +++ b/test/Controller/TestAsset/AbstractControllerStub.php @@ -0,0 +1,19 @@ + Date: Sun, 17 Feb 2019 11:59:12 +1000 Subject: [PATCH 2/2] Add CHANGELOG for #282 --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 51c813c2a..7394827d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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