Completed
Push — master ( be1f89...dd7c75 )
by Artem
13:35
created

RpcServerTest::testProcessMessageWithSerializer()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
rs 8.5806
cc 4
eloc 29
nc 1
nop 0
1
<?php
2
3
namespace RabbitMqModule;
4
5
use PhpAmqpLib\Channel\AMQPChannel;
6
use PhpAmqpLib\Connection\AbstractConnection;
7
use PhpAmqpLib\Message\AMQPMessage;
8
use Zend\Serializer\Serializer;
9
10
/**
11
 * Class RpcServerTest
12
 * @package RabbitMqModule
13
 */
14
class RpcServerTest extends \PHPUnit_Framework_TestCase
15
{
16
    public function testProcessMessage()
17
    {
18
        $response = 'ciao';
19
20
        $connection = static::getMockBuilder(AbstractConnection::class)
21
            ->disableOriginalConstructor()
22
            ->getMockForAbstractClass();
23
        $channel = static::getMockBuilder(AMQPChannel::class)
24
            ->disableOriginalConstructor()
25
            ->getMock();
26
27
        $message = new AMQPMessage('request', [
28
            'reply_to' => 'foo',
29
            'correlation_id' => 'bar',
30
        ]);
31
32
        $message->delivery_info = [
33
            'channel' => $channel,
34
            'delivery_tag' => 'foo',
35
        ];
36
37
        /* @var AbstractConnection $connection */
38
        $rpcServer = new RpcServer($connection, $channel);
39
        $rpcServer->setCallback(function () use ($response) {
40
            return $response;
41
        });
42
43
        $channel->expects(static::once())->method('basic_publish')
44
            ->with(
45
                static::callback(function ($a) use ($response) {
46
                    return $a instanceof AMQPMessage
47
                        && $a->body === $response
48
                        && $a->get('correlation_id') === 'bar'
49
                        && $a->get('content_type') === 'text/plain';
50
                }),
51
                static::equalTo(''),
52
                static::equalTo('foo')
53
            );
54
55
        $rpcServer->processMessage($message);
56
    }
57
58
    public function testProcessMessageWithSerializer()
59
    {
60
        $response = ['response' => 'ciao'];
61
62
        $connection = static::getMockBuilder(AbstractConnection::class)
63
            ->disableOriginalConstructor()
64
            ->getMockForAbstractClass();
65
        $channel = static::getMockBuilder(AMQPChannel::class)
66
            ->disableOriginalConstructor()
67
            ->getMock();
68
69
        $message = new AMQPMessage('request', [
70
            'reply_to' => 'foo',
71
            'correlation_id' => 'bar',
72
        ]);
73
74
        $message->delivery_info = [
75
            'channel' => $channel,
76
            'delivery_tag' => 'foo',
77
        ];
78
79
        $serializer = Serializer::factory('json');
80
81
        /* @var AbstractConnection $connection */
82
        $rpcServer = new RpcServer($connection, $channel);
83
        $rpcServer->setSerializer($serializer);
84
        $rpcServer->setCallback(function () use ($response) {
85
            return $response;
86
        });
87
88
        $channel->expects(static::once())->method('basic_publish')
89
            ->with(
90
                static::callback(function ($a) use ($response) {
91
                    return $a instanceof AMQPMessage
92
                    && $a->body === '{"response":"ciao"}'
93
                    && $a->get('correlation_id') === 'bar'
94
                    && $a->get('content_type') === 'text/plain';
95
                }),
96
                static::equalTo(''),
97
                static::equalTo('foo')
98
            );
99
100
        $rpcServer->processMessage($message);
101
    }
102
}
103