|
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
|
|
|
private $queue; |
|
18
|
|
|
private $msgReceived; |
|
19
|
|
|
|
|
20
|
|
|
protected function setUp() |
|
21
|
|
|
{ |
|
22
|
|
|
global $rabbitmq_host; |
|
|
|
|
|
|
23
|
|
|
global $rabbitmq_port; |
|
|
|
|
|
|
24
|
|
|
global $rabbitmq_user; |
|
|
|
|
|
|
25
|
|
|
global $rabbitmq_password; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$this->client = new Client($rabbitmq_host, $rabbitmq_port, $rabbitmq_user, $rabbitmq_password); |
|
28
|
|
|
$this->client->setPrefetchCount(1); |
|
29
|
|
|
|
|
30
|
|
|
$this->exchange = new Exchange($this->client, 'test_exchange', 'fanout'); |
|
31
|
|
|
$this->queue = new Queue($this->client, 'test_queue', [ |
|
32
|
|
|
new Consumer(function(AMQPMessage $msg) { |
|
33
|
|
|
$this->msgReceived = $msg; |
|
34
|
|
|
}) |
|
35
|
|
|
]); |
|
36
|
|
|
$this->queue->setDurable(true); |
|
37
|
|
|
|
|
38
|
|
|
$binding = new Binding($this->exchange, $this->queue); |
|
39
|
|
|
$this->client->register($binding); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
public function testExchange() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->exchange->publish(new Message('my message'), 'key'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @depends testExchange |
|
51
|
|
|
*/ |
|
52
|
|
|
public function testQueue() |
|
53
|
|
|
{ |
|
54
|
|
|
$consumerService = new ConsumerService($this->client, [ |
|
55
|
|
|
$this->queue |
|
56
|
|
|
]); |
|
57
|
|
|
|
|
58
|
|
|
$consumerService->run(true); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals('my message', $this->msgReceived->getBody()); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
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