1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SearchTests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use CommonTestClass; |
7
|
|
|
use kalanis\kw_mapper\MapperException; |
8
|
|
|
use kalanis\kw_mapper\Search\Connector\Records; |
9
|
|
|
use kalanis\kw_mapper\Search\Search; |
10
|
|
|
use kalanis\kw_mapper\Storage; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class FileTableTest extends CommonTestClass |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @throws MapperException |
17
|
|
|
*/ |
18
|
|
|
public function testSimpleSearch(): void |
19
|
|
|
{ |
20
|
|
|
$record = new \TableIdRecord(); |
21
|
|
|
$record->useIdAsMapper(); |
22
|
|
|
$lib = new Search($record); |
23
|
|
|
$lib->orderBy('title'); |
24
|
|
|
$this->assertEquals(5, $lib->getCount()); |
25
|
|
|
$lib->offset(1); |
26
|
|
|
$lib->limit(3); |
27
|
|
|
$lib->useAnd(); // just call |
28
|
|
|
$lib->useOr(); // just call |
29
|
|
|
$this->assertEquals(5, $lib->getCount()); |
30
|
|
|
$this->assertEquals(3, count($lib->getResults())); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws MapperException |
35
|
|
|
*/ |
36
|
|
|
public function testSearchFailPropertyDefine(): void |
37
|
|
|
{ |
38
|
|
|
$record = new \TableIdRecord(); |
39
|
|
|
$record->useIdAsMapper(); |
40
|
|
|
|
41
|
|
|
$lib = new Search($record); |
42
|
|
|
$this->expectException(MapperException::class); |
43
|
|
|
$lib->like('.file', 'mm'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws MapperException |
48
|
|
|
*/ |
49
|
|
|
public function testSearchLike(): void |
50
|
|
|
{ |
51
|
|
|
$record = new \TableIdRecord(); |
52
|
|
|
$record->useIdAsMapper(); |
53
|
|
|
|
54
|
|
|
$lib = new Search($record); |
55
|
|
|
$lib->like('file', 'dummy'); |
56
|
|
|
$this->assertEquals(4, $lib->getCount()); |
57
|
|
|
$this->assertNotEmpty($lib->getResults()); |
58
|
|
|
|
59
|
|
|
$lib = new Search($record); |
60
|
|
|
$lib->like('file.file', 'mm'); |
61
|
|
|
$this->assertEquals(4, $lib->getCount()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws MapperException |
66
|
|
|
*/ |
67
|
|
|
public function testSearchNotLike(): void |
68
|
|
|
{ |
69
|
|
|
$record = new \TableIdRecord(); |
70
|
|
|
$record->useIdAsMapper(); |
71
|
|
|
|
72
|
|
|
$lib = new Search($record); |
73
|
|
|
$lib->notLike('file.file', 'now'); |
74
|
|
|
$this->assertEquals(4, $lib->getCount()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws MapperException |
79
|
|
|
*/ |
80
|
|
|
public function testSearchExact(): void |
81
|
|
|
{ |
82
|
|
|
$record = new \TableIdRecord(); |
83
|
|
|
$record->useIdAsMapper(); |
84
|
|
|
$lib = new Search($record); |
85
|
|
|
$lib->exact('enabled', true); |
86
|
|
|
$this->assertEquals(3, $lib->getCount()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @throws MapperException |
91
|
|
|
*/ |
92
|
|
|
public function testSearchNotExact(): void |
93
|
|
|
{ |
94
|
|
|
$record = new \TableIdRecord(); |
95
|
|
|
$record->useIdAsMapper(); |
96
|
|
|
$lib = new Search($record); |
97
|
|
|
$lib->notExact('enabled', true); |
98
|
|
|
$this->assertEquals(2, $lib->getCount()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @throws MapperException |
103
|
|
|
*/ |
104
|
|
|
public function testSearchFrom(): void |
105
|
|
|
{ |
106
|
|
|
$record = new \TableIdRecord(); |
107
|
|
|
$record->useIdAsMapper(); |
108
|
|
|
$lib = new Search($record); |
109
|
|
|
$lib->from('id', 2, true); |
110
|
|
|
$this->assertEquals(4, $lib->getCount()); |
111
|
|
|
|
112
|
|
|
$lib = new Search($record); |
113
|
|
|
$lib->from('id', 2, false); |
114
|
|
|
$this->assertEquals(3, $lib->getCount()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @throws MapperException |
119
|
|
|
*/ |
120
|
|
|
public function testSearchTo(): void |
121
|
|
|
{ |
122
|
|
|
$record = new \TableIdRecord(); |
123
|
|
|
$record->useIdAsMapper(); |
124
|
|
|
$lib = new Search($record); |
125
|
|
|
$lib->to('id', 3, false); |
126
|
|
|
$this->assertEquals(2, $lib->getCount()); |
127
|
|
|
|
128
|
|
|
$lib = new Search($record); |
129
|
|
|
$lib->to('id', 3, true); |
130
|
|
|
$this->assertEquals(3, $lib->getCount()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @throws MapperException |
135
|
|
|
*/ |
136
|
|
|
public function testRegex(): void |
137
|
|
|
{ |
138
|
|
|
$record = new \TableIdRecord(); |
139
|
|
|
$record->useIdAsMapper(); |
140
|
|
|
$lib = new Search($record); |
141
|
|
|
$lib->regexp('file', '#dummy(\d+)#'); |
142
|
|
|
$this->assertEquals(4, $lib->getCount()); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @throws MapperException |
147
|
|
|
*/ |
148
|
|
|
public function testSearchBetween(): void |
149
|
|
|
{ |
150
|
|
|
$record = new \TableIdRecord(); |
151
|
|
|
$record->useIdAsMapper(); |
152
|
|
|
$lib = new Search($record); |
153
|
|
|
$lib->between('id', 2, 5); |
154
|
|
|
$this->assertEquals(4, $lib->getCount()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @throws MapperException |
159
|
|
|
*/ |
160
|
|
|
public function testSearchNull(): void |
161
|
|
|
{ |
162
|
|
|
$record = new \TableIdRecord(); |
163
|
|
|
$record->useIdAsMapper(); |
164
|
|
|
$lib = new Search($record); |
165
|
|
|
$lib->null('title'); |
166
|
|
|
$this->assertEquals(0, $lib->getCount()); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @throws MapperException |
171
|
|
|
*/ |
172
|
|
|
public function testSearchNotNull(): void |
173
|
|
|
{ |
174
|
|
|
$record = new \TableIdRecord(); |
175
|
|
|
$record->useIdAsMapper(); |
176
|
|
|
$lib = new Search($record); |
177
|
|
|
$lib->notNull('title'); |
178
|
|
|
$this->assertEquals(5, $lib->getCount()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @throws MapperException |
183
|
|
|
*/ |
184
|
|
|
public function testSearchIn(): void |
185
|
|
|
{ |
186
|
|
|
$record = new \TableIdRecord(); |
187
|
|
|
$record->useIdAsMapper(); |
188
|
|
|
$lib = new Search($record); |
189
|
|
|
$lib->in('desc', ['jkl', 'pqr', 'z12']); |
190
|
|
|
$this->assertEquals(2, $lib->getCount()); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @throws MapperException |
195
|
|
|
*/ |
196
|
|
|
public function testSearchNotIn(): void |
197
|
|
|
{ |
198
|
|
|
$record = new \TableIdRecord(); |
199
|
|
|
$record->useIdAsMapper(); |
200
|
|
|
$lib = new Search($record); |
201
|
|
|
$lib->notIn('desc', ['jkl', 'pqr', 'z12']); |
202
|
|
|
$this->assertEquals(3, $lib->getCount()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @throws MapperException |
207
|
|
|
*/ |
208
|
|
|
public function testSearchGrouping(): void |
209
|
|
|
{ |
210
|
|
|
$record = new \TableIdRecord(); |
211
|
|
|
$record->useIdAsMapper(); |
212
|
|
|
$lib = new Search($record); |
213
|
|
|
$lib->groupBy('enabled'); |
214
|
|
|
$this->assertEquals(2, $lib->getCount()); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @throws MapperException |
219
|
|
|
*/ |
220
|
|
|
public function testSearchChild(): void |
221
|
|
|
{ |
222
|
|
|
$record = new \TableIdRecord(); |
223
|
|
|
$record->useIdAsMapper(); |
224
|
|
|
$lib = new Search($record); |
225
|
|
|
$this->expectException(MapperException::class); |
226
|
|
|
$lib->child('any'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @throws MapperException |
231
|
|
|
*/ |
232
|
|
|
public function testSearchUnknownChild(): void |
233
|
|
|
{ |
234
|
|
|
$record = new \TableIdRecord(); |
235
|
|
|
$record->useIdAsMapper(); |
236
|
|
|
$lib = new Search($record); |
237
|
|
|
$this->expectException(MapperException::class); |
238
|
|
|
$lib->childNotExist('any', 'where'); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @throws MapperException |
243
|
|
|
*/ |
244
|
|
|
public function testSearchInitial(): void |
245
|
|
|
{ |
246
|
|
|
$record = new \TableIdRecord(); |
247
|
|
|
$record->useIdAsMapper(); |
248
|
|
|
$lib = new Records($record); |
249
|
|
|
$this->assertEquals(0, $lib->getCount()); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @throws MapperException |
254
|
|
|
*/ |
255
|
|
|
public function testSearchChildRecord(): void |
256
|
|
|
{ |
257
|
|
|
$record = new \TableIdRecord(); |
258
|
|
|
$record->useNoKeyMapper(); |
259
|
|
|
$lib = new Records($record); |
260
|
|
|
$this->expectException(MapperException::class); |
261
|
|
|
$lib->child('any'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @throws MapperException |
266
|
|
|
*/ |
267
|
|
|
public function testSearchUnknownChildRecord(): void |
268
|
|
|
{ |
269
|
|
|
$record = new \TableIdRecord(); |
270
|
|
|
$record->useNoKeyMapper(); |
271
|
|
|
$lib = new Records($record); |
272
|
|
|
$this->expectException(MapperException::class); |
273
|
|
|
$lib->childNotExist('any', 'where', 'for'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @throws MapperException |
278
|
|
|
*/ |
279
|
|
|
public function testSearchShittyConditionRecord(): void |
280
|
|
|
{ |
281
|
|
|
$record1 = new \XSimpleRecord(); |
282
|
|
|
$record1->useMock(); |
283
|
|
|
$record1->id = 1; |
284
|
|
|
$record1->title = 'abc'; |
285
|
|
|
|
286
|
|
|
$record2 = new \XSimpleRecord(); |
287
|
|
|
$record2->useMock(); |
288
|
|
|
$record2->id = 2; |
289
|
|
|
$record2->title = 'def'; |
290
|
|
|
|
291
|
|
|
$record3 = new \XSimpleRecord(); |
292
|
|
|
$record3->useMock(); |
293
|
|
|
$record3->id = 3; |
294
|
|
|
$record3->title = 'ghi'; |
295
|
|
|
|
296
|
|
|
$record4 = new \XSimpleRecord(); |
297
|
|
|
$record4->useMock(); |
298
|
|
|
$record4->id = 4; |
299
|
|
|
$record4->title = 'jkl'; |
300
|
|
|
|
301
|
|
|
$lib = new XRecords($record1); |
302
|
|
|
$lib->setInitialRecords([$record1, $record2, $record3, $record4]); |
303
|
|
|
$this->expectException(MapperException::class); |
304
|
|
|
$lib->checkConditionExt('gh....', ':file__', 'something'); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
|
309
|
|
|
class XRecords extends Records |
310
|
|
|
{ |
311
|
|
|
public function checkConditionExt(string $operation, $value, $expected): bool |
312
|
|
|
{ |
313
|
|
|
return $this->checkCondition($operation, $value, $expected); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|