Skip to content

Commit

Permalink
Merge pull request #17 from dsoriano/master
Browse files Browse the repository at this point in the history
Update the way that events are triggered
  • Loading branch information
kokspflanze authored Jul 28, 2020
2 parents 2672cdb + 08d9954 commit ac7cce6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 21 deletions.
9 changes: 3 additions & 6 deletions src/BjyAuthorize/Guard/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 3 additions & 6 deletions src/BjyAuthorize/Guard/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
13 changes: 7 additions & 6 deletions tests/BjyAuthorizeTest/Guard/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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');
Expand Down
7 changes: 4 additions & 3 deletions tests/BjyAuthorizeTest/Guard/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit ac7cce6

Please sign in to comment.