|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace TBolier\RethinkQL\Connection; |
|
5
|
|
|
|
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Mockery; |
|
8
|
|
|
use Mockery\MockInterface; |
|
9
|
|
|
use TBolier\RethinkQL\Connection\Socket\Socket; |
|
10
|
|
|
use TBolier\RethinkQL\IntegrationTest\BaseTestCase; |
|
11
|
|
|
use TBolier\RethinkQL\Response\ResponseInterface; |
|
12
|
|
|
use TBolier\RethinkQL\Types\Query\QueryType; |
|
13
|
|
|
use TBolier\RethinkQL\Types\Response\ResponseType; |
|
14
|
|
|
|
|
15
|
|
|
function unpack($format = '', $data = '', $offset = 0) |
|
16
|
|
|
{ |
|
17
|
|
|
return ConnectionTest::$functions->unpack($format, $data, $offset); |
|
|
|
|
|
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
function random_int($min = 0, $max = 0) |
|
21
|
|
|
{ |
|
22
|
|
|
return ConnectionTest::$functions->random_int($min, $max); |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
class ConnectionTest extends BaseTestCase |
|
26
|
|
|
{ |
|
27
|
|
|
public static $functions; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var MockInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $optionsMock; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setUp(): void |
|
38
|
|
|
{ |
|
39
|
|
|
parent::setUp(); |
|
40
|
|
|
|
|
41
|
|
|
self::$functions = Mockery::mock(); |
|
42
|
|
|
|
|
43
|
|
|
$this->useDefaultInternals(); |
|
44
|
|
|
|
|
45
|
|
|
$this->optionsMock = Mockery::mock(OptionsInterface::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testConnect(): void |
|
54
|
|
|
{ |
|
55
|
|
|
/** @var ConnectionInterface $connection */ |
|
56
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
57
|
|
|
$result = $connection->expr('foo'); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $result); |
|
60
|
|
|
$this->assertEquals('foo', $result->getData()[0]); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
public function testConnectWithOpenConnection(): void |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var ConnectionInterface $connection */ |
|
71
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
72
|
|
|
$connection2 = $connection->connect(); |
|
73
|
|
|
|
|
74
|
|
|
$this->assertSame($connection, $connection2); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
79
|
|
|
* @expectedExceptionMessage Test exception |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function testConnectThrowsCorrectException(): void |
|
83
|
|
|
{ |
|
84
|
|
|
$options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']); |
|
85
|
|
|
|
|
86
|
|
|
$handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface'); |
|
87
|
|
|
$handshakeMock->shouldReceive('hello')->andThrow(new \Exception('Test exception', 404)); |
|
88
|
|
|
|
|
89
|
|
|
$serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface'); |
|
90
|
|
|
|
|
91
|
|
|
$connection = new Connection( |
|
92
|
|
|
function () use ($options) { |
|
93
|
|
|
return new Socket( |
|
94
|
|
|
$options |
|
95
|
|
|
); |
|
96
|
|
|
}, |
|
97
|
|
|
$handshakeMock, |
|
98
|
|
|
$options->getDbname(), |
|
99
|
|
|
$serializerMock, |
|
100
|
|
|
$serializerMock |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$connection->connect(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function testServer(): void |
|
110
|
|
|
{ |
|
111
|
|
|
/** @var ConnectionInterface $connection */ |
|
112
|
|
|
$res = $this->createConnection('phpunit_default')->connect()->server(); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertEquals(QueryType::SERVER_INFO, $res->getType()); |
|
115
|
|
|
$this->assertInternalType('string', $res->getData()[0]['name']); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
120
|
|
|
* @expectedExceptionMessage Not connected |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testServerThrowsNotConnectedException(): void |
|
123
|
|
|
{ |
|
124
|
|
|
/** @var ConnectionInterface $connection */ |
|
125
|
|
|
$this->createConnection('phpunit_default')->server(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
130
|
|
|
*/ |
|
131
|
|
|
public function testServerThrowsExceptionOnConnectionException(): void |
|
132
|
|
|
{ |
|
133
|
|
|
$options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']); |
|
134
|
|
|
|
|
135
|
|
|
$handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface'); |
|
136
|
|
|
$handshakeMock->shouldReceive('hello'); |
|
137
|
|
|
|
|
138
|
|
|
$serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface'); |
|
139
|
|
|
|
|
140
|
|
|
$connection = new Connection( |
|
141
|
|
|
function () use ($options) { |
|
142
|
|
|
return new Socket( |
|
143
|
|
|
$options |
|
144
|
|
|
); |
|
145
|
|
|
}, |
|
146
|
|
|
$handshakeMock, |
|
147
|
|
|
$options->getDbname(), |
|
148
|
|
|
$serializerMock, |
|
149
|
|
|
$serializerMock |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
/** @var ConnectionInterface $connection */ |
|
153
|
|
|
$connection->connect()->server(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return void |
|
158
|
|
|
*/ |
|
159
|
|
|
public function testClose(): void |
|
160
|
|
|
{ |
|
161
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
162
|
|
|
|
|
163
|
|
|
$this->assertTrue($connection->isStreamOpen()); |
|
164
|
|
|
|
|
165
|
|
|
$connection->close(false); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertFalse($connection->isStreamOpen()); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @return void |
|
172
|
|
|
*/ |
|
173
|
|
|
public function testCloseNoReplyWait(): void |
|
174
|
|
|
{ |
|
175
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertTrue($connection->isStreamOpen()); |
|
178
|
|
|
|
|
179
|
|
|
$connection->close(true); |
|
180
|
|
|
|
|
181
|
|
|
$this->assertFalse($connection->isStreamOpen()); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
186
|
|
|
* @expectedExceptionMessage Not connected |
|
187
|
|
|
* @return void |
|
188
|
|
|
*/ |
|
189
|
|
|
public function testCloseThrowsExceptionOnNotConnected(): void |
|
190
|
|
|
{ |
|
191
|
|
|
$connection = $this->createConnection('phpunit_default'); |
|
192
|
|
|
$connection->close(); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
197
|
|
|
* @expectedExceptionMessage Not connected |
|
198
|
|
|
* @return void |
|
199
|
|
|
*/ |
|
200
|
|
|
public function testRunThrowsExceptionOnNotConnected(): void |
|
201
|
|
|
{ |
|
202
|
|
|
$connection = $this->createConnection('phpunit_default'); |
|
203
|
|
|
$connection->run(Mockery::mock('\TBolier\RethinkQL\Query\MessageInterface')); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* @return void |
|
208
|
|
|
*/ |
|
209
|
|
|
public function testRunNoReply(): void |
|
210
|
|
|
{ |
|
211
|
|
|
$message = $this->setUpMessageMock(); |
|
212
|
|
|
|
|
213
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
214
|
|
|
|
|
215
|
|
|
$reply = $connection->runNoReply($message); |
|
216
|
|
|
|
|
217
|
|
|
$connection->close(false); |
|
218
|
|
|
|
|
219
|
|
|
$this->assertEquals([], $reply); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
224
|
|
|
* @expectedExceptionMessage Generating the token failed. |
|
225
|
|
|
* @return void |
|
226
|
|
|
*/ |
|
227
|
|
|
public function testNoReplyException(): void |
|
228
|
|
|
{ |
|
229
|
|
|
self::$functions->shouldReceive('random_int')->andThrow(Exception::class, 'foo')->byDefault(); |
|
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
232
|
|
|
|
|
233
|
|
|
$connection->close(true); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return void |
|
238
|
|
|
*/ |
|
239
|
|
|
public function testRunPartialSuccess(): void |
|
240
|
|
|
{ |
|
241
|
|
|
$expectedToken = [ |
|
242
|
|
|
'token' => 1, |
|
243
|
|
|
'token2' => 0, |
|
244
|
|
|
'size' => 19 |
|
245
|
|
|
]; |
|
246
|
|
|
|
|
247
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
248
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
$message = $this->setUpMessageMock(); |
|
251
|
|
|
|
|
252
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
253
|
|
|
$responseMock->shouldReceive('getType')->andReturn(ResponseType::SUCCESS_PARTIAL); |
|
254
|
|
|
|
|
255
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
256
|
|
|
|
|
257
|
|
|
$connection = $connection->connect(); |
|
258
|
|
|
|
|
259
|
|
|
$reply = $connection->run($message); |
|
260
|
|
|
|
|
261
|
|
|
$connection->close(false); |
|
262
|
|
|
|
|
263
|
|
|
$this->assertEquals($responseMock, $reply); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
269
|
|
|
* @expectedExceptionMessage Serializing query message failed. |
|
270
|
|
|
* @return void |
|
271
|
|
|
*/ |
|
272
|
|
|
public function testIfRunThrowsException(): void |
|
273
|
|
|
{ |
|
274
|
|
|
/** @var ConnectionInterface $connection */ |
|
275
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
276
|
|
|
|
|
277
|
|
|
$messageMock = Mockery::mock('\TBolier\RethinkQL\Query\Message'); |
|
278
|
|
|
$messageMock->shouldReceive('setOptions'); |
|
279
|
|
|
|
|
280
|
|
|
$connection->run($messageMock); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
285
|
|
|
* @expectedExceptionMessage Generating the token failed. |
|
286
|
|
|
* @return void |
|
287
|
|
|
*/ |
|
288
|
|
|
public function testIfGenerateTokenThrowsException(): void |
|
289
|
|
|
{ |
|
290
|
|
|
self::$functions = Mockery::mock(); |
|
291
|
|
|
|
|
292
|
|
|
self::$functions->shouldReceive('random_int')->andThrow(Exception::class, 'foo')->byDefault(); |
|
|
|
|
|
|
293
|
|
|
|
|
294
|
|
|
/** @var ConnectionInterface $connection */ |
|
295
|
|
|
$connection = $this->createConnection('phpunit_default')->connect(); |
|
296
|
|
|
|
|
297
|
|
|
$messageMock = Mockery::mock('\TBolier\RethinkQL\Query\Message'); |
|
298
|
|
|
$messageMock->shouldReceive('setOptions'); |
|
299
|
|
|
|
|
300
|
|
|
$connection->run($messageMock); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
305
|
|
|
* @expectedExceptionMessage Response message has no type. |
|
306
|
|
|
* @return void |
|
307
|
|
|
*/ |
|
308
|
|
|
public function testConnectionExceptionThrownOnNoResponseType(): void |
|
309
|
|
|
{ |
|
310
|
|
|
$expectedToken = [ |
|
311
|
|
|
'token' => 1, |
|
312
|
|
|
'token2' => 0, |
|
313
|
|
|
'size' => 19 |
|
314
|
|
|
]; |
|
315
|
|
|
|
|
316
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
317
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
318
|
|
|
|
|
319
|
|
|
$message = $this->setUpMessageMock(); |
|
320
|
|
|
|
|
321
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
322
|
|
|
$responseMock->shouldReceive('getType')->andReturn(0); |
|
323
|
|
|
|
|
324
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
325
|
|
|
$connection = $connection->connect(); |
|
326
|
|
|
|
|
327
|
|
|
$reply = $connection->run($message); |
|
328
|
|
|
|
|
329
|
|
|
$connection->close(false); |
|
330
|
|
|
|
|
331
|
|
|
$this->assertEquals($responseMock, $reply); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
336
|
|
|
* @expectedExceptionMessage Compile error: foo, jsonQuery: "{}" |
|
337
|
|
|
* @return void |
|
338
|
|
|
*/ |
|
339
|
|
|
public function testConnectionExceptionThrownOnCompileError(): void |
|
340
|
|
|
{ |
|
341
|
|
|
$expectedToken = [ |
|
342
|
|
|
'token' => 1, |
|
343
|
|
|
'token2' => 0, |
|
344
|
|
|
'size' => 19 |
|
345
|
|
|
]; |
|
346
|
|
|
|
|
347
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
348
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
349
|
|
|
|
|
350
|
|
|
$message = $this->setUpMessageMock(); |
|
351
|
|
|
|
|
352
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
353
|
|
|
$responseMock->shouldReceive('getType')->andReturn(17); |
|
354
|
|
|
$responseMock->shouldReceive('getData')->andReturn([0=>'foo']); |
|
355
|
|
|
|
|
356
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
357
|
|
|
$connection = $connection->connect(); |
|
358
|
|
|
|
|
359
|
|
|
$reply = $connection->run($message); |
|
360
|
|
|
|
|
361
|
|
|
$connection->close(false); |
|
362
|
|
|
|
|
363
|
|
|
$this->assertEquals($responseMock, $reply); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
369
|
|
|
* @expectedExceptionMessage Runtime error: foo, jsonQuery: "{}" |
|
370
|
|
|
* @return void |
|
371
|
|
|
*/ |
|
372
|
|
|
public function testConnectionExceptionThrownOnRuntimeError(): void |
|
373
|
|
|
{ |
|
374
|
|
|
$expectedToken = [ |
|
375
|
|
|
'token' => 1, |
|
376
|
|
|
'token2' => 0, |
|
377
|
|
|
'size' => 19 |
|
378
|
|
|
]; |
|
379
|
|
|
|
|
380
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
381
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
382
|
|
|
|
|
383
|
|
|
$message = $this->setUpMessageMock(); |
|
384
|
|
|
|
|
385
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
386
|
|
|
$responseMock->shouldReceive('getType')->andReturn(18); |
|
387
|
|
|
$responseMock->shouldReceive('getData')->andReturn([0=>'foo']); |
|
388
|
|
|
|
|
389
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
390
|
|
|
$connection = $connection->connect(); |
|
391
|
|
|
|
|
392
|
|
|
$reply = $connection->run($message); |
|
393
|
|
|
|
|
394
|
|
|
$connection->close(false); |
|
395
|
|
|
|
|
396
|
|
|
$this->assertEquals($responseMock, $reply); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
401
|
|
|
* @expectedExceptionMessage Received wrong token. Response does not match the request. Expected 1, received 11 |
|
402
|
|
|
* @return void |
|
403
|
|
|
*/ |
|
404
|
|
|
public function testConnectionExceptionThrownOnInvalidResponseToken(): void |
|
405
|
|
|
{ |
|
406
|
|
|
$expectedToken = [ |
|
407
|
|
|
'token' => 11, |
|
408
|
|
|
'token2' => 0, |
|
409
|
|
|
'size' => 19 |
|
410
|
|
|
]; |
|
411
|
|
|
|
|
412
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
413
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
414
|
|
|
|
|
415
|
|
|
$message = $this->setUpMessageMock(); |
|
416
|
|
|
|
|
417
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
418
|
|
|
$responseMock->shouldReceive('getType')->andReturn(18); |
|
419
|
|
|
$responseMock->shouldReceive('getData')->andReturn([0=>'foo']); |
|
420
|
|
|
|
|
421
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
422
|
|
|
$connection = $connection->connect(); |
|
423
|
|
|
|
|
424
|
|
|
$reply = $connection->run($message); |
|
425
|
|
|
|
|
426
|
|
|
$connection->close(false); |
|
427
|
|
|
|
|
428
|
|
|
$this->assertEquals($responseMock, $reply); |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
433
|
|
|
* @expectedExceptionMessage Invalid response from server: Invalid token. |
|
434
|
|
|
* @return void |
|
435
|
|
|
*/ |
|
436
|
|
|
public function testConnectionExceptionThrownOnInvalidResponseToken2(): void |
|
437
|
|
|
{ |
|
438
|
|
|
$expectedToken = [ |
|
439
|
|
|
'token' => 1, |
|
440
|
|
|
'token2' => 42, |
|
441
|
|
|
'size' => 19 |
|
442
|
|
|
]; |
|
443
|
|
|
|
|
444
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
445
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
446
|
|
|
|
|
447
|
|
|
$message = $this->setUpMessageMock(); |
|
448
|
|
|
|
|
449
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
450
|
|
|
$responseMock->shouldReceive('getType')->andReturn(18); |
|
451
|
|
|
$responseMock->shouldReceive('getData')->andReturn([0=>'foo']); |
|
452
|
|
|
|
|
453
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
454
|
|
|
$connection = $connection->connect(); |
|
455
|
|
|
|
|
456
|
|
|
$reply = $connection->run($message); |
|
457
|
|
|
|
|
458
|
|
|
$connection->close(false); |
|
459
|
|
|
|
|
460
|
|
|
$this->assertEquals($responseMock, $reply); |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* @expectedException \TBolier\RethinkQL\Connection\ConnectionException |
|
465
|
|
|
* @expectedExceptionMessage Server says PHP-RQL is buggy: |
|
466
|
|
|
* @return void |
|
467
|
|
|
*/ |
|
468
|
|
|
public function testConnectionExceptionThrownOnClientErrorResponseType(): void |
|
469
|
|
|
{ |
|
470
|
|
|
$expectedToken = [ |
|
471
|
|
|
'token' => 1, |
|
472
|
|
|
'token2' => 0, |
|
473
|
|
|
'size' => 19 |
|
474
|
|
|
]; |
|
475
|
|
|
|
|
476
|
|
|
self::$functions->shouldReceive('unpack')->with('Vtoken/Vtoken2/Vsize', '', 0)->andReturn($expectedToken); |
|
477
|
|
|
self::$functions->shouldReceive('random_int')->andReturn(1); |
|
|
|
|
|
|
478
|
|
|
|
|
479
|
|
|
$message = $this->setUpMessageMock(); |
|
480
|
|
|
|
|
481
|
|
|
$responseMock = Mockery::mock('\TBolier\RethinkQL\Response\ResponseInterface'); |
|
482
|
|
|
$responseMock->shouldReceive('getType')->andReturn(16); |
|
483
|
|
|
$responseMock->shouldReceive('getData')->andReturn([0=>'foo']); |
|
484
|
|
|
|
|
485
|
|
|
$connection = $this->setUpMockConnection($responseMock); |
|
486
|
|
|
$connection = $connection->connect(); |
|
487
|
|
|
|
|
488
|
|
|
$reply = $connection->run($message); |
|
489
|
|
|
|
|
490
|
|
|
$connection->close(false); |
|
491
|
|
|
|
|
492
|
|
|
$this->assertEquals($responseMock, $reply); |
|
493
|
|
|
} |
|
494
|
|
|
|
|
495
|
|
|
/** |
|
496
|
|
|
* @return void |
|
497
|
|
|
*/ |
|
498
|
|
|
public function testUse(): void |
|
499
|
|
|
{ |
|
500
|
|
|
$connection = $this->createConnection('phpunit_default'); |
|
501
|
|
|
$connection->use('foo'); |
|
502
|
|
|
|
|
503
|
|
|
$reflection = new \ReflectionClass($connection); |
|
504
|
|
|
$dbName = $reflection->getProperty('dbName'); |
|
505
|
|
|
$dbName->setAccessible(true); |
|
506
|
|
|
$this->assertEquals('foo', $dbName->getValue($connection));} |
|
507
|
|
|
|
|
508
|
|
|
/** |
|
509
|
|
|
* Resets the mocks to their PHP internal equivalents |
|
510
|
|
|
*/ |
|
511
|
|
|
private function useDefaultInternals(): void |
|
512
|
|
|
{ |
|
513
|
|
|
// Reset expectations to use PHP built-in functions |
|
514
|
|
|
self::$functions = Mockery::mock(); |
|
515
|
|
|
|
|
516
|
|
|
self::$functions->shouldReceive('random_int')->byDefault()->andReturnUsing(function($min = 0, $max = 0) { |
|
|
|
|
|
|
517
|
|
|
return \random_int($min, $max); |
|
518
|
|
|
}); |
|
519
|
|
|
|
|
520
|
|
|
self::$functions->shouldReceive('unpack')->byDefault()->andReturnUsing(function($format = '', $data = '', $offset = 0) { |
|
|
|
|
|
|
521
|
|
|
return \unpack($format, $data, $offset); |
|
522
|
|
|
}); |
|
523
|
|
|
} |
|
524
|
|
|
|
|
525
|
|
|
/** |
|
526
|
|
|
* @return MockInterface |
|
527
|
|
|
*/ |
|
528
|
|
|
private function setUpMessageMock(): MockInterface |
|
529
|
|
|
{ |
|
530
|
|
|
$message = Mockery::mock('\TBolier\RethinkQL\Query\MessageInterface'); |
|
531
|
|
|
$message->shouldReceive('setOptions')->andReturnSelf()->getMock(); |
|
532
|
|
|
$message->shouldReceive('jsonSerialize')->andReturn('{}')->getMock(); |
|
|
|
|
|
|
533
|
|
|
|
|
534
|
|
|
return $message; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
/** |
|
538
|
|
|
* @return ConnectionInterface |
|
539
|
|
|
*/ |
|
540
|
|
|
private function setUpMockConnection(MockInterface $responseMock): ConnectionInterface |
|
541
|
|
|
{ |
|
542
|
|
|
$handshakeMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\HandshakeInterface'); |
|
543
|
|
|
$handshakeMock->shouldReceive('hello')->andReturnSelf(); |
|
544
|
|
|
|
|
545
|
|
|
$serializerMock = Mockery::mock('\Symfony\Component\Serializer\SerializerInterface'); |
|
546
|
|
|
$serializerMock->shouldReceive('serialize')->andReturn('{}'); |
|
|
|
|
|
|
547
|
|
|
|
|
548
|
|
|
$serializerMock->shouldReceive('deserialize')->andReturn($responseMock); |
|
549
|
|
|
|
|
550
|
|
|
$streamMock = Mockery::mock('\TBolier\RethinkQL\Connection\Socket\Socket'); |
|
551
|
|
|
$streamMock->shouldReceive('isWritable')->andReturn(true); |
|
552
|
|
|
$streamMock->shouldReceive('write')->andReturn(1); |
|
553
|
|
|
$streamMock->shouldReceive('close')->andReturn(1); |
|
554
|
|
|
$streamMock->shouldReceive('read')->andReturn(''); |
|
555
|
|
|
|
|
556
|
|
|
$options = new Options(PHPUNIT_CONNECTIONS['phpunit_default']); |
|
557
|
|
|
|
|
558
|
|
|
$connection = new Connection( |
|
559
|
|
|
function () use ($options, $streamMock) { |
|
560
|
|
|
return $streamMock; |
|
561
|
|
|
}, |
|
562
|
|
|
$handshakeMock, |
|
563
|
|
|
$options->getDbname(), |
|
564
|
|
|
$serializerMock, |
|
565
|
|
|
$serializerMock |
|
566
|
|
|
); |
|
567
|
|
|
|
|
568
|
|
|
|
|
569
|
|
|
return $connection; |
|
570
|
|
|
} |
|
571
|
|
|
|
|
572
|
|
|
/** |
|
573
|
|
|
* @return void |
|
574
|
|
|
*/ |
|
575
|
|
|
public function tearDown(): void |
|
576
|
|
|
{ |
|
577
|
|
|
$this->useDefaultInternals(); |
|
578
|
|
|
|
|
579
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub |
|
580
|
|
|
} |
|
581
|
|
|
} |
|
582
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.