From 46563e3b363c2bb8537ecf31e6e7fd79c7e820d2 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 28 Jul 2020 21:33:56 +0200 Subject: [PATCH 1/2] Update the way that events are triggered --- src/BjyAuthorize/Guard/Controller.php | 9 +++------ src/BjyAuthorize/Guard/Route.php | 9 +++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/BjyAuthorize/Guard/Controller.php b/src/BjyAuthorize/Guard/Controller.php index 78e7791..1683ccc 100644 --- a/src/BjyAuthorize/Guard/Controller.php +++ b/src/BjyAuthorize/Guard/Controller.php @@ -105,13 +105,10 @@ public function onDispatch(MvcEvent $event) /* @var $app \Laminas\Mvc\ApplicationInterface */ $app = $event->getTarget(); $eventManager = $app->getEventManager(); - $eventManager->setEventPrototype($event); - $results = $eventManager->trigger( - MvcEvent::EVENT_DISPATCH_ERROR, - null, - $event->getParams() - ); + $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); + $results = $eventManager->triggerEvent($event); + $return = $results->last(); if (! $return) { return $event->getResult(); diff --git a/src/BjyAuthorize/Guard/Route.php b/src/BjyAuthorize/Guard/Route.php index 0d8a193..ce6d58b 100644 --- a/src/BjyAuthorize/Guard/Route.php +++ b/src/BjyAuthorize/Guard/Route.php @@ -67,13 +67,10 @@ public function onRoute(MvcEvent $event) /* @var $app \Laminas\Mvc\Application */ $app = $event->getTarget(); $eventManager = $app->getEventManager(); - $eventManager->setEventPrototype($event); - $results = $eventManager->trigger( - MvcEvent::EVENT_DISPATCH_ERROR, - null, - $event->getParams() - ); + $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); + $results = $eventManager->triggerEvent($event); + $return = $results->last(); if (! $return) { return $event->getResult(); From 08d9954b69940fba3b3c7e117d3b2ef3cf709074 Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 28 Jul 2020 21:44:50 +0200 Subject: [PATCH 2/2] Fix unit tests --- tests/BjyAuthorizeTest/Guard/ControllerTest.php | 13 +++++++------ tests/BjyAuthorizeTest/Guard/RouteTest.php | 7 ++++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/BjyAuthorizeTest/Guard/ControllerTest.php b/tests/BjyAuthorizeTest/Guard/ControllerTest.php index 06ac11a..0987e45 100644 --- a/tests/BjyAuthorizeTest/Guard/ControllerTest.php +++ b/tests/BjyAuthorizeTest/Guard/ControllerTest.php @@ -149,7 +149,7 @@ public function testGetResourceName() public function testOnDispatchWithValidController() { $event = $this->createMvcEvent('test-controller'); - $event->getTarget()->getEventManager()->expects($this->never())->method('trigger'); + $event->getTarget()->getEventManager()->expects($this->never())->method('triggerEvent'); $this ->authorize ->expects($this->any()) @@ -171,7 +171,7 @@ function ($resource) { public function testOnDispatchWithValidControllerAndAction() { $event = $this->createMvcEvent('test-controller', 'test-action'); - $event->getTarget()->getEventManager()->expects($this->never())->method('trigger'); + $event->getTarget()->getEventManager()->expects($this->never())->method('triggerEvent'); $this ->authorize ->expects($this->any()) @@ -193,7 +193,7 @@ function ($resource) { public function testOnDispatchWithValidControllerAndMethod() { $event = $this->createMvcEvent('test-controller', null, 'PUT'); - $event->getTarget()->getEventManager()->expects($this->never())->method('trigger'); + $event->getTarget()->getEventManager()->expects($this->never())->method('triggerEvent'); $this ->authorize ->expects($this->any()) @@ -215,7 +215,7 @@ function ($resource) { public function testOnDispatchWithValidControllerAction() { $event = $this->createMvcEvent('test-controller', 'test-action'); - $event->getTarget()->getEventManager()->expects($this->never())->method('trigger'); + $event->getTarget()->getEventManager()->expects($this->never())->method('triggerEvent'); $this ->authorize ->expects($this->any()) @@ -251,12 +251,13 @@ public function testOnDispatchWithInvalidResource() $responseCollection = $this->getMockBuilder(\Laminas\EventManager\ResponseCollection::class) ->getMock(); + $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); $event ->getTarget() ->getEventManager() ->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR, null, []) + ->method('triggerEvent') + ->with($event) ->willReturn($responseCollection); $this->assertNull($this->controllerGuard->onDispatch($event), 'Does not stop event propagation'); diff --git a/tests/BjyAuthorizeTest/Guard/RouteTest.php b/tests/BjyAuthorizeTest/Guard/RouteTest.php index 11c5b27..16eb924 100644 --- a/tests/BjyAuthorizeTest/Guard/RouteTest.php +++ b/tests/BjyAuthorizeTest/Guard/RouteTest.php @@ -176,7 +176,7 @@ public function testGetRulesWithAssertion() public function testOnRouteWithValidRoute() { $event = $this->createMvcEvent('test-route'); - $event->getTarget()->getEventManager()->expects($this->never())->method('trigger'); + $event->getTarget()->getEventManager()->expects($this->never())->method('triggerEvent'); $this ->authorize ->expects($this->any()) @@ -216,12 +216,13 @@ public function testOnRouteWithInvalidResource() $responseCollection = $this->getMockBuilder(\Laminas\EventManager\ResponseCollection::class) ->getMock(); + $event->setName(MvcEvent::EVENT_DISPATCH_ERROR); $event ->getTarget() ->getEventManager() ->expects($this->once()) - ->method('trigger') - ->with(MvcEvent::EVENT_DISPATCH_ERROR, null, $event->getParams()) + ->method('triggerEvent') + ->with($event) ->willReturn($responseCollection); $this->assertNull($this->routeGuard->onRoute($event), 'Does not stop event propagation');