Completed
Push — 1.0 ( e7718b...afd35d )
by David
04:20
created

ClientTest::testDeadLetterQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
36
        global $rabbitmq_port;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
37
        global $rabbitmq_user;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
38
        global $rabbitmq_password;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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