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