ConnectionTest::testCloseNoReplyWait()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 18
rs 9.9332
cc 2
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\Message\MessageInterface;
9
use TBolier\RethinkQL\Response\Cursor;
10
use TBolier\RethinkQL\Response\ResponseInterface;
11
use TBolier\RethinkQL\Types\Query\QueryType;
12
use TBolier\RethinkQL\Types\Response\ResponseType;
13
14
class ConnectionTest extends ConnectionTestCase
15
{
16
    /**
17
     * @return void
18
     */
19
    public function setUp(): void
20
    {
21
        parent::setUp();
22
    }
23
24
    /**
25
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
26
     * @expectedExceptionMessage Test exception
27
     * @return void
28
     * @throws ConnectionException
29
     */
30
    public function testConnectThrowsCorrectException(): void
31
    {
32
        $this->handshake->shouldReceive('hello')->once()->andThrow(
33
            new ConnectionException('Test exception')
34
        );
35
36
        $this->connection->connect();
37
    }
38
39
    /**
40
     * @expectedException \TBolier\RethinkQL\Connection\ConnectionException
41
     * @expectedExceptionMessage No open stream, please connect first
42
     */
43
    public function testQueryWithoutConnection(): void
44
    {
45
        $this->connection->writeQuery(1223456789, \Mockery::mock(MessageInterface::class));
0 ignored issues
show
Bug introduced by
Mockery::mock(TBolier\Re...essageInterface::class) of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type TBolier\RethinkQL\Message\MessageInterface expected by parameter $message of TBolier\RethinkQL\Connec...onnection::writeQuery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $this->connection->writeQuery(1223456789, /** @scrutinizer ignore-type */ \Mockery::mock(MessageInterface::class));
Loading history...
46
    }
47
48
    public function testExpr()
49
    {
50
        $this->connect();
51
52
        $response = \Mockery::mock(ResponseInterface::class);
53
        $response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_ATOM);
54
55
        $this->setExpectations($response);
56
57
        $this->connection->expr('foo');
58
    }
59
60
    public function testRunAtom()
61
    {
62
        $this->connect();
63
64
        $message = \Mockery::mock(MessageInterface::class);
65
        $message->shouldReceive('setOptions')->once()->andReturn();
66
67
        $response = \Mockery::mock(ResponseInterface::class);
68
        $response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_ATOM);
69
70
        $this->setExpectations($response);
71
72
        try {
73
            $this->assertInstanceOf(ResponseInterface::class, $this->connection->run($message, false));
0 ignored issues
show
Bug introduced by
$message of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type TBolier\RethinkQL\Message\MessageInterface expected by parameter $message of TBolier\RethinkQL\Connection\Connection::run(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $this->assertInstanceOf(ResponseInterface::class, $this->connection->run(/** @scrutinizer ignore-type */ $message, false));
Loading history...
74
        } catch (\Exception $e) {
75
            $this->fail($e->getMessage());
76
        }
77
    }
78
79
    /**
80
     * @throws Exception
81
     */
82
    public function testRunPartial()
83
    {
84
        $this->connect();
85
86
        $message = \Mockery::mock(MessageInterface::class);
87
        $message->shouldReceive('setOptions')->once()->andReturn();
88
89
        $response = \Mockery::mock(ResponseInterface::class);
90
        $response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_PARTIAL);
91
        $response->shouldReceive('getData')->atLeast()->andReturn(['yolo']);
92
        $response->shouldReceive('isAtomic')->once()->andReturn(true);
93
94
        $this->setExpectations($response);
95
96
        try {
97
            $this->assertInstanceOf(Cursor::class, $this->connection->run($message, false));
0 ignored issues
show
Bug introduced by
$message of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type TBolier\RethinkQL\Message\MessageInterface expected by parameter $message of TBolier\RethinkQL\Connection\Connection::run(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            $this->assertInstanceOf(Cursor::class, $this->connection->run(/** @scrutinizer ignore-type */ $message, false));
Loading history...
98
        } catch (\Exception $e) {
99
            $this->fail($e->getMessage());
100
        }
101
    }
102
103
    /**
104
     * @throws Exception
105
     */
