|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\Tests\Backend; |
|
13
|
|
|
|
|
14
|
|
|
class AMQPBackendTest extends \PHPUnit_Framework_TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
const EXCHANGE = 'exchange'; |
|
17
|
|
|
const QUEUE = 'foo'; |
|
18
|
|
|
const KEY = 'message.type.foo'; |
|
19
|
|
|
const DEAD_LETTER_EXCHANGE = 'dlx'; |
|
20
|
|
|
const DEAD_LETTER_ROUTING_KEY = 'message.type.dl'; |
|
21
|
|
|
|
|
22
|
|
|
protected function setUp() |
|
23
|
|
|
{ |
|
24
|
|
|
if (!class_exists('PhpAmqpLib\Channel\AMQPChannel')) { |
|
25
|
|
|
$this->markTestSkipped('AMQP Lib not installed'); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey() |
|
30
|
|
|
{ |
|
31
|
|
|
list($backend, $channelMock) = $this->getBackendAndChannelMock(); |
|
32
|
|
|
|
|
33
|
|
|
$channelMock->expects($this->once()) |
|
34
|
|
|
->method('exchange_declare') |
|
35
|
|
|
->with($this->equalTo(self::EXCHANGE), |
|
36
|
|
|
$this->equalTo('direct'), |
|
37
|
|
|
$this->isType('boolean'), |
|
38
|
|
|
$this->isType('boolean'), |
|
39
|
|
|
$this->isType('boolean') |
|
40
|
|
|
); |
|
41
|
|
|
$channelMock->expects($this->once()) |
|
42
|
|
|
->method('queue_declare') |
|
43
|
|
|
->with($this->equalTo(self::QUEUE), |
|
44
|
|
|
$this->isType('boolean'), |
|
45
|
|
|
$this->isType('boolean'), |
|
46
|
|
|
$this->isType('boolean'), |
|
47
|
|
|
$this->isType('boolean'), |
|
48
|
|
|
$this->isType('boolean'), |
|
49
|
|
|
$this->equalTo(array()) |
|
50
|
|
|
); |
|
51
|
|
|
$channelMock->expects($this->once()) |
|
52
|
|
|
->method('queue_bind') |
|
53
|
|
|
->with($this->equalTo(self::QUEUE), |
|
54
|
|
|
$this->equalTo(self::EXCHANGE), |
|
55
|
|
|
$this->equalTo(self::KEY) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$backend->initialize(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testInitializeWithDeadLetterExchangeAndNoDeadLetterRoutingKey() |
|
62
|
|
|
{ |
|
63
|
|
|
list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE); |
|
64
|
|
|
|
|
65
|
|
|
$channelMock->expects($this->exactly(2)) |
|
66
|
|
|
->method('exchange_declare') |
|
67
|
|
|
->withConsecutive( |
|
68
|
|
|
array( |
|
69
|
|
|
$this->equalTo(self::EXCHANGE), |
|
70
|
|
|
$this->equalTo('direct'), |
|
71
|
|
|
$this->isType('boolean'), |
|
72
|
|
|
$this->isType('boolean'), |
|
73
|
|
|
$this->isType('boolean'), |
|
74
|
|
|
), |
|
75
|
|
|
array( |
|
76
|
|
|
$this->equalTo(self::DEAD_LETTER_EXCHANGE), |
|
77
|
|
|
$this->equalTo('direct'), |
|
78
|
|
|
$this->isType('boolean'), |
|
79
|
|
|
$this->isType('boolean'), |
|
80
|
|
|
$this->isType('boolean'), |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
$channelMock->expects($this->once()) |
|
84
|
|
|
->method('queue_declare') |
|
85
|
|
|
->with($this->equalTo(self::QUEUE), |
|
86
|
|
|
$this->isType('boolean'), |
|
87
|
|
|
$this->isType('boolean'), |
|
88
|
|
|
$this->isType('boolean'), |
|
89
|
|
|
$this->isType('boolean'), |
|
90
|
|
|
$this->isType('boolean'), |
|
91
|
|
|
$this->equalTo(array( |
|
92
|
|
|
'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE), |
|
93
|
|
|
)) |
|
94
|
|
|
); |
|
95
|
|
|
$channelMock->expects($this->exactly(2)) |
|
96
|
|
|
->method('queue_bind') |
|
97
|
|
|
->withConsecutive( |
|
98
|
|
|
array( |
|
99
|
|
|
$this->equalTo(self::QUEUE), |
|
100
|
|
|
$this->equalTo(self::EXCHANGE), |
|
101
|
|
|
$this->equalTo(self::KEY), |
|
102
|
|
|
), |
|
103
|
|
|
array( |
|
104
|
|
|
$this->equalTo(self::QUEUE), |
|
105
|
|
|
$this->equalTo(self::DEAD_LETTER_EXCHANGE), |
|
106
|
|
|
$this->equalTo(self::KEY), |
|
107
|
|
|
) |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
$backend->initialize(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey() |
|
114
|
|
|
{ |
|
115
|
|
|
list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE, self::DEAD_LETTER_ROUTING_KEY); |
|
116
|
|
|
|
|
117
|
|
|
$channelMock->expects($this->once()) |
|
118
|
|
|
->method('exchange_declare') |
|
119
|
|
|
->with($this->equalTo(self::EXCHANGE), |
|
120
|
|
|
$this->equalTo('direct'), |
|
121
|
|
|
$this->isType('boolean'), |
|
122
|
|
|
$this->isType('boolean'), |
|
123
|
|
|
$this->isType('boolean') |
|
124
|
|
|
); |
|
125
|
|
|
$channelMock->expects($this->once()) |
|
126
|
|
|
->method('queue_declare') |
|
127
|
|
|
->with($this->equalTo(self::QUEUE), |
|
128
|
|
|
$this->isType('boolean'), |
|
129
|
|
|
$this->isType('boolean'), |
|
130
|
|
|
$this->isType('boolean'), |
|
131
|
|
|
$this->isType('boolean'), |
|
132
|
|
|
$this->isType('boolean'), |
|
133
|
|
|
$this->equalTo(array( |
|
134
|
|
|
'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE), |
|
135
|
|
|
'x-dead-letter-routing-key' => array('S', self::DEAD_LETTER_ROUTING_KEY), |
|
136
|
|
|
)) |
|
137
|
|
|
); |
|
138
|
|
|
$channelMock->expects($this->once()) |
|
139
|
|
|
->method('queue_bind') |
|
140
|
|
|
->with($this->equalTo(self::QUEUE), |
|
141
|
|
|
$this->equalTo(self::EXCHANGE), |
|
142
|
|
|
$this->equalTo(self::KEY) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
$backend->initialize(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function getBackendAndChannelMock($recover = false, $deadLetterExchange = null, $deadLetterRoutingKey = null) |
|
149
|
|
|
{ |
|
150
|
|
|
$mock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackend') |
|
151
|
|
|
->setConstructorArgs(array(self::EXCHANGE, self::QUEUE, $recover, self::KEY, $deadLetterExchange, $deadLetterRoutingKey)) |
|
152
|
|
|
->setMethods(array('getIterator')) |
|
153
|
|
|
->getMock(); |
|
154
|
|
|
|
|
155
|
|
|
$settings = array( |
|
156
|
|
|
'host' => 'foo', |
|
157
|
|
|
'port' => 'port', |
|
158
|
|
|
'user' => 'user', |
|
159
|
|
|
'pass' => 'pass', |
|
160
|
|
|
'vhost' => '/', |
|
161
|
|
|
); |
|
162
|
|
|
|
|
163
|
|
|
$queues = array( |
|
164
|
|
|
array('queue' => self::QUEUE, 'routing_key' => self::KEY), |
|
165
|
|
|
); |
|
166
|
|
|
|
|
167
|
|
|
$channelMock = $this->getMockBuilder('\PhpAmqpLib\Channel\AMQPChannel') |
|
168
|
|
|
->disableOriginalConstructor() |
|
169
|
|
|
->setMethods(array('queue_declare', 'exchange_declare', 'queue_bind')) |
|
170
|
|
|
->getMock() |
|
171
|
|
|
; |
|
172
|
|
|
|
|
173
|
|
|
$dispatcherMock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackendDispatcher') |
|
174
|
|
|
->setConstructorArgs(array($settings, $queues, 'default', array(array('type' => self::KEY, 'backend' => $mock)))) |
|
175
|
|
|
->setMethods(array('getChannel')) |
|
176
|
|
|
->getMock(); |
|
177
|
|
|
|
|
178
|
|
|
$dispatcherMock->method('getChannel') |
|
179
|
|
|
->willReturn($channelMock); |
|
180
|
|
|
|
|
181
|
|
|
$mock->setDispatcher($dispatcherMock); |
|
182
|
|
|
|
|
183
|
|
|
return array($mock, $channelMock); |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|