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 testDeclareExchange() |
41
|
|
|
{ |
42
|
|
|
$options = $this->getDefaultOptionsProphet(); |
43
|
|
|
$resource = $this->prophesize(\AMQPExchange::class); |
44
|
|
|
|
45
|
|
|
$resource->setFlags(AMQP_NOPARAM | AMQP_DURABLE | AMQP_PASSIVE | AMQP_AUTODELETE | AMQP_INTERNAL | AMQP_NOWAIT) |
46
|
|
|
->shouldBeCalled(); |
47
|
|
|
$resource->setType('exchangeType')->shouldBeCalled(); |
48
|
|
|
$resource->setName('exchangeName')->shouldBeCalled(); |
49
|
|
|
$resource->setArguments(['arg1' => 'value1'])->shouldBeCalled(); |
50
|
|
|
|
51
|
|
|
$resource->declareExchange()->shouldBeCalled(); |
52
|
|
|
|
53
|
|
|
$exchange = new Exchange(); |
54
|
|
|
$exchange->setResource($resource->reveal()); |
55
|
|
|
$exchange->setOptions($options->reveal()); |
56
|
|
|
|
57
|
|
|
static::assertSame($exchange, $exchange->declareExchange()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return \Prophecy\Prophecy\ObjectProphecy |
62
|
|
|
*/ |
63
|
|
|
protected function getDefaultOptionsProphet() |
64
|
|
|
{ |
65
|
|
|
$options = $this->prophesize(Options\ExchangeOptions::class); |
66
|
|
|
|
67
|
|
|
$options->isDurable()->willReturn(true); |
68
|
|
|
$options->isPassive()->willReturn(true); |
69
|
|
|
$options->isAutoDelete()->willReturn(true); |
70
|
|
|
$options->isInternal()->willReturn(true); |
71
|
|
|
$options->isNoWait()->willReturn(true); |
72
|
|
|
$options->getType()->willReturn('exchangeType'); |
73
|
|
|
$options->getName()->willReturn('exchangeName'); |
74
|
|
|
$options->getArguments()->willReturn(['arg1' => 'value1']); |
75
|
|
|
|
76
|
|
|
return $options; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @dataProvider deleteProvider() |
81
|
|
|
*/ |
82
|
|
|
public function testDelete($ifUnused, $noWait, $flags) |
83
|
|
|
{ |
84
|
|
|
$options = $this->getDefaultOptionsProphet(); |
85
|
|
|
$resource = $this->prophesize(\AMQPExchange::class); |
86
|
|
|
|
87
|
|
|
$resource->setFlags(AMQP_NOPARAM | AMQP_DURABLE | AMQP_PASSIVE | AMQP_AUTODELETE | AMQP_INTERNAL | AMQP_NOWAIT) |
88
|
|
|
->shouldBeCalled(); |
89
|
|
|
$resource->setType('exchangeType')->shouldBeCalled(); |
90
|
|
|
$resource->setName('exchangeName')->shouldBeCalled(); |
91
|
|
|
$resource->setArguments(['arg1' => 'value1'])->shouldBeCalled(); |
92
|
|
|
|
93
|
|
|
$resource->delete('exchangeName', $flags)->shouldBeCalled(); |
94
|
|
|
|
95
|
|
|
$exchange = new Exchange(); |
96
|
|
|
$exchange->setResource($resource->reveal()); |
97
|
|
|
$exchange->setOptions($options->reveal()); |
98
|
|
|
|
99
|
|
|
static::assertSame($exchange, $exchange->delete($ifUnused, $noWait)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testBind() |
103
|
|
|
{ |
104
|
|
|
$arguments = ['arg2' => 'value2']; |
105
|
|
|
|
106
|
|
|
$options = $this->getDefaultOptionsProphet(); |
107
|
|
|
$resource = $this->prophesize(\AMQPExchange::class); |
108
|
|
|
|
109
|
|
|
$resource->setFlags(AMQP_NOPARAM | AMQP_DURABLE | AMQP_PASSIVE | AMQP_AUTODELETE | AMQP_INTERNAL | AMQP_NOWAIT) |
110
|
|
|
->shouldBeCalled(); |
111
|
|
|
$resource->setType('exchangeType')->shouldBeCalled(); |
112
|
|
|
$resource->setName('exchangeName')->shouldBeCalled(); |
113
|
|
|
$resource->setArguments(['arg1' => 'value1'])->shouldBeCalled(); |
114
|
|
|
|
115
|
|
|
$resource->bind('exchange2bindName', 'routingKey', $arguments)->shouldBeCalled(); |
116
|
|
|
|
117
|
|
|
$exchange = new Exchange(); |
118
|
|
|
$exchange->setResource($resource->reveal()); |
119
|
|
|
$exchange->setOptions($options->reveal()); |
120
|
|
|
|
121
|
|
|
static::assertSame($exchange, $exchange->bind('exchange2bindName', 'routingKey', false, $arguments)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testUnbind() |
125
|
|
|
{ |
126
|
|
|
$arguments = ['arg2' => 'value2']; |
127
|
|
|
|
128
|
|
|
$options = $this->getDefaultOptionsProphet(); |
129
|
|
|
$resource = $this->prophesize(\AMQPExchange::class); |
130
|
|
|
|
131
|
|
|
$resource->setFlags(AMQP_NOPARAM | AMQP_DURABLE | AMQP_PASSIVE | AMQP_AUTODELETE | AMQP_INTERNAL | AMQP_NOWAIT) |
132
|
|
|
->shouldBeCalled(); |
133
|
|
|
$resource->setType('exchangeType')->shouldBeCalled(); |
134
|
|
|
$resource->setName('exchangeName')->shouldBeCalled(); |
135
|
|
|
$resource->setArguments(['arg1' => 'value1'])->shouldBeCalled(); |
136
|
|
|
|
137
|
|
|
$resource->unbind('exchange2bindName', 'routingKey', $arguments)->shouldBeCalled(); |
138
|
|
|
|
139
|
|
|
$exchange = new Exchange(); |
140
|
|
|
$exchange->setResource($resource->reveal()); |
141
|
|
|
$exchange->setOptions($options->reveal()); |
142
|
|
|
|
143
|
|
|
static::assertSame($exchange, $exchange->unbind('exchange2bindName', 'routingKey', $arguments)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @dataProvider publishProvider() |
148
|
|
|
*/ |
149
|
|
|
public function testPublish($message, $routingKey, $mandatory, $immediate, $attributes, $flags) |
150
|
|
|
{ |
151
|
|
|
$options = $this->getDefaultOptionsProphet(); |
152
|
|
|
$resource = $this->prophesize(\AMQPExchange::class); |
153
|
|
|
|
154
|
|
|
$resource->setFlags(AMQP_NOPARAM | AMQP_DURABLE | AMQP_PASSIVE | AMQP_AUTODELETE | AMQP_INTERNAL | AMQP_NOWAIT) |
155
|
|
|
->shouldBeCalled(); |
156
|
|
|
$resource->setType('exchangeType')->shouldBeCalled(); |
157
|
|
|
$resource->setName('exchangeName')->shouldBeCalled(); |
158
|
|
|
$resource->setArguments(['arg1' => 'value1'])->shouldBeCalled(); |
159
|
|
|
|
160
|
|
|
$resource->publish($message, $routingKey, $flags, $attributes)->shouldBeCalled(); |
161
|
|
|
|
162
|
|
|
$exchange = new Exchange(); |
163
|
|
|
$exchange->setResource($resource->reveal()); |
164
|
|
|
$exchange->setOptions($options->reveal()); |
165
|
|
|
|
166
|
|
|
static::assertSame($exchange, $exchange->publish($message, $routingKey, $mandatory, $immediate, $attributes)); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testSetAndGetChannel() |
170
|
|
|
{ |
171
|
|
|
$channel = $this->prophesize(Channel::class); |
172
|
|
|
|
173
|
|
|
$exchange = new Exchange(); |
174
|
|
|
static::assertSame($exchange, $exchange->setChannel($channel->reveal())); |
175
|
|
|
static::assertSame($channel->reveal(), $exchange->getChannel()); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testGetConnection() |
179
|
|
|
{ |
180
|
|
|
$channel = $this->prophesize(Channel::class); |
181
|
|
|
$connection = $this->prophesize(Connection::class); |
182
|
|
|
|
183
|
|
|
$channel->getConnection()->willReturn($connection); |
184
|
|
|
|
185
|
|
|
$exchange = new Exchange(); |
186
|
|
|
static::assertSame($exchange, $exchange->setChannel($channel->reveal())); |
187
|
|
|
static::assertSame($connection->reveal(), $exchange->getConnection()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function deleteProvider() |
191
|
|
|
{ |
192
|
|
|
return [ |
193
|
|
|
[true, true, AMQP_IFUNUSED | AMQP_NOWAIT], |
194
|
|
|
[true, false, AMQP_IFUNUSED], |
195
|
|
|
[false, true, AMQP_NOWAIT], |
196
|
|
|
[false, false, AMQP_NOPARAM], |
197
|
|
|
]; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function publishProvider() |
201
|
|
|
{ |
202
|
|
|
return [ |
203
|
|
|
['message', 'routingKey', true, true, ['attr1' => 'value1'], AMQP_MANDATORY | AMQP_IMMEDIATE], |
204
|
|
|
['message', 'routingKey', true, false, ['attr1' => 'value1'], AMQP_MANDATORY], |
205
|
|
|
['message', 'routingKey', false, true, ['attr1' => 'value1'], AMQP_IMMEDIATE], |
206
|
|
|
['message', 'routingKey', false, false, ['attr1' => 'value1'], AMQP_NOPARAM], |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|