106
    public function testRunSequence()
107
    {
108
        $this->connect();
109
110
        $response = \Mockery::mock(ResponseInterface::class);
111
        $response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_SEQUENCE);
112
        $response->shouldReceive('getData')->atLeast()->andReturn(['yolo']);
113
        $response->shouldReceive('isAtomic')->once()->andReturn(true);
114
115
        $this->setExpectations($response);
116
117
        $message = \Mockery::mock(MessageInterface::class);
118
        $message->shouldReceive('setOptions')->once()->andReturn();
119
120
        try {
121
            $this->assertInstanceOf(Cursor::class, $this->connection->run($message, false));
0 ignored issues
show
Bug introduced by
$message of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type TBolier\RethinkQL\Message\MessageInterface expected by parameter $message of TBolier\RethinkQL\Connection\Connection::run(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
            $this->assertInstanceOf(Cursor::class, $this->connection->run(/** @scrutinizer ignore-type */ $message, false));
Loading history...
122
        } catch (\Exception $e) {
123
            $this->fail($e->getMessage());
124
        }
125
    }
126
127
    /**
128
     * @throws Exception
129
     */
130
    public function testServer(): void
131
    {
132
        $this->connect();
133
134
        $response = \Mockery::mock(ResponseInterface::class);
135
        $response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SERVER_INFO);
136
        $response->shouldReceive('getData')->atLeast()->andReturn(['yolo']);
137
138
        $this->setExpectations($response);
139
140
        $res = $this->connection->server();
141
142
        $this->assertEquals(QueryType::SERVER_INFO, $res->getType());
143
        $this->assertInternalType('array', $res->getData());
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertInternalType() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/3369 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

143
        /** @scrutinizer ignore-deprecated */ $this->assertInternalType('array', $res->getData());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
144
        $this->assertEquals(['yolo'], $res->getData());
145
    }
146
147
    /**
148
     * @throws Exception
149
     */
150
    public function testRunNoReply()
151
    {
152
        $this->connect();
153
154
        $message = \Mockery::mock(MessageInterface::class);
155
        $message->shouldReceive('setOptions')->once()->andReturn();
156
157
        $this->setExpectations();
158
159
        try {
160
            $this->assertEmpty($this->connection->runNoReply($message));
0 ignored issues
show
Bug introduced by
$message of type Mockery\LegacyMockInterface|Mockery\MockInterface is incompatible with the type TBolier\RethinkQL\Message\MessageInterface expected by parameter $query of TBolier\RethinkQL\Connec...onnection::runNoReply(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

160
            $this->assertEmpty($this->connection->runNoReply(/** @scrutinizer ignore-type */ $message));
Loading history...
161
        } catch (\Exception $e) {
162
            $this->fail($e->getMessage());
163
        }
164
165
        $this->assertTrue(true);
166
    }
167
168
    /**
169
     * @return void
170
     * @throws Exception
171
     */
172
    public function testClose(): void
173
    {
174
        $this->connect();
175
176
        $this->stream->shouldReceive('close')->once();
177
178
        try {
179
            $this->connection->close(false);
180
        } catch (\Exception $e) {
181
            $this->fail($e->getMessage());
182
        }
183
184
        $this->assertTrue(true);
185
    }
186
187
    /**
188
     * @throws Exception
189
     */
190
    public function testCloseNoReplyWait(): void
191
    {
192
        $this->connect();
193
194
        $response = \Mockery::mock(ResponseInterface::class);
195
        $response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::WAIT_COMPLETE);
196
197
        $this->setExpectations($response);
198
199
        $this->stream->shouldReceive('close')->once();
200
201
        try {
202
            $this->connection->close(true);
203
        } catch (\Exception $e) {
204
            $this->fail($e->getMessage());
205
        }
206
207
        $this->assertTrue(true);
208
    }
209
210
    /**
211
     * @throws Exception
212
     */
213
    public function testUse()
214
    {
215
        try {
216
            $this->connection->use('test');
217
        } catch (\Exception $e) {
218
            $this->fail($e->getMessage());
219
        }
220
221
        $this->assertTrue(true);
222
    }
223
}
224