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)); |
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)); |
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)); |
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)); |
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()); |
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)); |
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
|
|
|
|