Test Setup Failed
Pull Request — master (#14)
by Timon
05:00
created

ConnectionTest::testRunSequence()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TBolier\RethinkQL\UnitTest\Connection;
5
6
use Exception;
7
use TBolier\RethinkQL\Connection\ConnectionException;
8
use TBolier\RethinkQL\Connection\ConnectionInterface;
9
use TBolier\RethinkQL\Query\MessageInterface;
10
use TBolier\RethinkQL\Response\Cursor;
11
use TBolier\RethinkQL\Response\ResponseInterface;
12
use TBolier\RethinkQL\Types\Query\QueryType;
13
use TBolier\RethinkQL\Types\Response\ResponseType;
14
15
class ConnectionTest extends ConnectionTestCase
16
{
17
    /**
18
     * @return void
19
     */
20
    public function setUp(): void
21
    {
22
        parent::setUp();
23
    }
24
25
    /**
26
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
27
     * @expectedExceptionMessage Test exception
28
     * @return void
29
     * @throws ConnectionException
30
     */
31
    public function testConnectThrowsCorrectException(): void
32
    {
33
        $this->handshake->shouldReceive('hello')->once()->andThrow(
34
            new ConnectionException('Test exception')
35
        );
36
37
        $this->connection->connect();
38
    }
39
40
    public function testExpr()
41
    {
42
        $this->connect();
43
44
        $response = \Mockery::mock(ResponseInterface::class);
45
        $response->shouldReceive('getType')->once()->andReturn(ResponseType::SUCCESS_ATOM);
46
47
        $this->setExpectations($response);
48
49
        $this->connection->expr('foo');
50
    }
51
52
    public function testRunAtom()
53
    {
54
        $this->connect();
55
56
        $message = \Mockery::mock(MessageInterface::class);
57
        $message->shouldReceive('setOptions')->once()->andReturn();
58
59
        $response = \Mockery::mock(ResponseInterface::class);
60
        $response->shouldReceive('getType')->once()->andReturn(ResponseType::SUCCESS_ATOM);
61
62
        $this->setExpectations($response);
63
64
        try {
65
            $this->assertInstanceOf(ResponseInterface::class, $this->connection->run($message, false));
66
        } catch (\Exception $e) {
67
            $this->fail($e->getMessage());
68
        }
69
    }
70
71
    /**
72
     * @throws Exception
73
     */
74
    public function testRunPartial()
75
    {
76
        $this->connect();
77
78
        $this->stream->shouldReceive('isWritable')->once()->andReturnTrue();
79
80
        $message = \Mockery::mock(MessageInterface::class);
81
        $message->shouldReceive('setOptions')->once()->andReturn();
82
83
        $response = \Mockery::mock(ResponseInterface::class);
84
        $response->shouldReceive('getType')->once()->andReturn(ResponseType::SUCCESS_PARTIAL);
85
        $response->shouldReceive('getData')->once()->andReturn(['yolo']);
86
87
        $this->setExpectations($response);
88
89
        try {
90
            $this->assertInstanceOf(Cursor::class, $this->connection->run($message, false));
91
        } catch (\Exception $e) {
92
            $this->fail($e->getMessage());
93
        }
94
    }
95
96
    /**
97
     * @throws Exception
98
     */
99
    public function testRunSequence()
100
    {
101
        $this->connect();
102
103
        $response = \Mockery::mock(ResponseInterface::class);
104
        $response->shouldReceive('getType')->once()->andReturn(ResponseType::SUCCESS_SEQUENCE);
105
        $response->shouldReceive('getData')->once()->andReturn(['yolo']);
106
107
        $this->setExpectations($response);
108
109
        $message = \Mockery::mock(MessageInterface::class);
110
        $message->shouldReceive('setOptions')->once()->andReturn();
111
112
        try {
113
            $this->assertInstanceOf(Cursor::class, $this->connection->run($message, false));
114
        } catch (\Exception $e) {
115
            $this->fail($e->getMessage());
116
        }
117
    }
118
119
    /**
120
     * @throws Exception
121
     */
122
    public function testChanges()
123
    {
124
        $message = \Mockery::mock(MessageInterface::class);
125
126
        try {
127
            $this->connection->changes($message);
128
        } catch (\Exception $e) {
129
            $this->fail($e->getMessage());
130
        }
131
132
        $this->assertTrue(true);
133
    }
134
135
    /**
136
     * @throws Exception
137
     */
138
    public function testServer(): void
139
    {
140
        $this->connect();
141
142
        $response = \Mockery::mock(ResponseInterface::class);
143
        $response->shouldReceive('getType')->once()->andReturn(ResponseType::SERVER_INFO);
144
        $response->shouldReceive('getData')->once()->andReturn(['yolo']);
145
146
        $this->setExpectations($response);
147
148
        $res = $this->connection->server();
149
150
        $this->assertEquals(QueryType::SERVER_INFO, $res->getType());
151
        $this->assertInternalType('array', $res->getData());
152
        $this->assertEquals(['yolo'], $res->getData());
153
    }
154
155
    /**
156
     * @throws Exception
157
     */
158
    public function testRunNoReply()
159
    {
160
        $this->connect();
161
162
        $message = \Mockery::mock(MessageInterface::class);
163
        $message->shouldReceive('setOptions')->once()->andReturn();
164
165
        $this->setExpectations();
166
167
        try {
168
            $this->connection->runNoReply($message);
169
        } catch (\Exception $e) {
170
            $this->fail($e->getMessage());
171
        }
172
173
        $this->assertTrue(true);
174
    }
175
176
    /**
177
     * @return void
178
     * @throws Exception
179
     */
180
    public function testClose(): void
181
    {
182
        $this->connect();
183
184
        $this->stream->shouldReceive('isWritable')->once()->andReturnTrue();
185
        $this->stream->shouldReceive('close')->once();
186
187
        try {
188
            $this->connection->close(false);
189
        } catch (\Exception $e) {
190
            $this->fail($e->getMessage());
191
        }
192
193
        $this->assertTrue(true);
194
    }
195
196
    /**
197
     * @throws Exception
198
     */
199
    public function testCloseNoReplyWait(): void
200
    {
201
        $this->connect();
202
203
        $response = \Mockery::mock(ResponseInterface::class);
204
        $response->shouldReceive('getType')->once()->andReturn(ResponseType::WAIT_COMPLETE);
205
206
        $this->setExpectations($response);
207
208
        $this->stream->shouldReceive('close')->once();
209
210
        try {
211
            $this->connection->close(true);
212
        } catch (\Exception $e) {
213
            $this->fail($e->getMessage());
214
        }
215
216
        $this->assertTrue(true);
217
    }
218
219
    /**
220
     * @throws Exception
221
     */
222
    public function testUse()
223
    {
224
        try {
225
            $this->connection->use('test');
226
        } catch (\Exception $e) {
227
            $this->fail($e->getMessage());
228
        }
229
230
        $this->assertTrue(true);
231
    }
232
}
233