-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathUseCasesTest.php
214 lines (165 loc) · 6.85 KB
/
UseCasesTest.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace Enqueue\AsyncEventDispatcher\Tests\Functional;
use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher;
use Enqueue\AsyncEventDispatcher\AsyncListener;
use Enqueue\AsyncEventDispatcher\AsyncProcessor;
use Enqueue\AsyncEventDispatcher\SimpleRegistry;
use Enqueue\Bundle\Tests\Functional\App\TestAsyncEventTransformer;
use Enqueue\Fs\FsConnectionFactory;
use Interop\Queue\Context;
use Interop\Queue\Processor;
use Interop\Queue\Queue;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Filesystem\Filesystem;
/**
* @group functional
*/
class UseCasesTest extends TestCase
{
/**
* @var Context
*/
protected $context;
/**
* @var Queue
*/
protected $queue;
/**
* @var EventDispatcherInterface
*/
protected $dispatcher;
/**
* @var AsyncEventDispatcher
*/
protected $asyncDispatcher;
/**
* @var callable
*/
protected $asyncListener;
/**
* @var AsyncProcessor
*/
protected $asyncProcessor;
protected function setUp(): void
{
(new Filesystem())->remove(__DIR__.'/queues/');
// it could be any other queue-interop/queue-interop compatible context.
$this->context = $context = (new FsConnectionFactory('file://'.__DIR__.'/queues'))->createContext();
$this->queue = $queue = $context->createQueue('symfony_events');
$registry = new SimpleRegistry(
[
'test_async' => 'test_async',
'test_async_from_async' => 'test_async',
],
[
'test_async' => new TestAsyncEventTransformer($context),
]);
$asyncListener = new AsyncListener($context, $registry, $queue);
$this->asyncListener = function ($event, $name, $dispatcher) use ($asyncListener) {
$asyncListener->onEvent($event, $name);
$consumer = $this->context->createConsumer($this->queue);
$message = $consumer->receiveNoWait();
if ($message) {
$consumer->reject($message, true);
echo "Send message for event: $name\n";
}
};
$this->dispatcher = $dispatcher = new EventDispatcher();
$this->asyncDispatcher = $asyncDispatcher = new AsyncEventDispatcher($dispatcher, $asyncListener);
$this->asyncProcessor = new AsyncProcessor($registry, $asyncDispatcher);
}
public function testShouldDispatchBothAsyncEventAndSyncOne()
{
$this->dispatcher->addListener('test_async', function () {
echo "Sync event\n";
});
$this->dispatcher->addListener('test_async', $this->asyncListener);
$this->asyncDispatcher->addListener('test_async', function ($event, $eventName) {
echo "Async event\n";
});
$this->dispatch($this->dispatcher, new GenericEvent(), 'test_async');
$this->processMessages();
$this->expectOutputString("Sync event\nSend message for event: test_async\nAsync event\n");
}
public function testShouldDispatchBothAsyncEventAndSyncOneFromWhenDispatchedFromInsideAnotherEvent()
{
$this->dispatcher->addListener('foo', function ($event, $name, EventDispatcherInterface $dispatcher) {
echo "Foo event\n";
$this->dispatch($dispatcher, new GenericEvent(), 'test_async');
});
$this->dispatcher->addListener('test_async', function () {
echo "Sync event\n";
});
$this->dispatcher->addListener('test_async', $this->asyncListener);
$this->asyncDispatcher->addListener('test_async', function ($event, $eventName) {
echo "Async event\n";
});
$this->dispatch($this->dispatcher, new GenericEvent(), 'foo');
$this->processMessages();
$this->expectOutputString("Foo event\nSync event\nSend message for event: test_async\nAsync event\n");
}
public function testShouldDispatchOtherAsyncEventFromAsyncEvent()
{
$this->dispatcher->addListener('test_async', $this->asyncListener);
$this->dispatcher->addListener('test_async_from_async', $this->asyncListener);
$this->asyncDispatcher->addListener('test_async', function ($event, $eventName, EventDispatcherInterface $dispatcher) {
echo "Async event\n";
$this->dispatch($dispatcher, new GenericEvent(), 'test_async_from_async');
});
$this->dispatcher->addListener('test_async_from_async', function ($event, $eventName, EventDispatcherInterface $dispatcher) {
echo "Async event from event\n";
});
$this->dispatch($this->dispatcher, new GenericEvent(), 'test_async');
$this->processMessages();
$this->processMessages();
$this->expectOutputString("Send message for event: test_async\nAsync event\nSend message for event: test_async_from_async\nAsync event from event\n");
}
public function testShouldDispatchSyncListenerIfDispatchedFromAsycListner()
{
$this->dispatcher->addListener('test_async', $this->asyncListener);
$this->dispatcher->addListener('sync', function () {
echo "Sync event\n";
});
$this->asyncDispatcher->addListener('test_async', function ($event, $eventName, EventDispatcherInterface $dispatcher) {
echo "Async event\n";
$this->dispatch($dispatcher, new GenericEvent(), 'sync');
});
$this->dispatch($this->dispatcher, new GenericEvent(), 'test_async');
$this->processMessages();
$this->expectOutputString("Send message for event: test_async\nAsync event\nSync event\n");
}
private function dispatch(EventDispatcherInterface $dispatcher, $event, $eventName): void
{
if (!class_exists(Event::class)) {
// Symfony 5
$dispatcher->dispatch($event, $eventName);
} else {
// Symfony < 5
$dispatcher->dispatch($eventName, $event);
}
}
private function processMessages()
{
$consumer = $this->context->createConsumer($this->queue);
if ($message = $consumer->receiveNoWait()) {
$result = $this->asyncProcessor->process($message, $this->context);
switch ((string) $result) {
case Processor::ACK:
$consumer->acknowledge($message);
break;
case Processor::REJECT:
$consumer->reject($message);
break;
case Processor::REQUEUE:
$consumer->reject($message, true);
break;
default:
throw new \LogicException('Result is not supported');
}
}
}
}