Completed
Push — master ( ccbe64...bcccab )
by Thomas Mauro
05:56
created

ConnectionTest::testCreateChannel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
namespace AMQPAL\Adapter\PhpAmqpLib;
4
5
use AMQPAL\Adapter\PhpAmqpLib\Options\ConnectionOptions;
6
use PhpAmqpLib\Connection\AbstractConnection;
7
use PhpAmqpLib\Channel\AMQPChannel;
8
use AMQPAL\Options;
9
use Prophecy\Argument;
10
11
class ConnectionTest extends \PHPUnit_Framework_TestCase
12
{
13
14
    public function testConstructorWithOptions()
15
    {
16
        $adapterConnection = $this->prophesize(AbstractConnection::class);
17
        $options = $this->prophesize(ConnectionOptions::class);
18
        $connectionFactoryFactory = $this->prophesize(Factory\ConnectionFactoryFactory::class);
19
        $connectionFactory = $this->prophesize(Factory\ConnectionFactoryInterface::class);
20
21
        $connectionFactory->createConnection($options->reveal())->willReturn($adapterConnection->reveal());
22
        $connectionFactoryFactory->createFactory('foo')->willReturn($connectionFactory->reveal());
23
        $options->getType()->shouldBeCalled()->willReturn('foo');
24
        $options->getConnectionFactoryFactory()->willReturn($connectionFactoryFactory->reveal());
25
26
        $connection = new Connection($options->reveal());
27
28
        $resource = $connection->getResource();
29
30
        static::assertInstanceOf(AbstractConnection::class, $resource);
31
        static::assertSame($adapterConnection->reveal(), $resource);
32
    }
33
34
    public function testSetOptions()
35
    {
36
        $adapterConnection = $this->prophesize(AbstractConnection::class);
37
        $options = $this->prophesize(ConnectionOptions::class);
38
39
        $connection = new Connection($adapterConnection->reveal());
40
41
        static::assertSame($connection, $connection->setOptions($options->reveal()));
42
    }
43
44
    public function testSetOptionsArray()
45
    {
46
        $adapterConnection = $this->prophesize(AbstractConnection::class);
47
48
        $options = [
49
            'type' => 'foo'
50
        ];
51
52
        $connection = new Connection($adapterConnection->reveal());
53
54
        static::assertSame($connection, $connection->setOptions($options));
55
        $optionsResult = $connection->getOptions();
56
        static::assertInstanceOf(ConnectionOptions::class, $optionsResult);
57
        static::assertEquals('foo', $optionsResult->getType());
58
    }
59
60
    public function testIsConnected()
61
    {
62
        $adapterConnection = $this->prophesize(AbstractConnection::class);
63
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(true);
64
65
        $connection = new Connection($adapterConnection->reveal());
66
67
        $result = $connection->isConnected();
68
        static::assertTrue($result);
69
    }
70
71
    public function testConnect()
72
    {
73
        $adapterConnection = $this->prophesize(AbstractConnection::class);
74
75
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(false);
76
        $adapterConnection->reconnect()->shouldBeCalled();
77
78
        $connection = new Connection($adapterConnection->reveal());
79
80
        $result = $connection->connect();
81
        static::assertInstanceOf(Connection::class, $result);
82
    }
83
84
    public function testReconnect()
85
    {
86
        $adapterConnection = $this->prophesize(AbstractConnection::class);
87
88
        $adapterConnection->reconnect()->shouldBeCalled();
89
90
        $connection = new Connection($adapterConnection->reveal());
91
92
        $result = $connection->reconnect();
93
        static::assertInstanceOf(Connection::class, $result);
94
    }
95
96
    public function testDisconnect()
97
    {
98
        $adapterConnection = $this->prophesize(AbstractConnection::class);
99
100
        $adapterConnection->close()->shouldBeCalled();
101
102
        $connection = new Connection($adapterConnection->reveal());
103
104
        $result = $connection->disconnect();
105
        static::assertInstanceOf(Connection::class, $result);
106
    }
107
108
    public function testCreateChannelWithResource()
109
    {
110
        $adapterConnection = $this->prophesize(AbstractConnection::class);
111
        $channelPrototype = $this->prophesize(Channel::class);
112
        $channelResource = $this->prophesize(AMQPChannel::class);
113
114
        $connection = new Connection($adapterConnection->reveal());
115
        $connection->registerChannel($channelPrototype->reveal());
116
117
        $channelPrototype->setResource($channelResource->reveal())->shouldBeCalled();
118
        $channelPrototype->setConnection($connection)->shouldBeCalled();
119
120
        $result = $connection->createChannel($channelResource->reveal());
121
        static::assertInstanceOf(Channel::class, $result);
122
    }
123
124
    public function testCreateChannel()
125
    {
126
        $adapterConnection = $this->prophesize(AbstractConnection::class);
127
        $channelPrototype = $this->prophesize(Channel::class);
128
        $channelResource = $this->prophesize(AMQPChannel::class);
129
130
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(true);
131
        $adapterConnection->channel()->shouldBeCalled()->willReturn($channelResource->reveal());
132
        $channelPrototype->setResource($channelResource->reveal())->shouldBeCalled();
133
134
        $connection = new Connection($adapterConnection->reveal());
135
        $connection->registerChannel($channelPrototype->reveal());
136
        $channelPrototype->setConnection($connection)->shouldBeCalled();
137
138
        $channel = $connection->createChannel();
139
        static::assertInstanceOf(Channel::class, $channel);
140
    }
141
142
    public function testCreateChannelWithConnect()
143
    {
144
        $adapterConnection = $this->prophesize(AbstractConnection::class);
145
        $channelPrototype = $this->prophesize(Channel::class);
146
        $channelResource = $this->prophesize(AMQPChannel::class);
147
148
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(false);
149
        $adapterConnection->reconnect()->shouldBeCalled();
150
        $adapterConnection->channel()->shouldBeCalled()->willReturn($channelResource->reveal());
151
        $channelPrototype->setResource($channelResource->reveal())->shouldBeCalled();
152
153
        $connection = new Connection($adapterConnection->reveal());
154
        $connection->registerChannel($channelPrototype->reveal());
155
        $channelPrototype->setConnection($connection)->shouldBeCalled();
156
        $channelPrototype->setConnection($connection)->shouldBeCalled();
157
158
        $channel = $connection->createChannel();
159
        static::assertInstanceOf(Channel::class, $channel);
160
    }
161
}
162