-
Notifications
You must be signed in to change notification settings - Fork 56
/
RouteGuardTest.php
73 lines (58 loc) · 2.59 KB
/
RouteGuardTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace ProophTest\ServiceBus\Plugin\Guard;
use PHPUnit_Framework_TestCase as TestCase;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ActionEventEmitter;
use Prooph\Common\Event\ListenerHandler;
use Prooph\ServiceBus\MessageBus;
use Prooph\ServiceBus\Plugin\Guard\AuthorizationService;
use Prooph\ServiceBus\Plugin\Guard\RouteGuard;
/**
* Class RouteGuardTest
* @package ProophTest\ServiceBus\Plugin\Guard
*/
final class RouteGuardTest extends TestCase
{
/**
* @test
*/
public function it_attaches_to_action_event_emitter()
{
$listenerHandler = $this->prophesize(ListenerHandler::class);
$authorizationService = $this->prophesize(AuthorizationService::class);
$routeGuard = new RouteGuard($authorizationService->reveal());
$actionEventEmitter = $this->prophesize(ActionEventEmitter::class);
$actionEventEmitter
->attachListener(MessageBus::EVENT_ROUTE, [$routeGuard, 'onRoute'], 1000)
->willReturn($listenerHandler->reveal());
$routeGuard->attach($actionEventEmitter->reveal());
}
/**
* @test
*/
public function it_allows_when_authorization_service_grants_access()
{
$authorizationService = $this->prophesize(AuthorizationService::class);
$authorizationService->isGranted('test_event', new \stdClass())->willReturn(true);
$actionEvent = $this->prophesize(ActionEvent::class);
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME)->willReturn('test_event');
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE)->willReturn(new \stdClass());
$routeGuard = new RouteGuard($authorizationService->reveal());
$this->assertNull($routeGuard->onRoute($actionEvent->reveal()));
}
/**
* @test
* @expectedException Prooph\ServiceBus\Plugin\Guard\UnauthorizedException
*/
public function it_stops_propagation_and_throws_unauthorizedexception_when_authorization_service_denies_access()
{
$authorizationService = $this->prophesize(AuthorizationService::class);
$authorizationService->isGranted('test_event', new \stdClass())->willReturn(false);
$actionEvent = $this->prophesize(ActionEvent::class);
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE_NAME)->willReturn('test_event');
$actionEvent->getParam(MessageBus::EVENT_PARAM_MESSAGE)->willReturn(new \stdClass());
$actionEvent->stopPropagation(true)->willReturn(null);
$routeGuard = new RouteGuard($authorizationService->reveal());
$routeGuard->onRoute($actionEvent->reveal());
}
}