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\Query\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 BaseConnectionTestCase |
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
|
|
|
public function testExpr() |
40
|
|
|
{ |
41
|
|
|
$this->connect(); |
42
|
|
|
|
43
|
|
|
$response = \Mockery::mock(ResponseInterface::class); |
44
|
|
|
$response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_ATOM); |
45
|
|
|
|
46
|
|
|
$this->setExpectations($response); |
47
|
|
|
|
48
|
|
|
$this->connection->expr('foo'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testRunAtom() |
52
|
|
|
{ |
53
|
|
|
$this->connect(); |
54
|
|
|
|
55
|
|
|
$message = \Mockery::mock(MessageInterface::class); |
56
|
|
|
$message->shouldReceive('setOptions')->once()->andReturn(); |
57
|
|
|
|
58
|
|
|
$response = \Mockery::mock(ResponseInterface::class); |
59
|
|
|
$response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_ATOM); |
60
|
|
|
|
61
|
|
|
$this->setExpectations($response); |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $this->connection->run($message, false)); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
$this->fail($e->getMessage()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws Exception |
72
|
|
|
*/ |
73
|
|
|
public function testRunPartial() |
74
|
|
|
{ |
75
|
|
|
$this->connect(); |
76
|
|
|
|
77
|
|
|
$message = \Mockery::mock(MessageInterface::class); |
78
|
|
|
$message->shouldReceive('setOptions')->once()->andReturn(); |
79
|
|
|
|
80
|
|
|
$response = \Mockery::mock(ResponseInterface::class); |
81
|
|
|
$response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_PARTIAL); |
82
|
|
|
$response->shouldReceive('getData')->atLeast()->andReturn(['yolo']); |
83
|
|
|
|
84
|
|
|
$this->setExpectations($response); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
$this->assertInstanceOf(Cursor::class, $this->connection->run($message, false)); |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
$this->fail($e->getMessage()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws Exception |
95
|
|
|
*/ |
96
|
|
|
public function testRunSequence() |
97
|
|
|
{ |
98
|
|
|
$this->connect(); |
99
|
|
|
|
100
|
|
|
$response = \Mockery::mock(ResponseInterface::class); |
101
|
|
|
$response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SUCCESS_SEQUENCE); |
102
|
|
|
$response->shouldReceive('getData')->atLeast()->andReturn(['yolo']); |
103
|
|
|
|
104
|
|
|
$this->setExpectations($response); |
105
|
|
|
|
106
|
|
|
$message = \Mockery::mock(MessageInterface::class); |
107
|
|
|
$message->shouldReceive('setOptions')->once()->andReturn(); |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$this->assertInstanceOf(Cursor::class, $this->connection->run($message, false)); |
111
|
|
|
} catch (\Exception $e) { |
112
|
|
|
$this->fail($e->getMessage()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws Exception |
118
|
|
|
*/ |
119
|
|
|
public function testChanges() |
120
|
|
|
{ |
121
|
|
|
$message = \Mockery::mock(MessageInterface::class); |
122
|
|
|
|
123
|
|
|
try { |
124
|
|
|
$this->connection->changes($message); |
125
|
|
|
} catch (\Exception $e) { |
126
|
|
|
$this->fail($e->getMessage()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->assertTrue(true); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @throws Exception |
134
|
|
|
*/ |
135
|
|
|
public function testServer(): void |
136
|
|
|
{ |
137
|
|
|
$this->connect(); |
138
|
|
|
|
139
|
|
|
$response = \Mockery::mock(ResponseInterface::class); |
140
|
|
|
$response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::SERVER_INFO); |
141
|
|
|
$response->shouldReceive('getData')->atLeast()->andReturn(['yolo']); |
142
|
|
|
|
143
|
|
|
$this->setExpectations($response); |
144
|
|
|
|
145
|
|
|
$res = $this->connection->server(); |
146
|
|
|
|
147
|
|
|
$this->assertEquals(QueryType::SERVER_INFO, $res->getType()); |
148
|
|
|
$this->assertInternalType('array', $res->getData()); |
149
|
|
|
$this->assertEquals(['yolo'], $res->getData()); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @throws Exception |
154
|
|
|
*/ |
155
|
|
|
public function testRunNoReply() |
156
|
|
|
{ |
157
|
|
|
$this->connect(); |
158
|
|
|
|
159
|
|
|
$message = \Mockery::mock(MessageInterface::class); |
160
|
|
|
$message->shouldReceive('setOptions')->once()->andReturn(); |
161
|
|
|
|
162
|
|
|
$this->setExpectations(); |
163
|
|
|
|
164
|
|
|
try { |
165
|
|
|
$this->connection->runNoReply($message); |
166
|
|
|
} catch (\Exception $e) { |
167
|
|
|
$this->fail($e->getMessage()); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->assertTrue(true); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return void |
175
|
|
|
* @throws Exception |
176
|
|
|
*/ |
177
|
|
|
public function testClose(): void |
178
|
|
|
{ |
179
|
|
|
$this->connect(); |
180
|
|
|
|
181
|
|
|
$this->stream->shouldReceive('close')->once(); |
182
|
|
|
|
183
|
|
|
try { |
184
|
|
|
$this->connection->close(false); |
185
|
|
|
} catch (\Exception $e) { |
186
|
|
|
$this->fail($e->getMessage()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->assertTrue(true); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @throws Exception |
194
|
|
|
*/ |
195
|
|
|
public function testCloseNoReplyWait(): void |
196
|
|
|
{ |
197
|
|
|
$this->connect(); |
198
|
|
|
|
199
|
|
|
$response = \Mockery::mock(ResponseInterface::class); |
200
|
|
|
$response->shouldReceive('getType')->atLeast()->andReturn(ResponseType::WAIT_COMPLETE); |
201
|
|
|
|
202
|
|
|
$this->setExpectations($response); |
203
|
|
|
|
204
|
|
|
$this->stream->shouldReceive('close')->once(); |
205
|
|
|
|
206
|
|
|
try { |
207
|
|
|
$this->connection->close(true); |
208
|
|
|
} catch (\Exception $e) { |
209
|
|
|
$this->fail($e->getMessage()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$this->assertTrue(true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @throws Exception |
217
|
|
|
*/ |
218
|
|
|
public function testUse() |
219
|
|
|
{ |
220
|
|
|
try { |
221
|
|
|
$this->connection->use('test'); |
222
|
|
|
} catch (\Exception $e) { |
223
|
|
|
$this->fail($e->getMessage()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->assertTrue(true); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|