-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathAsyncListenerTest.php
165 lines (136 loc) · 4.59 KB
/
AsyncListenerTest.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
<?php
namespace Enqueue\AsyncEventDispatcher\Tests;
use Enqueue\AsyncEventDispatcher\AsyncListener;
use Enqueue\AsyncEventDispatcher\EventTransformer;
use Enqueue\AsyncEventDispatcher\Registry;
use Enqueue\NoEffect\NullMessage;
use Enqueue\NoEffect\NullQueue;
use Enqueue\Test\ClassExtensionTrait;
use Enqueue\Test\ReadAttributeTrait;
use Interop\Queue\Context;
use Interop\Queue\Producer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Contracts\EventDispatcher\Event;
class AsyncListenerTest extends TestCase
{
use ClassExtensionTrait;
use ReadAttributeTrait;
public function testCouldBeConstructedWithContextAndRegistryAndEventQueueAsString()
{
$eventQueue = new NullQueue('symfony_events');
$context = $this->createContextMock();
$context
->expects($this->once())
->method('createQueue')
->with('symfony_events')
->willReturn($eventQueue)
;
$listener = new AsyncListener($context, $this->createRegistryMock(), 'symfony_events');
$this->assertAttributeSame($eventQueue, 'eventQueue', $listener);
}
public function testCouldBeConstructedWithContextAndRegistryAndQueue()
{
$eventQueue = new NullQueue('symfony_events');
$context = $this->createContextMock();
$context
->expects($this->never())
->method('createQueue')
;
$listener = new AsyncListener($context, $this->createRegistryMock(), $eventQueue);
$this->assertAttributeSame($eventQueue, 'eventQueue', $listener);
}
public function testShouldDoNothingIfSyncModeOn()
{
$producer = $this->createContextMock();
$producer
->expects($this->never())
->method('createProducer')
;
$registry = $this->createRegistryMock();
$registry
->expects($this->never())
->method('getTransformerNameForEvent')
;
$listener = new AsyncListener($producer, $registry, new NullQueue('symfony_events'));
$listener->syncMode('fooEvent');
$listener->onEvent(new Event(), 'fooEvent');
$listener->onEvent(new GenericEvent(), 'fooEvent');
}
public function testShouldSendMessageIfSyncModeOff()
{
$event = new GenericEvent();
$message = new NullMessage('serializedEvent');
$eventQueue = new NullQueue('symfony_events');
$transformerMock = $this->createEventTransformerMock();
$transformerMock
->expects($this->once())
->method('toMessage')
->with('fooEvent', $this->identicalTo($event))
->willReturn($message)
;
$registry = $this->createRegistryMock();
$registry
->expects($this->once())
->method('getTransformerNameForEvent')
->with('fooEvent')
->willReturn('fooTrans')
;
$registry
->expects($this->once())
->method('getTransformer')
->with('fooTrans')
->willReturn($transformerMock)
;
$producer = $this->createProducerMock();
$producer
->expects($this->once())
->method('send')
->with($this->identicalTo($eventQueue), $this->identicalTo($message))
;
$context = $this->createContextMock();
$context
->expects($this->once())
->method('createProducer')
->with()
->willReturn($producer)
;
$listener = new AsyncListener($context, $registry, $eventQueue);
$listener->onEvent($event, 'fooEvent');
$this->assertEquals('serializedEvent', $message->getBody());
$this->assertEquals([], $message->getHeaders());
$this->assertEquals([
'event_name' => 'fooEvent',
'transformer_name' => 'fooTrans',
], $message->getProperties());
}
/**
* @return MockObject|EventTransformer
*/
private function createEventTransformerMock()
{
return $this->createMock(EventTransformer::class);
}
/**
* @return MockObject|Producer
*/
private function createProducerMock()
{
return $this->createMock(Producer::class);
}
/**
* @return MockObject|Context
*/
private function createContextMock()
{
return $this->createMock(Context::class);
}
/**
* @return MockObject|Registry
*/
private function createRegistryMock()
{
return $this->createMock(Registry::class);
}
}