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

allowing more event identifiers in AbstractController #248

Closed
Closed
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
21 changes: 15 additions & 6 deletions src/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,23 @@ public function getResponse()
public function setEventManager(EventManagerInterface $events)
{
$className = get_class($this);
$identifierList = [
__CLASS__,
$className,
];

$offset = 0;
$substrCont = substr_count($className, '\\');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/$substrCont/$substrCount


for ($i = 0; $i < $substrCont; $i++) {
$nsPos = strpos($className, '\\', $offset) ?: 0;
$namespace = substr($className, 0, $nsPos);
$offset = strlen($namespace) + 1;
$identifierList[] = $namespace;
}

$nsPos = strpos($className, '\\') ?: 0;
$events->setIdentifiers(array_merge(
[
__CLASS__,
$className,
substr($className, 0, $nsPos)
],
$identifierList,
array_values(class_implements($className)),
(array) $this->eventIdentifier
));
Expand Down
49 changes: 41 additions & 8 deletions test/Controller/ActionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
namespace ZendTest\Mvc\Controller;

use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Zend\EventManager as ZendEventManager;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;
use Zend\EventManager\SharedEventManagerInterface;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\Controller\AbstractController;
use Zend\Mvc\Controller\Plugin\Url;
use Zend\Mvc\Controller\PluginManager;
use Zend\Mvc\InjectApplicationEventInterface;
Expand All @@ -30,10 +31,18 @@

class ActionControllerTest extends TestCase
{
public $controller;
public $event;
public $request;
public $response;
/** @var SampleController */
protected $controller;
/** @var MvcEvent */
protected $event;
/** @var Request */
protected $request;
/** @var null */
protected $response;
/** @var RouteMatch */
protected $routeMatch;
/** @var EventManager */
protected $events;

public function setUp()
{
Expand All @@ -45,13 +54,13 @@ public function setUp()
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);

$this->sharedEvents = new SharedEventManager();
$this->events = $this->createEventManager($this->sharedEvents);
$sharedEvents = new SharedEventManager();
$this->events = $this->createEventManager($sharedEvents);
$this->controller->setEventManager($this->events);
}

/**
* @param SharedEventManager
* @param SharedEventManagerInterface $sharedManager
* @return EventManager
*/
protected function createEventManager(SharedEventManagerInterface $sharedManager)
Expand Down Expand Up @@ -222,4 +231,28 @@ public function testMethodOverloadingShouldInvokePluginAsFunctorIfPossible()
$this->controller->layout('alternate/layout');
$this->assertEquals('alternate/layout', $model->getTemplate());
}

public function testSetEventManagerIdentifiers()
{
$identifierList = $this->events->getIdentifiers();
$this->assertInternalType('array', $identifierList);
$this->assertCount(12, $identifierList);
$this->assertSame(
[
AbstractController::class,
TestAsset\SampleController::class,
'ZendTest',
'ZendTest\Mvc',
'ZendTest\Mvc\Controller',
'ZendTest\Mvc\Controller\TestAsset',
DispatchableInterface::class,
ZendEventManager\EventManagerAwareInterface::class,
ZendEventManager\EventsCapableInterface::class,
InjectApplicationEventInterface::class,
TestAsset\SampleInterface::class,
AbstractActionController::class,
],
$identifierList
);
}
}