forked from Jeroeny/enqueue-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmqpContextTest.php
264 lines (217 loc) · 8.17 KB
/
AmqpContextTest.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace Enqueue\AmqpExt\Tests;
use Enqueue\AmqpExt\AmqpConsumer;
use Enqueue\AmqpExt\AmqpContext;
use Enqueue\AmqpExt\AmqpProducer;
use Enqueue\AmqpExt\AmqpSubscriptionConsumer;
use Enqueue\Null\NullQueue;
use Enqueue\Null\NullTopic;
use Enqueue\Test\ClassExtensionTrait;
use Interop\Amqp\Impl\AmqpMessage;
use Interop\Amqp\Impl\AmqpQueue;
use Interop\Amqp\Impl\AmqpTopic;
use Interop\Queue\Context;
use Interop\Queue\Exception\InvalidDestinationException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
class AmqpContextTest extends TestCase
{
use ClassExtensionTrait;
public function testShouldImplementQueueInteropContextInterface()
{
$this->assertClassImplements(Context::class, AmqpContext::class);
}
public function testCouldBeConstructedWithExtChannelAsFirstArgument()
{
new AmqpContext($this->createExtChannelMock());
}
public function testCouldBeConstructedWithExtChannelCallbackFactoryAsFirstArgument()
{
new AmqpContext(function () {
return $this->createExtChannelMock();
});
}
public function testThrowIfNeitherCallbackNorExtChannelAsFirstArgument()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The extChannel argument must be either AMQPChannel or callable that return AMQPChannel.');
new AmqpContext(new \stdClass());
}
public function testShouldReturnAmqpMessageOnCreateMessageCallWithoutArguments()
{
$context = new AmqpContext($this->createExtChannelMock());
$message = $context->createMessage();
$this->assertInstanceOf(AmqpMessage::class, $message);
$this->assertSame('', $message->getBody());
$this->assertSame([], $message->getHeaders());
$this->assertSame([], $message->getProperties());
}
public function testShouldReturnAmqpMessageOnCreateMessageCal()
{
$context = new AmqpContext($this->createExtChannelMock());
$message = $context->createMessage('theBody', ['foo' => 'fooVal'], ['bar' => 'barVal']);
$this->assertInstanceOf(AmqpMessage::class, $message);
$this->assertSame('theBody', $message->getBody());
$this->assertSame(['bar' => 'barVal'], $message->getHeaders());
$this->assertSame(['foo' => 'fooVal'], $message->getProperties());
}
public function testShouldCreateTopicWithGivenName()
{
$context = new AmqpContext($this->createExtChannelMock());
$topic = $context->createTopic('theName');
$this->assertInstanceOf(AmqpTopic::class, $topic);
$this->assertSame('theName', $topic->getTopicName());
$this->assertSame(AmqpTopic::FLAG_NOPARAM, $topic->getFlags());
$this->assertSame([], $topic->getArguments());
}
public function testShouldCreateQueueWithGivenName()
{
$context = new AmqpContext($this->createExtChannelMock());
$queue = $context->createQueue('theName');
$this->assertInstanceOf(AmqpQueue::class, $queue);
$this->assertSame('theName', $queue->getQueueName());
$this->assertSame(AmqpQueue::FLAG_NOPARAM, $queue->getFlags());
$this->assertSame([], $queue->getArguments());
$this->assertNull($queue->getConsumerTag());
}
public function testShouldReturnAmqpProducer()
{
$context = new AmqpContext($this->createExtChannelMock());
$producer = $context->createProducer();
$this->assertInstanceOf(AmqpProducer::class, $producer);
}
public function testShouldReturnAmqpConsumerForGivenQueue()
{
$context = new AmqpContext($this->createExtChannelMock());
$queue = new AmqpQueue('aName');
$consumer = $context->createConsumer($queue);
$this->assertInstanceOf(AmqpConsumer::class, $consumer);
$this->assertAttributeSame($context, 'context', $consumer);
$this->assertAttributeSame($queue, 'queue', $consumer);
}
public function testShouldThrowIfNotAmqpQueueGivenOnCreateConsumerCall()
{
$context = new AmqpContext($this->createExtChannelMock());
$this->expectException(InvalidDestinationException::class);
$this->expectExceptionMessage('The destination must be an instance of Interop\Amqp\AmqpQueue but got Enqueue\Null\NullQueue.');
$context->createConsumer(new NullQueue('aName'));
}
public function testShouldThrowIfNotAmqpTopicGivenOnCreateConsumerCall()
{
$context = new AmqpContext($this->createExtChannelMock());
$this->expectException(InvalidDestinationException::class);
$this->expectExceptionMessage('The destination must be an instance of Interop\Amqp\AmqpTopic but got Enqueue\Null\NullTopic.');
$context->createConsumer(new NullTopic('aName'));
}
public function testShouldDoNothingIfConnectionAlreadyClosed()
{
$extConnectionMock = $this->createExtConnectionMock();
$extConnectionMock
->expects($this->once())
->method('isConnected')
->willReturn(false)
;
$extConnectionMock
->expects($this->never())
->method('isPersistent')
;
$extConnectionMock
->expects($this->never())
->method('pdisconnect')
;
$extConnectionMock
->expects($this->never())
->method('disconnect')
;
$extChannelMock = $this->createExtChannelMock();
$extChannelMock
->expects($this->once())
->method('getConnection')
->willReturn($extConnectionMock)
;
$context = new AmqpContext($extChannelMock);
$context->close();
}
public function testShouldCloseNotPersistedConnection()
{
$extConnectionMock = $this->createExtConnectionMock();
$extConnectionMock
->expects($this->once())
->method('isConnected')
->willReturn(true)
;
$extConnectionMock
->expects($this->once())
->method('isPersistent')
->willReturn(false)
;
$extConnectionMock
->expects($this->never())
->method('pdisconnect')
;
$extConnectionMock
->expects($this->once())
->method('disconnect')
;
$extChannelMock = $this->createExtChannelMock();
$extChannelMock
->expects($this->once())
->method('getConnection')
->willReturn($extConnectionMock)
;
$context = new AmqpContext($extChannelMock);
$context->close();
}
public function testShouldClosePersistedConnection()
{
$extConnectionMock = $this->createExtConnectionMock();
$extConnectionMock
->expects($this->once())
->method('isConnected')
->willReturn(true)
;
$extConnectionMock
->expects($this->once())
->method('isPersistent')
->willReturn(true)
;
$extConnectionMock
->expects($this->once())
->method('pdisconnect')
;
$extConnectionMock
->expects($this->never())
->method('disconnect')
;
$extChannelMock = $this->createExtChannelMock();
$extChannelMock
->expects($this->once())
->method('getConnection')
->willReturn($extConnectionMock)
;
$context = new AmqpContext($extChannelMock);
$context->close();
}
public function testShouldReturnExpectedSubscriptionConsumerInstance()
{
$context = new AmqpContext($this->createExtChannelMock());
$this->assertInstanceOf(AmqpSubscriptionConsumer::class, $context->createSubscriptionConsumer());
}
/**
* @return MockObject|\AMQPChannel
*/
private function createExtChannelMock()
{
return $this->createMock(\AMQPChannel::class);
}
/**
* @return MockObject|\AMQPChannel
*/
private function createExtConnectionMock()
{
return $this->getMockBuilder(\AMQPConnection::class)
->setMethods(['isPersistent', 'isConnected', 'pdisconnect', 'disconnect'])
->getMock()
;
}
}