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
|
|
|
public 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
|
|
|
); |
76
|
|
|
$channelMock->expects($this->once()) |
77
|
|
|
->method('queue_declare') |
78
|
|
|
->with($this->equalTo(self::QUEUE), |
79
|
|
|
$this->isType('boolean'), |
80
|
|
|
$this->isType('boolean'), |
81
|
|
|
$this->isType('boolean'), |
82
|
|
|
$this->isType('boolean'), |
83
|
|
|
$this->isType('boolean'), |
84
|
|
|
$this->equalTo(array( |
85
|
|
|
'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE), |
86
|
|
|
)) |
87
|
|
|
); |
88
|
|
|
$channelMock->expects($this->exactly(2)) |
89
|
|
|
->method('queue_bind') |
90
|
|
|
->withConsecutive( |
91
|
|
|
array( |
92
|
|
|
$this->equalTo(self::QUEUE), |
93
|
|
|
$this->equalTo(self::EXCHANGE), |
94
|
|
|
$this->equalTo(self::KEY), |
95
|
|
|
), |
96
|
|
|
array( |
97
|
|
|
$this->equalTo(self::QUEUE), |
98
|
|
|
$this->equalTo(self::DEAD_LETTER_EXCHANGE), |
99
|
|
|
$this->equalTo(self::KEY), |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$backend->initialize(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey() |
107
|
|
|
{ |
108
|
|
|
list($backend, $channelMock) = $this->getBackendAndChannelMock(false, self::DEAD_LETTER_EXCHANGE, self::DEAD_LETTER_ROUTING_KEY); |
109
|
|
|
|
110
|
|
|
$channelMock->expects($this->once()) |
111
|
|
|
->method('exchange_declare') |
112
|
|
|
->with($this->equalTo(self::EXCHANGE), |
113
|
|
|
$this->equalTo('direct'), |
114
|
|
|
$this->isType('boolean'), |
115
|
|
|
$this->isType('boolean'), |
116
|
|
|
$this->isType('boolean') |
117
|
|
|
); |
118
|
|
|
$channelMock->expects($this->once()) |
119
|
|
|
->method('queue_declare') |
120
|
|
|
->with($this->equalTo(self::QUEUE), |
121
|
|
|
$this->isType('boolean'), |
122
|
|
|
$this->isType('boolean'), |
123
|
|
|
$this->isType('boolean'), |
124
|
|
|
$this->isType('boolean'), |
125
|
|
|
$this->isType('boolean'), |
126
|
|
|
$this->equalTo(array( |
127
|
|
|
'x-dead-letter-exchange' => array('S', self::DEAD_LETTER_EXCHANGE), |
128
|
|
|
'x-dead-letter-routing-key' => array('S', self::DEAD_LETTER_ROUTING_KEY), |
129
|
|
|
)) |
130
|
|
|
); |
131
|
|
|
$channelMock->expects($this->once()) |
132
|
|
|
->method('queue_bind') |
133
|
|
|
->with($this->equalTo(self::QUEUE), |
134
|
|
|
$this->equalTo(self::EXCHANGE), |
135
|
|
|
$this->equalTo(self::KEY) |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$backend->initialize(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function getBackendAndChannelMock($recover = false, $deadLetterExchange = null, $deadLetterRoutingKey = null) |
142
|
|
|
{ |
143
|
|
|
$mock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackend') |
144
|
|
|
->setConstructorArgs(array(self::EXCHANGE, self::QUEUE, $recover, self::KEY, $deadLetterExchange, $deadLetterRoutingKey)) |
145
|
|
|
->setMethods(array('getIterator')) |
146
|
|
|
->getMock(); |
147
|
|
|
|
148
|
|
|
$settings = array( |
149
|
|
|
'host' => 'foo', |
150
|
|
|
'port' => 'port', |
151
|
|
|
'user' => 'user', |
152
|
|
|
'pass' => 'pass', |
153
|
|
|
'vhost' => '/', |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$queues = array( |
157
|
|
|
array('queue' => self::QUEUE, 'routing_key' => self::KEY), |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
$channelMock = $this->getMockBuilder('\PhpAmqpLib\Channel\AMQPChannel') |
161
|
|
|
->disableOriginalConstructor() |
162
|
|
|
->setMethods(array('queue_declare', 'exchange_declare', 'queue_bind')) |
163
|
|
|
->getMock() |
164
|
|
|
; |
165
|
|
|
|
166
|
|
|
$dispatcherMock = $this->getMockBuilder('\Sonata\NotificationBundle\Backend\AMQPBackendDispatcher') |
167
|
|
|
->setConstructorArgs(array($settings, $queues, 'default', array(array('type' => self::KEY, 'backend' => $mock)))) |
168
|
|
|
->setMethods(array('getChannel')) |
169
|
|
|
->getMock(); |
170
|
|
|
|
171
|
|
|
$dispatcherMock->method('getChannel') |
172
|
|
|
->willReturn($channelMock); |
173
|
|
|
|
174
|
|
|
$mock->setDispatcher($dispatcherMock); |
175
|
|
|
|
176
|
|
|
return array($mock, $channelMock); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|