1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Foolz\SphinxQL\Drivers\Mysqli\Connection; |
4
|
|
|
use Foolz\Sphinxql\Drivers\ResultSetInterface; |
5
|
|
|
use Foolz\SphinxQL\SphinxQL; |
6
|
|
|
use Foolz\SphinxQL\Tests\TestUtil; |
7
|
|
|
|
8
|
|
|
class ResultSetTest extends \PHPUnit\Framework\TestCase |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Connection |
12
|
|
|
*/ |
13
|
|
|
public static $conn = null; |
14
|
|
|
|
15
|
|
|
public static $data = array( |
16
|
|
|
0 => array('id' => '10', 'gid' => '9003', |
17
|
|
|
'title' => 'modifying the same line again', 'content' => 'because i am that lazy'), |
18
|
|
|
1 => array('id' => '11', 'gid' => '201', |
19
|
|
|
'title' => 'replacing value by value', 'content' => 'i have no idea who would use this directly'), |
20
|
|
|
2 => array('id' => '12', 'gid' => '200', |
21
|
|
|
'title' => 'simple logic', 'content' => 'inside the box there was the content'), |
22
|
|
|
3 => array('id' => '13', 'gid' => '304', |
23
|
|
|
'title' => 'i am getting bored', 'content' => 'with all this CONTENT'), |
24
|
|
|
4 => array('id' => '14', 'gid' => '304', |
25
|
|
|
'title' => 'i want a vacation', 'content' => 'the code is going to break sometime'), |
26
|
|
|
5 => array('id' => '15', 'gid' => '304', |
27
|
|
|
'title' => 'there\'s no hope in this class', 'content' => 'just give up'), |
28
|
|
|
6 => array('id' => '16', 'gid' => '500', |
29
|
|
|
'title' => 'we need to test', 'content' => 'selecting the best result in groups'), |
30
|
|
|
7 => array('id' => '17', 'gid' => '500', |
31
|
|
|
'title' => 'what is there to do', 'content' => 'we need to create dummy data for tests'), |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
public static function setUpBeforeClass() |
35
|
|
|
{ |
36
|
|
|
$conn = TestUtil::getConnectionDriver(); |
37
|
|
|
$conn->setParam('port', 9307); |
38
|
|
|
self::$conn = $conn; |
|
|
|
|
39
|
|
|
|
40
|
|
|
(new SphinxQL(self::$conn))->getConnection()->query('TRUNCATE RTINDEX rt'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return SphinxQL |
45
|
|
|
*/ |
46
|
|
|
protected function createSphinxQL() |
47
|
|
|
{ |
48
|
|
|
return new SphinxQL(self::$conn); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function refill() |
52
|
|
|
{ |
53
|
|
|
$this->createSphinxQL()->getConnection()->query('TRUNCATE RTINDEX rt'); |
54
|
|
|
|
55
|
|
|
$sq = $this->createSphinxQL() |
56
|
|
|
->insert() |
57
|
|
|
->into('rt') |
58
|
|
|
->columns('id', 'gid', 'title', 'content'); |
59
|
|
|
|
60
|
|
|
foreach (static::$data as $row) { |
61
|
|
|
$sq->values($row['id'], $row['gid'], $row['title'], $row['content']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$sq->execute(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function testIsResultSet() |
68
|
|
|
{ |
69
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
70
|
|
|
$this->assertInstanceOf(ResultSetInterface::class, $res); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function testStore() |
74
|
|
|
{ |
75
|
|
|
$this->refill(); |
76
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
77
|
|
|
$res->store()->store(); |
78
|
|
|
$this->assertCount(8, $res->fetchAllNum()); |
79
|
|
|
|
80
|
|
|
$res = self::$conn->query('UPDATE rt SET gid = 202 WHERE gid < 202'); |
81
|
|
|
$this->assertEquals(2, $res->store()->getStored()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testHasRow() |
85
|
|
|
{ |
86
|
|
|
$this->refill(); |
87
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
88
|
|
|
$this->assertTrue($res->hasRow(2)); |
89
|
|
|
$this->assertTrue(isset($res[2])); |
90
|
|
|
$this->assertFalse($res->hasRow(1000)); |
91
|
|
|
$this->assertFalse(isset($res[1000])); |
92
|
|
|
$res->freeResult(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testToRow() |
96
|
|
|
{ |
97
|
|
|
$this->refill(); |
98
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
99
|
|
|
$res->toRow(2); |
100
|
|
|
$row = $res->fetchAssoc(); |
101
|
|
|
$this->assertEquals(12, $row['id']); |
102
|
|
|
$res->freeResult(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @expectedException Foolz\SphinxQL\Exception\ResultSetException |
107
|
|
|
* @expectedExceptionMessage The row does not exist. |
108
|
|
|
*/ |
109
|
|
|
public function testToRowThrows() |
110
|
|
|
{ |
111
|
|
|
$this->refill(); |
112
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
113
|
|
|
$res->toRow(8); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testHasNextRow() |
117
|
|
|
{ |
118
|
|
|
$this->refill(); |
119
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
120
|
|
|
$this->assertTrue($res->hasNextRow()); |
121
|
|
|
$res->freeResult(); |
122
|
|
|
$res = self::$conn->query('SELECT * FROM rt WHERE id = 9000'); |
123
|
|
|
$this->assertFalse($res->hasNextRow()); |
124
|
|
|
$res->freeResult(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testToNextRow() |
128
|
|
|
{ |
129
|
|
|
$this->refill(); |
130
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
131
|
|
|
$res->toNextRow()->toNextRow()->toNextRow(); |
132
|
|
|
$row = $res->fetchAssoc(); |
133
|
|
|
$this->assertEquals(13, $row['id']); |
134
|
|
|
$res->freeResult(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException Foolz\SphinxQL\Exception\ResultSetException |
139
|
|
|
* @expectedExceptionMessage The row does not exist. |
140
|
|
|
*/ |
141
|
|
|
public function testToNextRowThrows() |
142
|
|
|
{ |
143
|
|
|
$this->refill(); |
144
|
|
|
$res = self::$conn->query('SELECT * FROM rt WHERE id = 10'); |
145
|
|
|
$res->toNextRow()->toNextRow(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testCount() |
149
|
|
|
{ |
150
|
|
|
$this->refill(); |
151
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
152
|
|
|
$this->assertEquals(8, $res->count()); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testFetchAllAssoc() |
156
|
|
|
{ |
157
|
|
|
$expect = array( |
158
|
|
|
0 => array( |
159
|
|
|
'id' => '10', |
160
|
|
|
'gid' => '9003' |
161
|
|
|
), |
162
|
|
|
1 => array( |
163
|
|
|
'id' => '11', |
164
|
|
|
'gid' => '201' |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
|
169
|
|
|
$this->refill(); |
170
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
171
|
|
|
$array = $res->fetchAllAssoc(); |
172
|
|
|
$this->assertSame($expect[0], $array[0]); |
173
|
|
|
$this->assertSame($expect[1], $array[1]); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testFetchAssoc() |
177
|
|
|
{ |
178
|
|
|
$expect = array( |
179
|
|
|
0 => array( |
180
|
|
|
'id' => '10', |
181
|
|
|
'gid' => '9003' |
182
|
|
|
), |
183
|
|
|
1 => array( |
184
|
|
|
'id' => '11', |
185
|
|
|
'gid' => '201' |
186
|
|
|
) |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
$this->refill(); |
191
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
192
|
|
|
$this->assertSame($expect[0], $res->fetchAssoc()); |
193
|
|
|
$this->assertSame($expect[1], $res->fetchAssoc()); |
194
|
|
|
$res->fetchAssoc(); |
195
|
|
|
$res->fetchAssoc(); |
196
|
|
|
$res->fetchAssoc(); |
197
|
|
|
$res->fetchAssoc(); |
198
|
|
|
$res->fetchAssoc(); |
199
|
|
|
$res->fetchAssoc(); |
200
|
|
|
$this->assertNull($res->fetchAssoc()); |
201
|
|
|
|
202
|
|
|
$res = self::$conn->query('SELECT * FROM rt')->store(); |
203
|
|
|
$this->assertSame($expect[0], $res->fetchAssoc()); |
204
|
|
|
$this->assertSame($expect[1], $res->fetchAssoc()); |
205
|
|
|
$res->fetchAssoc(); |
206
|
|
|
$res->fetchAssoc(); |
207
|
|
|
$res->fetchAssoc(); |
208
|
|
|
$res->fetchAssoc(); |
209
|
|
|
$res->fetchAssoc(); |
210
|
|
|
$res->fetchAssoc(); |
211
|
|
|
$this->assertNull($res->fetchAssoc()); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testFetchAllNum() |
215
|
|
|
{ |
216
|
|
|
$expect = array( |
217
|
|
|
0 => array( |
218
|
|
|
0 => '10', |
219
|
|
|
1 => '9003' |
220
|
|
|
), |
221
|
|
|
1 => array( |
222
|
|
|
0 => '11', |
223
|
|
|
1 => '201' |
224
|
|
|
) |
225
|
|
|
); |
226
|
|
|
|
227
|
|
|
$this->refill(); |
228
|
|
|
$res = self::$conn->query('SELECT * FROM rt LIMIT 2'); |
229
|
|
|
$array = $res->fetchAllNum(); |
230
|
|
|
$this->assertSame($expect, $array); |
231
|
|
|
|
232
|
|
|
$res = self::$conn->query('SELECT * FROM rt LIMIT 2'); |
233
|
|
|
$array = $res->store()->fetchAllNum(); |
234
|
|
|
$this->assertSame($expect, $array); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function testFetchNum() |
238
|
|
|
{ |
239
|
|
|
$expect = array( |
240
|
|
|
0 => array( |
241
|
|
|
0 => '10', |
242
|
|
|
1 => '9003' |
243
|
|
|
), |
244
|
|
|
1 => array( |
245
|
|
|
0 => '11', |
246
|
|
|
1 => '201' |
247
|
|
|
) |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
$this->refill(); |
251
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
252
|
|
|
$this->assertSame($expect[0], $res->fetchNum()); |
253
|
|
|
$this->assertSame($expect[1], $res->fetchNum()); |
254
|
|
|
$res->fetchNum(); |
255
|
|
|
$res->fetchNum(); |
256
|
|
|
$res->fetchNum(); |
257
|
|
|
$res->fetchNum(); |
258
|
|
|
$res->fetchNum(); |
259
|
|
|
$res->fetchNum(); |
260
|
|
|
$this->assertNull($res->fetchNum()); |
261
|
|
|
|
262
|
|
|
$res = self::$conn->query('SELECT * FROM rt')->store(); |
263
|
|
|
$this->assertSame($expect[0], $res->fetchNum()); |
264
|
|
|
$this->assertSame($expect[1], $res->fetchNum()); |
265
|
|
|
$res->fetchNum(); |
266
|
|
|
$res->fetchNum(); |
267
|
|
|
$res->fetchNum(); |
268
|
|
|
$res->fetchNum(); |
269
|
|
|
$res->fetchNum(); |
270
|
|
|
$res->fetchNum(); |
271
|
|
|
$this->assertNull($res->fetchNum()); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function testGetAffectedRows() |
275
|
|
|
{ |
276
|
|
|
$this->refill(); |
277
|
|
|
$res = self::$conn->query('UPDATE rt SET gid=0 WHERE id > 0'); |
278
|
|
|
$this->assertSame(8, $res->getAffectedRows()); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function testArrayAccess() |
282
|
|
|
{ |
283
|
|
|
$expect = array( |
284
|
|
|
0 => array( |
285
|
|
|
'id' => '10', |
286
|
|
|
'gid' => '9003' |
287
|
|
|
), |
288
|
|
|
1 => array( |
289
|
|
|
'id' => '11', |
290
|
|
|
'gid' => '201' |
291
|
|
|
) |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
|
295
|
|
|
$this->refill(); |
296
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
297
|
|
|
$this->assertSame($expect[0], $res[0]); |
298
|
|
|
$this->assertSame($expect[1], $res[1]); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function testCountable() |
302
|
|
|
{ |
303
|
|
|
$this->refill(); |
304
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
305
|
|
|
$this->assertEquals($res->count(), count($res)); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function testIterator() |
309
|
|
|
{ |
310
|
|
|
$expect = array( |
311
|
|
|
0 => array( |
312
|
|
|
'id' => '10', |
313
|
|
|
'gid' => '9003' |
314
|
|
|
), |
315
|
|
|
1 => array( |
316
|
|
|
'id' => '11', |
317
|
|
|
'gid' => '201' |
318
|
|
|
) |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
$this->refill(); |
322
|
|
|
$res = self::$conn->query('SELECT * FROM rt'); |
323
|
|
|
$array = array(); |
324
|
|
|
foreach ($res as $key => $value) { |
325
|
|
|
$array[$key] = $value; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$this->assertSame($expect[0], $array[0]); |
329
|
|
|
$this->assertSame($expect[1], $array[1]); |
330
|
|
|
|
331
|
|
|
$res = self::$conn->query('SELECT * FROM rt WHERE id = 404'); |
332
|
|
|
$array = array(); |
333
|
|
|
foreach ($res as $key => $value) { |
334
|
|
|
$array[$key] = $value; |
335
|
|
|
} |
336
|
|
|
$this->assertEmpty($array); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.