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
|
|
|
protected function init() |
34
|
|
|
{ |
35
|
|
|
global $rabbitmq_host; |
|
|
|
|
36
|
|
|
global $rabbitmq_port; |
|
|
|
|
37
|
|
|
global $rabbitmq_user; |
|
|
|
|
38
|
|
|
global $rabbitmq_password; |
|
|
|
|
39
|
|
|
|
40
|
|
|
$this->client = new Client($rabbitmq_host, $rabbitmq_port, $rabbitmq_user, $rabbitmq_password); |
41
|
|
|
$this->client->setPrefetchCount(1); |
42
|
|
|
|
43
|
|
|
$this->exchange = new Exchange($this->client, 'test_exchange', 'fanout'); |
44
|
|
|
$this->queue = new Queue($this->client, 'test_queue', [ |
45
|
|
|
new Consumer(function(AMQPMessage $msg) { |
46
|
|
|
$this->msgReceived = $msg; |
47
|
|
|
if ($this->triggerException) { |
48
|
|
|
throw new \Exception('boom!'); |
49
|
|
|
} |
50
|
|
|
}) |
51
|
|
|
]); |
52
|
|
|
$this->queue->setDurable(true); |
53
|
|
|
|
54
|
|
|
$binding = new Binding($this->exchange, $this->queue); |
55
|
|
|
$this->client->register($binding); |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
$this->deadLetterExchange = new Exchange($this->client, 'test_dead_letter_exchange', 'fanout'); |
59
|
|
|
|
60
|
|
|
$this->deadLetterQueue = new Queue($this->client, 'test_dead_letter_queue', [ |
61
|
|
|
new Consumer(function(AMQPMessage $msg) { |
62
|
|
|
$this->deadLetterMsgReceived = $msg; |
63
|
|
|
}) |
64
|
|
|
]); |
65
|
|
|
$this->deadLetterQueue->setDurable(true); |
66
|
|
|
|
67
|
|
|
$this->queue->setDeadLetterExchange($this->deadLetterExchange); |
68
|
|
|
|
69
|
|
|
$binding = new Binding($this->deadLetterExchange, $this->deadLetterQueue); |
70
|
|
|
$this->client->register($binding); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testExchange() |
74
|
|
|
{ |
75
|
|
|
$this->init(); |
76
|
|
|
$this->exchange->publish(new Message('my message'), 'key'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @depends testExchange |
81
|
|
|
*/ |
82
|
|
|
public function testQueue() |
83
|
|
|
{ |
84
|
|
|
$this->init(); |
85
|
|
|
|
86
|
|
|
$consumerService = new ConsumerService($this->client, [ |
87
|
|
|
$this->queue |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$consumerService->run(true); |
91
|
|
|
|
92
|
|
|
$this->assertEquals('my message', $this->msgReceived->getBody()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @depends testExchange |
98
|
|
|
*/ |
99
|
|
|
public function testDeadLetterQueue() |
100
|
|
|
{ |
101
|
|
|
$this->init(); |
102
|
|
|
|
103
|
|
|
$this->exchange->publish(new Message('my other message'), 'key'); |
104
|
|
|
|
105
|
|
|
$this->triggerException = true; |
106
|
|
|
|
107
|
|
|
$consumerService = new ConsumerService($this->client, [ |
108
|
|
|
$this->queue |
109
|
|
|
]); |
110
|
|
|
|
111
|
|
|
$consumerService->run(true); |
112
|
|
|
$consumerService->run(true); |
113
|
|
|
|
114
|
|
|
$this->assertEquals('my other message', $this->msgReceived->getBody()); |
115
|
|
|
$this->assertEquals('my other message', $this->deadLetterMsgReceived->getBody()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state