|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cubiche package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Cubiche |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cubiche\Core\EventBus\Tests\Units\Event; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Bus\Exception\NotFoundException; |
|
14
|
|
|
use Cubiche\Core\Bus\Middlewares\Locking\LockingMiddleware; |
|
15
|
|
|
use Cubiche\Core\Bus\Tests\Fixtures\FooMessage; |
|
16
|
|
|
use Cubiche\Core\Bus\Tests\Units\BusTests; |
|
17
|
|
|
use Cubiche\Core\EventBus\Event\Event; |
|
18
|
|
|
use Cubiche\Core\EventBus\Event\EventBus; |
|
19
|
|
|
use Cubiche\Core\EventBus\Middlewares\EventDispatcher\EventDispatcherMiddleware; |
|
20
|
|
|
use Cubiche\Core\EventBus\Tests\Fixtures\Event\LoginUserEvent; |
|
21
|
|
|
use Cubiche\Core\EventBus\Tests\Fixtures\Event\LoginUserEventListener; |
|
22
|
|
|
use Cubiche\Core\EventBus\Tests\Fixtures\Event\UserEventSubscriber; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* EventBusTests class. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class EventBusTests extends BusTests |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Test create without event dispatcher middleware. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testCreateWithoutEventDispatcherMiddleware() |
|
35
|
|
|
{ |
|
36
|
|
|
$this |
|
37
|
|
|
->given($middleware = new LockingMiddleware()) |
|
38
|
|
|
->and($eventBus = new EventBus([$middleware])) |
|
39
|
|
|
->then() |
|
40
|
|
|
->exception(function () use ($eventBus) { |
|
41
|
|
|
$eventBus->dispatch(new LoginUserEvent('[email protected]')); |
|
42
|
|
|
}) |
|
43
|
|
|
->isInstanceOf(NotFoundException::class) |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Test dispatch chained middlewares. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testDispatchChainedMiddlewares() |
|
51
|
|
|
{ |
|
52
|
|
|
$this |
|
53
|
|
|
->given($eventBus = EventBus::create()) |
|
54
|
|
|
->and($event = new LoginUserEvent('[email protected]')) |
|
55
|
|
|
->and($eventBus->addListener($event->eventName(), function (LoginUserEvent $event) { |
|
56
|
|
|
$this |
|
57
|
|
|
->string($event->email()) |
|
58
|
|
|
->isEqualTo('[email protected]') |
|
59
|
|
|
; |
|
60
|
|
|
|
|
61
|
|
|
$event->setEmail('[email protected]'); |
|
62
|
|
|
})) |
|
63
|
|
|
->and($eventBus->dispatch($event)) |
|
64
|
|
|
->then() |
|
65
|
|
|
->string($event->email()) |
|
66
|
|
|
->isEqualTo('[email protected]') |
|
67
|
|
|
; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Test dispatch with invalid event. |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testDispatchWithInvalidEvent() |
|
74
|
|
|
{ |
|
75
|
|
|
$this |
|
76
|
|
|
->given($eventBus = EventBus::create()) |
|
77
|
|
|
->then() |
|
78
|
|
|
->exception(function () use ($eventBus) { |
|
79
|
|
|
$eventBus->dispatch(new FooMessage()); |
|
80
|
|
|
}) |
|
81
|
|
|
->isInstanceOf(\InvalidArgumentException::class) |
|
82
|
|
|
; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Test dispatcherMiddleware method. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testDispatcherMiddleware() |
|
89
|
|
|
{ |
|
90
|
|
|
$this |
|
91
|
|
|
->given($eventBus = EventBus::create()) |
|
92
|
|
|
->when($middleware = $eventBus->dispatcherMiddleware()) |
|
93
|
|
|
->then() |
|
94
|
|
|
->object($middleware) |
|
95
|
|
|
->isInstanceOf(EventDispatcherMiddleware::class) |
|
96
|
|
|
; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Test AddListener method. |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testAddListener() |
|
103
|
|
|
{ |
|
104
|
|
|
$this |
|
105
|
|
|
->given($eventBus = EventBus::create()) |
|
106
|
|
|
->and($eventBus->addListener('event.foo', array(new LoginUserEventListener(), 'loginUser'))) |
|
107
|
|
|
->and($eventBus->addListener('event.foo', function (Event $event) { |
|
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
})) |
|
110
|
|
|
->and($eventBus->addListener('event.bar', function (Event $event) { |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
})) |
|
113
|
|
|
->when($listeners = $eventBus->listeners()) |
|
114
|
|
|
->then() |
|
115
|
|
|
->array($listeners->toArray()) |
|
116
|
|
|
->hasKey('event.foo') |
|
117
|
|
|
->hasKey('event.bar') |
|
118
|
|
|
->array($listeners->toArray()) |
|
119
|
|
|
->hasSize(2) |
|
120
|
|
|
->array($listeners->toArray()) |
|
121
|
|
|
->array['event.foo'] |
|
122
|
|
|
->hasSize(2) |
|
123
|
|
|
; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Test RemoveListener method. |
|
128
|
|
|
*/ |
|
129
|
|
|
public function testRemoveListener() |
|
130
|
|
|
{ |
|
131
|
|
|
$this |
|
132
|
|
|
->given($eventBus = EventBus::create()) |
|
133
|
|
|
->and($listener1 = array(new LoginUserEventListener(), 'loginUser')) |
|
134
|
|
|
->and($listener2 = function (Event $event) { |
|
135
|
|
|
return $event->eventName(); |
|
136
|
|
|
}) |
|
137
|
|
|
->and($listener3 = function (Event $event) { |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
}) |
|
140
|
|
|
->and($eventBus->addListener('event.foo', $listener1, 100)) |
|
141
|
|
|
->and($eventBus->addListener('event.foo', $listener2, 50)) |
|
142
|
|
|
->and($eventBus->addListener('event.bar', $listener3)) |
|
143
|
|
|
->then() |
|
144
|
|
|
->boolean($eventBus->hasEventListeners('event.foo')) |
|
145
|
|
|
->isTrue() |
|
146
|
|
|
->and() |
|
147
|
|
|
->when($eventBus->removeListener('event.foo', $listener1)) |
|
148
|
|
|
->then() |
|
149
|
|
|
->boolean($eventBus->hasEventListeners('event.foo')) |
|
150
|
|
|
->isTrue() |
|
151
|
|
|
->and() |
|
152
|
|
|
->when($eventBus->removeListener('event.unknow', $listener2)) |
|
153
|
|
|
->and($eventBus->removeListener('event.foo', $listener2)) |
|
154
|
|
|
->then() |
|
155
|
|
|
->boolean($eventBus->hasEventListeners('event.foo')) |
|
156
|
|
|
->isFalse() |
|
157
|
|
|
; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Test AddSubscriber method. |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testAddSubscriber() |
|
164
|
|
|
{ |
|
165
|
|
|
$this |
|
166
|
|
|
->given($eventBus = EventBus::create()) |
|
167
|
|
|
->and($eventBus->addSubscriber(new UserEventSubscriber())) |
|
168
|
|
|
->then() |
|
169
|
|
|
->boolean($eventBus->hasListeners()) |
|
170
|
|
|
->isTrue() |
|
171
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::FOO_EVENT)) |
|
172
|
|
|
->isTrue() |
|
173
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::BAR_EVENT)) |
|
174
|
|
|
->isTrue() |
|
175
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::USER_LOGIN)) |
|
176
|
|
|
->isTrue() |
|
177
|
|
|
; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Test RemoveSubscriber method. |
|
182
|
|
|
*/ |
|
183
|
|
|
public function testRemoveSubscriber() |
|
184
|
|
|
{ |
|
185
|
|
|
$this |
|
186
|
|
|
->given($eventBus = EventBus::create()) |
|
187
|
|
|
->and($subscriber = new UserEventSubscriber()) |
|
188
|
|
|
->and($eventBus->addSubscriber($subscriber)) |
|
189
|
|
|
->then() |
|
190
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::FOO_EVENT)) |
|
191
|
|
|
->isTrue() |
|
192
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::BAR_EVENT)) |
|
193
|
|
|
->isTrue() |
|
194
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::USER_LOGIN)) |
|
195
|
|
|
->isTrue() |
|
196
|
|
|
->and() |
|
197
|
|
|
->when($eventBus->removeSubscriber($subscriber)) |
|
198
|
|
|
->then() |
|
199
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::FOO_EVENT)) |
|
200
|
|
|
->isFalse() |
|
201
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::BAR_EVENT)) |
|
202
|
|
|
->isFalse() |
|
203
|
|
|
->boolean($eventBus->hasEventListeners(UserEventSubscriber::USER_LOGIN)) |
|
204
|
|
|
->isFalse() |
|
205
|
|
|
; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.