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

ConnectionTest::testCreateChannelWithResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
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 15
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace AMQPAL\Adapter\AMQP;
4
5
use AMQPAL\Adapter\AMQP\Options\ConnectionOptions;
6
7
class ConnectionTest extends \PHPUnit_Framework_TestCase
8
{
9
10
    public function testConstructorWithOptions()
11
    {
12
        $options = $this->prophesize(ConnectionOptions::class);
13
14
        $options->getHost()->willReturn('hostname');
15
        $options->getPort()->willReturn(3333);
16
        $options->getVhost()->willReturn('vhost');
17
        $options->getUsername()->willReturn('login');
18
        $options->getPassword()->willReturn('password');
19
        $options->getReadTimeout()->willReturn(2);
20
        $options->getWriteTimeout()->willReturn(2);
21
        $options->getConnectTimeout()->willReturn(2);
22
        $options->getChannelMax()->willReturn(2);
23
        $options->getFrameMax()->willReturn(2);
24
        $options->getHeartbeat()->willReturn(2);
25
        $options->isPersistent()->willReturn(false);
26
27
        $connection = new Connection($options->reveal());
28
29
        $resource = $connection->getResource();
30
31
        static::assertInstanceOf(\AMQPConnection::class, $resource);
32
        static::assertEquals($resource->getHost(), 'hostname');
33
        static::assertEquals($resource->getPort(), 3333);
34
        static::assertEquals($resource->getVhost(), 'vhost');
35
        static::assertEquals($resource->getLogin(), 'login');
36
        static::assertEquals($resource->getPassword(), 'password');
37
        static::assertEquals($resource->getReadTimeout(), 2);
38
        static::assertEquals($resource->getWriteTimeout(), 2);
39
    }
40
41
    public function testSetOptionsWithArray()
42
    {
43
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
44
45
        $connection = new Connection($adapterConnection->reveal());
46
47
        $connection->setOptions(['host' => 'foo']);
48
        $options = $connection->getOptions();
49
        static::assertInstanceOf(ConnectionOptions::class, $options);
50
        static::assertEquals('foo', $options->getHost());
51
    }
52
53
    public function testIsConnected()
54
    {
55
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
56
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(true);
57
58
        $connection = new Connection($adapterConnection->reveal());
59
60
        $result = $connection->isConnected();
61
        static::assertTrue($result);
62
    }
63
64
    public function testConnect()
65
    {
66
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
67
        $options = $this->prophesize(ConnectionOptions::class);
68
        $options->isPersistent()->willReturn(false);
69
70
        $adapterConnection->connect()->shouldBeCalled();
71
72
        $connection = new Connection($adapterConnection->reveal());
73
        $connection->setOptions($options->reveal());
74
75
        $result = $connection->connect();
76
        static::assertInstanceOf(Connection::class, $result);
77
    }
78
79
    public function testConnectPersistent()
80
    {
81
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
82
        $options = $this->prophesize(ConnectionOptions::class);
83
        $options->isPersistent()->willReturn(true);
84
85
        $adapterConnection->pconnect()->shouldBeCalled();
86
87
        $connection = new Connection($adapterConnection->reveal());
88
        $connection->setOptions($options->reveal());
89
90
        $result = $connection->connect();
91
        static::assertInstanceOf(Connection::class, $result);
92
    }
93
94
    public function testReconnect()
95
    {
96
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
97
        $options = $this->prophesize(ConnectionOptions::class);
98
        $options->isPersistent()->willReturn(false);
99
100
        $adapterConnection->reconnect()->shouldBeCalled();
101
102
        $connection = new Connection($adapterConnection->reveal());
103
        $connection->setOptions($options->reveal());
104
105
        $result = $connection->reconnect();
106
        static::assertInstanceOf(Connection::class, $result);
107
    }
108
109
    public function testReconnectPersistent()
110
    {
111
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
112
        $options = $this->prophesize(ConnectionOptions::class);
113
        $options->isPersistent()->willReturn(true);
114
115
        $adapterConnection->preconnect()->shouldBeCalled();
116
117
        $connection = new Connection($adapterConnection->reveal());
118
        $connection->setOptions($options->reveal());
119
120
        $result = $connection->reconnect();
121
        static::assertInstanceOf(Connection::class, $result);
122
    }
123
124
    public function testDisconnect()
125
    {
126
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
127
        $options = $this->prophesize(ConnectionOptions::class);
128
        $options->isPersistent()->willReturn(false);
129
130
        $adapterConnection->disconnect()->shouldBeCalled();
131
132
        $connection = new Connection($adapterConnection->reveal());
133
        $connection->setOptions($options->reveal());
134
135
        $result = $connection->disconnect();
136
        static::assertInstanceOf(Connection::class, $result);
137
    }
138
139
    public function testDisconnectPersistent()
140
    {
141
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
142
        $options = $this->prophesize(ConnectionOptions::class);
143
        $options->isPersistent()->willReturn(true);
144
145
        $adapterConnection->pdisconnect()->shouldBeCalled();
146
147
        $connection = new Connection($adapterConnection->reveal());
148
        $connection->setOptions($options->reveal());
149
150
        $result = $connection->disconnect();
151
        static::assertInstanceOf(Connection::class, $result);
152
    }
153
154
    public function testCreateChannelWithResource()
155
    {
156
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
157
        $channelPrototype = $this->prophesize(Channel::class);
158
        $channelResource = $this->prophesize(\AMQPChannel::class);
159
160
        $connection = new Connection($adapterConnection->reveal());
161
        $connection->registerChannel($channelPrototype->reveal());
162
163
        $channelPrototype->setResource($channelResource->reveal())->shouldBeCalled();
164
        $channelPrototype->setConnection($connection)->shouldBeCalled();
165
166
        $result = $connection->createChannel($channelResource->reveal());
167
        static::assertInstanceOf(Channel::class, $result);
168
    }
169
170
    public function testCreateChannel()
171
    {
172
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
173
        $channelPrototype = $this->prophesize(Channel::class);
174
        $channelResource = $this->prophesize(\AMQPChannel::class);
175
176
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(true);
177
        $channelPrototype->setResource($channelResource->reveal())->shouldBeCalled();
178
179
        $connection = static::getMockBuilder(Connection::class)
180
            ->setMethods(['createChannelResource'])
181
            ->setConstructorArgs([$adapterConnection->reveal(), $channelPrototype->reveal()])
182
            ->getMock();
183
184
        $connection->registerChannel($channelPrototype->reveal());
185
        $channelPrototype->setConnection($connection)->shouldBeCalled();
186
187
        $connection->expects(static::once())
188
            ->method('createChannelResource')
189
            ->willReturn($channelResource->reveal());
190
191
        $channel = $connection->createChannel();
192
        static::assertInstanceOf(Channel::class, $channel);
193
    }
194
195
    public function testCreateChannelWithConnect()
196
    {
197
        $adapterConnection = $this->prophesize(\AMQPConnection::class);
198
        $channelPrototype = $this->prophesize(Channel::class);
199
        $channelResource = $this->prophesize(\AMQPChannel::class);
200
201
        $adapterConnection->isConnected()->shouldBeCalled()->willReturn(false);
202
        $channelPrototype->setResource($channelResource->reveal())->shouldBeCalled();
203
204
        $connection = static::getMockBuilder(Connection::class)
205
            ->setMethods(['createChannelResource', 'connect'])
206
            ->setConstructorArgs([$adapterConnection->reveal(), $channelPrototype->reveal()])
207
            ->getMock();
208
209
        $connection->registerChannel($channelPrototype->reveal());
210
        $channelPrototype->setConnection($connection)->shouldBeCalled();
211
212
        $connection->expects(static::once())
213
            ->method('connect')
214
            ->willReturn($connection);
215
216
        $connection->expects(static::once())
217
            ->method('createChannelResource')
218
            ->willReturn($channelResource->reveal());
219
220
        $channel = $connection->createChannel();
221
        static::assertInstanceOf(Channel::class, $channel);
222
    }
223
}
224