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

Commit

Permalink
Add full namespace identifier for controller events
Browse files Browse the repository at this point in the history
  • Loading branch information
Xerkus committed Feb 17, 2019
1 parent ff6971a commit b6c3c67
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
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
}
}

0 comments on commit b6c3c67

Please sign in to comment.