|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mouf\AmqpClient\Objects; |
|
4
|
|
|
|
|
5
|
|
|
use Mouf\AmqpClient\Client; |
|
6
|
|
|
use Mouf\AmqpClient\Consumer; |
|
7
|
|
|
use Mouf\AmqpClient\ConsumerService; |
|
8
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
9
|
|
|
|
|
10
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Client |
|
14
|
|
|
*/ |
|
15
|
|
|
private $client; |
|
16
|
|
|
private $exchange; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Queue |
|
19
|
|
|
*/ |
|
20
|
|
|
private $queue; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Exchange |
|
23
|
|
|
*/ |
|
24
|
|
|
private $deadLetterExchange; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Queue |
|
27
|
|
|
*/ |
|
28
|
|
|
private $deadLetterQueue; |
|
29
|
|
|
private $msgReceived; |
|
30
|
|
|
private $deadLetterMsgReceived; |
|
31
|
|
|
private $triggerException = false; |
|
32
|
|
|
|
|
33
|
|
|
private function makeClient() |
|
34
|
|
|
{ |
|
35
|
|
|
global $rabbitmq_host; |
|
|
|
|
|
|
36
|
|
|
global $rabbitmq_port; |
|
|
|
|
|
|
37
|
|
|
global $rabbitmq_user; |
|
|
|
|
|
|
38
|
|
|
global $rabbitmq_password; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
$client = new Client($rabbitmq_host, $rabbitmq_port, $rabbitmq_user, $rabbitmq_password); |
|
41
|
|
|
$client->setPrefetchCount(1); |
|
42
|
|
|
return $client; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function init() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->client = $this->makeClient(); |
|
48
|
|
|
$this->exchange = new Exchange($this->client, 'test_exchange', 'fanout'); |
|
49
|
|
|
$this->queue = new Queue($this->client, 'test_queue', [ |
|
50
|
|
|
new Consumer(function(AMQPMessage $msg) { |
|
51
|
|
|
$this->msgReceived = $msg; |
|
52
|
|
|
if ($this->triggerException) { |
|
53
|
|
|
throw new \Exception('boom!'); |
|
54
|
|
|
} |
|
55
|
|
|
}) |
|
56
|
|
|
]); |
|
57
|
|
|
$this->queue->setDurable(true); |
|
58
|
|
|
|
|
59
|
|
|
$binding = new Binding($this->exchange, $this->queue); |
|
60
|
|
|
$this->client->register($binding); |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$this->deadLetterExchange = new Exchange($this->client, 'test_dead_letter_exchange', 'fanout'); |
|
64
|
|
|
|
|
65
|
|
|
$this->deadLetterQueue = new Queue($this->client, 'test_dead_letter_queue', [ |
|
66
|
|
|
new Consumer(function(AMQPMessage $msg) { |
|
67
|
|
|
$this->deadLetterMsgReceived = $msg; |
|
68
|
|
|
}) |
|
69
|
|
|
]); |
|
70
|
|
|
$this->deadLetterQueue->setDurable(true); |
|
71
|
|
|
|
|
72
|
|
|
$this->queue->setDeadLetterExchange($this->deadLetterExchange); |
|
73
|
|
|
|
|
74
|
|
|
$binding = new Binding($this->deadLetterExchange, $this->deadLetterQueue); |
|
75
|
|
|
$this->client->register($binding); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testExchange() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->init(); |
|
81
|
|
|
$this->exchange->publish(new Message('my message'), 'key'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @depends testExchange |
|
86
|
|
|
*/ |
|
87
|
|
|
public function testQueue() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->init(); |
|
90
|
|
|
|
|
91
|
|
|
$consumerService = new ConsumerService($this->client, [ |
|
92
|
|
|
$this->queue |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
$consumerService->run(true); |
|
96
|
|
|
|
|
97
|
|
|
$this->assertEquals('my message', $this->msgReceived->getBody()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @depends testExchange |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testDeadLetterQueue() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->init(); |
|
106
|
|
|
|
|
107
|
|
|
$this->exchange->publish(new Message('my other message'), 'key'); |
|
108
|
|
|
|
|
109
|
|
|
$this->triggerException = true; |
|
110
|
|
|
|
|
111
|
|
|
$consumerService = new ConsumerService($this->client, [ |
|
112
|
|
|
$this->queue, |
|
113
|
|
|
$this->deadLetterQueue |
|
114
|
|
|
]); |
|
115
|
|
|
|
|
116
|
|
|
$consumerService->run(true); |
|
117
|
|
|
$consumerService->run(true); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertEquals('my other message', $this->msgReceived->getBody()); |
|
120
|
|
|
$this->assertEquals('my other message', $this->deadLetterMsgReceived->getBody()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function testExceptionOnExchangeEmptyName() |
|
124
|
|
|
{ |
|
125
|
|
|
$client = $this->makeClient(); |
|
126
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
127
|
|
|
new Exchange($client, '', 'direct'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
View Code Duplication |
public function testDefaultExchange() |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
$client = $this->makeClient(); |
|
133
|
|
|
// Let's send a message on RabbitMQ default exchange (named "") |
|
134
|
|
|
$exchange = new DefaultExchange($client); |
|
135
|
|
|
$queue = new Queue($client, 'test_direct_queue', [ |
|
136
|
|
|
new Consumer(function(AMQPMessage $msg) { |
|
137
|
|
|
$this->msgReceived = $msg; |
|
138
|
|
|
}) |
|
139
|
|
|
]); |
|
140
|
|
|
|
|
141
|
|
|
// The key is the name of the queue. |
|
142
|
|
|
$exchange->publish(new Message('hello'), 'test_direct_queue'); |
|
143
|
|
|
|
|
144
|
|
|
$consumerService = new ConsumerService($client, [ |
|
145
|
|
|
$queue |
|
146
|
|
|
]); |
|
147
|
|
|
|
|
148
|
|
|
$consumerService->run(true); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertEquals('hello', $this->msgReceived->getBody()); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
public function testPublishToQueue() |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
$client = $this->makeClient(); |
|
156
|
|
|
$queue = new Queue($client, 'test_direct_queue', [ |
|
157
|
|
|
new Consumer(function(AMQPMessage $msg) { |
|
158
|
|
|
$this->msgReceived = $msg; |
|
159
|
|
|
}) |
|
160
|
|
|
]); |
|
161
|
|
|
|
|
162
|
|
|
// The key is the name of the queue. |
|
163
|
|
|
$queue->publish(new Message('hello')); |
|
164
|
|
|
|
|
165
|
|
|
$consumerService = new ConsumerService($client, [ |
|
166
|
|
|
$queue |
|
167
|
|
|
]); |
|
168
|
|
|
|
|
169
|
|
|
$consumerService->run(true); |
|
170
|
|
|
|
|
171
|
|
|
$this->assertEquals('hello', $this->msgReceived->getBody()); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state