Completed
Pull Request — 1.0 (#5)
by Benoit
02:05
created

ClientTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 7
c 5
b 0
f 0
lcom 1
cbo 9
dl 0
loc 121
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testExchange() 0 5 1
A testQueue() 0 12 1
B init() 0 44 3
A testDeadLetterQueue() 0 19 1
A testConnectionException() 0 5 1
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($port = null)
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
        if (!$port) {
41
          $port = $rabbitmq_port;
42
        }
43
44
45
        $this->client = new Client($rabbitmq_host, $port, $rabbitmq_user, $rabbitmq_password);
46
        $this->client->setPrefetchCount(1);
47
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
     * @expectedException \Mouf\AmqpClient\Exception\ConnectionException
124
     */
125
    public function testConnectionException()
126
    {
127
        $this->init(1242000042);
128
        $this->client->getChannel();
129
    }
130
}
131