1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter\PhpAmqpLib; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
7
|
|
|
use AMQPAL\Options; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
|
10
|
|
|
class ExchangeTest extends \PHPUnit_Framework_TestCase |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public function testSetOptions() |
14
|
|
|
{ |
15
|
|
|
$options = $this->getDefaultOptionsProphet(); |
16
|
|
|
|
17
|
|
|
$exchange = new Exchange(); |
18
|
|
|
|
19
|
|
|
static::assertSame($exchange, $exchange->setOptions($options->reveal())); |
20
|
|
|
static::assertSame($options->reveal(), $exchange->getOptions()); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testSetOptionsWithArray() |
24
|
|
|
{ |
25
|
|
|
$options = ['name' => 'exchangeName', 'type' => 'exchangeType']; |
26
|
|
|
|
27
|
|
|
$exchange = new Exchange(); |
28
|
|
|
|
29
|
|
|
static::assertSame($exchange, $exchange->setOptions($options)); |
30
|
|
|
$exchangeOptions = $exchange->getOptions(); |
31
|
|
|
static::assertInstanceOf(Options\ExchangeOptions::class, $exchangeOptions); |
32
|
|
|
static::assertEquals('exchangeName', $exchangeOptions->getName()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testDeclareExchange() |
36
|
|
|
{ |
37
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
38
|
|
|
$channel = $this->prophesize(Channel::class); |
39
|
|
|
$options = $this->getDefaultOptionsProphet(); |
40
|
|
|
|
41
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
42
|
|
|
$adapterChannel->exchange_declare( |
43
|
|
|
'exchangeName', |
44
|
|
|
'exchangeType', |
45
|
|
|
true, |
46
|
|
|
true, |
47
|
|
|
true, |
48
|
|
|
true, |
49
|
|
|
true, |
50
|
|
|
['arg1' => 'value1'] |
51
|
|
|
)->shouldBeCalled(); |
52
|
|
|
|
53
|
|
|
$exchange = new Exchange(); |
54
|
|
|
$exchange->setChannel($channel->reveal()); |
55
|
|
|
$exchange->setOptions($options->reveal()); |
56
|
|
|
|
57
|
|
|
static::assertSame($exchange, $exchange->declareExchange()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function testDeclareExchangeWithNoDeclare() |
61
|
|
|
{ |
62
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
63
|
|
|
$channel = $this->prophesize(Channel::class); |
64
|
|
|
$options = $this->getDefaultOptionsProphet(); |
65
|
|
|
|
66
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
67
|
|
|
|
68
|
|
|
$exchange = new Exchange(); |
69
|
|
|
$exchange->setChannel($channel->reveal()); |
70
|
|
|
$exchange->setOptions($options->reveal()); |
71
|
|
|
|
72
|
|
|
static::assertSame($exchange, $exchange->declareExchange()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return \Prophecy\Prophecy\ObjectProphecy|Options\ExchangeOptions |
77
|
|
|
*/ |
78
|
|
|
protected function getDefaultOptionsProphet() |
79
|
|
|
{ |
80
|
|
|
$options = $this->prophesize(Options\ExchangeOptions::class); |
81
|
|
|
|
82
|
|
|
$options->isDurable()->willReturn(true); |
83
|
|
|
$options->isPassive()->willReturn(true); |
84
|
|
|
$options->isAutoDelete()->willReturn(true); |
85
|
|
|
$options->isInternal()->willReturn(true); |
86
|
|
|
$options->isNoWait()->willReturn(true); |
87
|
|
|
$options->getType()->willReturn('exchangeType'); |
88
|
|
|
$options->getName()->willReturn('exchangeName'); |
89
|
|
|
$options->getArguments()->willReturn(['arg1' => 'value1']); |
90
|
|
|
|
91
|
|
|
return $options; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @dataProvider deleteProvider() |
96
|
|
|
*/ |
97
|
|
|
public function testDelete($ifUnused, $noWait) |
98
|
|
|
{ |
99
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
100
|
|
|
$channel = $this->prophesize(Channel::class); |
101
|
|
|
$options = $this->getDefaultOptionsProphet(); |
102
|
|
|
|
103
|
|
|
$adapterChannel->exchange_delete('exchangeName', $ifUnused, $noWait)->shouldBeCalled(); |
104
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
105
|
|
|
|
106
|
|
|
$exchange = new Exchange(); |
107
|
|
|
$exchange->setChannel($channel->reveal()); |
108
|
|
|
$exchange->setOptions($options->reveal()); |
109
|
|
|
|
110
|
|
|
static::assertSame($exchange, $exchange->delete($ifUnused, $noWait)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testBind() |
114
|
|
|
{ |
115
|
|
|
$arguments = ['arg2' => 'value2']; |
116
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
117
|
|
|
|
118
|
|
|
$channel = $this->prophesize(Channel::class); |
119
|
|
|
$options = $this->getDefaultOptionsProphet(); |
120
|
|
|
|
121
|
|
|
$adapterChannel->exchange_bind('exchangeName', 'exchange2bindName', 'routingKey', false, $arguments) |
122
|
|
|
->shouldBeCalled(); |
123
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
124
|
|
|
|
125
|
|
|
$exchange = new Exchange(); |
126
|
|
|
$exchange->setChannel($channel->reveal()); |
127
|
|
|
$exchange->setOptions($options->reveal()); |
128
|
|
|
|
129
|
|
|
static::assertSame($exchange, $exchange->bind('exchange2bindName', 'routingKey', false, $arguments)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testBindWithNullRoutingKey() |
133
|
|
|
{ |
134
|
|
|
$arguments = ['arg2' => 'value2']; |
135
|
|
|
|
136
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
137
|
|
|
|
138
|
|
|
$channel = $this->prophesize(Channel::class); |
139
|
|
|
$options = $this->getDefaultOptionsProphet(); |
140
|
|
|
|
141
|
|
|
$adapterChannel->exchange_bind('exchangeName', 'exchange2bindName', '', false, $arguments) |
142
|
|
|
->shouldBeCalled(); |
143
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
144
|
|
|
|
145
|
|
|
$exchange = new Exchange(); |
146
|
|
|
$exchange->setChannel($channel->reveal()); |
147
|
|
|
$exchange->setOptions($options->reveal()); |
148
|
|
|
|
149
|
|
|
static::assertSame($exchange, $exchange->bind('exchange2bindName', null, false, $arguments)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testUnbind() |
153
|
|
|
{ |
154
|
|
|
$arguments = ['arg2' => 'value2']; |
155
|
|
|
|
156
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
157
|
|
|
|
158
|
|
|
$channel = $this->prophesize(Channel::class); |
159
|
|
|
$options = $this->getDefaultOptionsProphet(); |
160
|
|
|
|
161
|
|
|
$adapterChannel->exchange_unbind('exchangeName', 'exchange2bindName', 'routingKey', $arguments) |
162
|
|
|
->shouldBeCalled(); |
163
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
164
|
|
|
|
165
|
|
|
$exchange = new Exchange(); |
166
|
|
|
$exchange->setChannel($channel->reveal()); |
167
|
|
|
$exchange->setOptions($options->reveal()); |
168
|
|
|
|
169
|
|
|
static::assertSame($exchange, $exchange->unbind('exchange2bindName', 'routingKey', $arguments)); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testUnbindWithNullRoutingKey() |
173
|
|
|
{ |
174
|
|
|
$arguments = ['arg2' => 'value2']; |
175
|
|
|
|
176
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
177
|
|
|
|
178
|
|
|
$channel = $this->prophesize(Channel::class); |
179
|
|
|
$options = $this->getDefaultOptionsProphet(); |
180
|
|
|
|
181
|
|
|
$adapterChannel->exchange_unbind('exchangeName', 'exchange2bindName', '', $arguments) |
182
|
|
|
->shouldBeCalled(); |
183
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
184
|
|
|
|
185
|
|
|
$exchange = new Exchange(); |
186
|
|
|
$exchange->setChannel($channel->reveal()); |
187
|
|
|
$exchange->setOptions($options->reveal()); |
188
|
|
|
|
189
|
|
|
static::assertSame($exchange, $exchange->unbind('exchange2bindName', null, $arguments)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @dataProvider publishProvider() |
194
|
|
|
*/ |
195
|
|
|
public function testPublish($message, $routingKey, $mandatory, $immediate, $attributes) |
196
|
|
|
{ |
197
|
|
|
|
198
|
|
|
$adapterChannel = $this->prophesize(AMQPChannel::class); |
199
|
|
|
|
200
|
|
|
$channel = $this->prophesize(Channel::class); |
201
|
|
|
$options = $this->getDefaultOptionsProphet(); |
202
|
|
|
|
203
|
|
|
$adapterChannel->basic_publish(Argument::that(function (AMQPMessage $msgObject) use ($message) { |
204
|
|
|
return $msgObject instanceof AMQPMessage && |
205
|
|
|
$msgObject->getBody() === $message && |
206
|
|
|
$msgObject->get('delivery_mode') == 5; |
207
|
|
|
}), 'exchangeName', $routingKey ?: '', $mandatory, $immediate) |
208
|
|
|
->shouldBeCalled(); |
209
|
|
|
$channel->getResource()->willReturn($adapterChannel); |
210
|
|
|
|
211
|
|
|
$exchange = new Exchange(); |
212
|
|
|
$exchange->setChannel($channel->reveal()); |
213
|
|
|
$exchange->setOptions($options->reveal()); |
214
|
|
|
|
215
|
|
|
static::assertSame($exchange, $exchange->publish($message, $routingKey, $mandatory, $immediate, $attributes)); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function testSetAndGetChannel() |
219
|
|
|
{ |
220
|
|
|
$channel = $this->prophesize(Channel::class); |
221
|
|
|
|
222
|
|
|
$exchange = new Exchange(); |
223
|
|
|
static::assertSame($exchange, $exchange->setChannel($channel->reveal())); |
224
|
|
|
static::assertSame($channel->reveal(), $exchange->getChannel()); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
public function testGetConnection() |
228
|
|
|
{ |
229
|
|
|
$channel = $this->prophesize(Channel::class); |
230
|
|
|
$connection = $this->prophesize(Connection::class); |
231
|
|
|
|
232
|
|
|
$channel->getConnection()->willReturn($connection); |
233
|
|
|
|
234
|
|
|
$exchange = new Exchange(); |
235
|
|
|
static::assertSame($exchange, $exchange->setChannel($channel->reveal())); |
236
|
|
|
static::assertSame($connection->reveal(), $exchange->getConnection()); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function deleteProvider() |
240
|
|
|
{ |
241
|
|
|
return [ |
242
|
|
|
[true, true], |
243
|
|
|
[true, false], |
244
|
|
|
[false, true], |
245
|
|
|
[false, false], |
246
|
|
|
]; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function publishProvider() |
250
|
|
|
{ |
251
|
|
|
return [ |
252
|
|
|
['message', 'routingKey', true, true, ['delivery_mode' => 5]], |
253
|
|
|
['message', null, true, false, ['delivery_mode' => 5]], |
254
|
|
|
['message', 'routingKey', false, true, ['delivery_mode' => 5]], |
255
|
|
|
['message', 'routingKey', false, false, ['delivery_mode' => 5]], |
256
|
|
|
]; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|