-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathQueryBuilderTest.php
578 lines (482 loc) · 15.9 KB
/
QueryBuilderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use Illuminate\Database\Query\Builder;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Facades\DB;
use MongoDB\BSON\Regex;
use MongoDB\Laravel\Collection;
use MongoDB\Laravel\Tests\TestCase;
use function file_get_contents;
use function json_decode;
class QueryBuilderTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$db = DB::connection('mongodb');
$db->collection('movies')
->insert(json_decode(file_get_contents(__DIR__ . '/sample_mflix.movies.json'), true));
}
protected function importTheaters(): void
{
$db = DB::connection('mongodb');
$db->collection('theaters')
->insert(json_decode(file_get_contents(__DIR__ . '/sample_mflix.theaters.json'), true));
$db->collection('theaters')
->raw()
->createIndex(['location.geo' => '2dsphere']);
}
protected function tearDown(): void
{
$db = DB::connection('mongodb');
$db->collection('movies')->raw()->drop();
$db->collection('theaters')->raw()->drop();
parent::tearDown();
}
public function testWhere(): void
{
// begin query where
$result = DB::connection('mongodb')
->collection('movies')
->where('imdb.rating', 9.3)
->get();
// end query where
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testOrWhere(): void
{
// begin query orWhere
$result = DB::connection('mongodb')
->collection('movies')
->where('year', 1955)
->orWhere('title', 'Back to the Future')
->get();
// end query orWhere
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testAndWhere(): void
{
// begin query andWhere
$result = DB::connection('mongodb')
->collection('movies')
->where('imdb.rating', '>', 8.5)
->where('year', '<', 1940)
->get();
// end query andWhere
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereNot(): void
{
// begin query whereNot
$result = DB::connection('mongodb')
->collection('movies')
->whereNot('imdb.rating', '>', 2)
->get();
// end query whereNot
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testNestedLogical(): void
{
// begin query nestedLogical
$result = DB::connection('mongodb')
->collection('movies')
->where('imdb.rating', '>', 8.5)
->where(function (Builder $query) {
return $query
->where('year', 1986)
->orWhere('year', 1996);
})->get();
// end query nestedLogical
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereBetween(): void
{
// begin query whereBetween
$result = DB::connection('mongodb')
->collection('movies')
->whereBetween('imdb.rating', [9, 9.5])
->get();
// end query whereBetween
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereNull(): void
{
// begin query whereNull
$result = DB::connection('mongodb')
->collection('movies')
->whereNull('runtime')
->get();
// end query whereNull
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereIn(): void
{
// begin query whereIn
$result = DB::collection('movies')
->whereIn('title', ['Toy Story', 'Shrek 2', 'Johnny English'])
->get();
// end query whereIn
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereDate(): void
{
// begin query whereDate
$result = DB::connection('mongodb')
->collection('movies')
->whereDate('released', '2010-1-15')
->get();
// end query whereDate
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testLike(): void
{
// begin query like
$result = DB::collection('movies')
->where('title', 'like', '%spider_man%')
->get();
// end query like
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testDistinct(): void
{
// begin query distinct
$result = DB::collection('movies')
->distinct('year')->get();
// end query distinct
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testGroupBy(): void
{
// begin query groupBy
$result = DB::collection('movies')
->where('rated', 'G')
->groupBy('runtime')
->orderBy('runtime', 'asc')
->get(['title']);
// end query groupBy
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testAggCount(): void
{
// begin aggregation count
$result = DB::collection('movies')
->count();
// end aggregation count
$this->assertIsInt($result);
}
public function testAggMax(): void
{
// begin aggregation max
$result = DB::collection('movies')
->max('runtime');
// end aggregation max
$this->assertIsInt($result);
}
public function testAggMin(): void
{
// begin aggregation min
$result = DB::collection('movies')
->min('year');
// end aggregation min
$this->assertIsInt($result);
}
public function testAggAvg(): void
{
// begin aggregation avg
$result = DB::collection('movies')
->avg('imdb.rating');
// end aggregation avg
$this->assertIsFloat($result);
}
public function testAggSum(): void
{
// begin aggregation sum
$result = DB::collection('movies')
->sum('imdb.votes');
// end aggregation sum
$this->assertIsInt($result);
}
public function testAggWithFilter(): void
{
// begin aggregation with filter
$result = DB::collection('movies')
->where('year', '>', 2000)
->avg('imdb.rating');
// end aggregation with filter
$this->assertIsFloat($result);
}
public function testOrderBy(): void
{
// begin query orderBy
$result = DB::collection('movies')
->where('title', 'like', 'back to the future%')
->orderBy('imdb.rating', 'desc')
->get();
// end query orderBy
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testSkip(): void
{
// begin query skip
$result = DB::collection('movies')
->where('title', 'like', 'star trek%')
->orderBy('year', 'asc')
->skip(4)
->get();
// end query skip
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testProjection(): void
{
// begin query projection
$result = DB::collection('movies')
->where('imdb.rating', '>', 8.5)
->project([
'title' => 1,
'cast' => ['$slice' => [1, 3]],
])
->get();
// end query projection
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testProjectionWithPagination(): void
{
// begin query projection with pagination
$resultsPerPage = 15;
$projectionFields = ['title', 'runtime', 'imdb.rating'];
$result = DB::collection('movies')
->orderBy('imdb.votes', 'desc')
->paginate($resultsPerPage, $projectionFields);
// end query projection with pagination
$this->assertInstanceOf(AbstractPaginator::class, $result);
}
public function testExists(): void
{
// begin query exists
$result = DB::collection('movies')
->exists('random_review', true);
// end query exists
$this->assertIsBool($result);
}
public function testAll(): void
{
// begin query all
$result = DB::collection('movies')
->where('movies', 'all', ['title', 'rated', 'imdb.rating'])
->get();
// end query all
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testSize(): void
{
// begin query size
$result = DB::collection('movies')
->where('directors', 'size', 5)
->get();
// end query size
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testType(): void
{
// begin query type
$result = DB::collection('movies')
->where('released', 'type', 4)
->get();
// end query type
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testMod(): void
{
// begin query modulo
$result = DB::collection('movies')
->where('year', 'mod', [2, 0])
->get();
// end query modulo
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereRegex(): void
{
// begin query whereRegex
$result = DB::connection('mongodb')
->collection('movies')
->where('title', 'REGEX', new Regex('^the lord of .*', 'i'))
->get();
// end query whereRegex
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testWhereRaw(): void
{
// begin query raw
$result = DB::collection('movies')
->whereRaw([
'imdb.votes' => ['$gte' => 1000 ],
'$or' => [
['imdb.rating' => ['$gt' => 7]],
['directors' => ['$in' => [ 'Yasujiro Ozu', 'Sofia Coppola', 'Federico Fellini' ]]],
],
])->get();
// end query raw
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testElemMatch(): void
{
// begin query elemMatch
$result = DB::collection('movies')
->where('writers', 'elemMatch', ['$in' => ['Maya Forbes', 'Eric Roth']])
->get();
// end query elemMatch
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testCursorTimeout(): void
{
// begin query cursor timeout
$result = DB::collection('movies')
->timeout(2) // value in seconds
->where('year', 2001)
->get();
// end query cursor timeout
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $result);
}
public function testNear(): void
{
$this->importTheaters();
// begin query near
$results = DB::collection('theaters')
->where('location.geo', 'near', [
'$geometry' => [
'type' => 'Point',
'coordinates' => [
-86.6423,
33.6054,
],
],
'$maxDistance' => 50,
])->get();
// end query near
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $results);
}
public function testGeoWithin(): void
{
// begin query geoWithin
$results = DB::collection('theaters')
->where('location.geo', 'geoWithin', [
'$geometry' => [
'type' => 'Polygon',
'coordinates' => [
[
[-72, 40],
[-74, 41],
[-72, 39],
[-72, 40],
],
],
],
])->get();
// end query geoWithin
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $results);
}
public function testGeoIntersects(): void
{
// begin query geoIntersects
$results = DB::collection('theaters')
->where('location.geo', 'geoIntersects', [
'$geometry' => [
'type' => 'LineString',
'coordinates' => [
[-73.600525, 40.74416],
[-72.600525, 40.74416],
],
],
])->get();
// end query geoIntersects
$this->assertInstanceOf(\Illuminate\Support\Collection::class, $results);
}
public function testGeoNear(): void
{
$this->importTheaters();
// begin query geoNear
$results = DB::collection('theaters')->raw(
function (Collection $collection) {
return $collection->aggregate([
[
'$geoNear' => [
'near' => [
'type' => 'Point',
'coordinates' => [-118.34, 34.10],
],
'distanceField' => 'dist.calculated',
'maxDistance' => 500,
'includeLocs' => 'dist.location',
'spherical' => true,
],
],
]);
},
)->toArray();
// end query geoNear
$this->assertIsArray($results);
$this->assertSame(8900, $results[0]['theaterId']);
}
public function testUpsert(): void
{
// begin upsert
$result = DB::collection('movies')
->where('title', 'Will Hunting')
->update(
[
'plot' => 'An autobiographical movie',
'year' => 1998,
'writers' => [ 'Will Hunting' ],
],
['upsert' => true],
);
// end upsert
$this->assertIsInt($result);
}
public function testIncrement(): void
{
// begin increment
$result = DB::collection('movies')
->where('title', 'Field of Dreams')
->increment('imdb.votes', 3000);
// end increment
$this->assertIsInt($result);
}
public function testDecrement(): void
{
// begin decrement
$result = DB::collection('movies')
->where('title', 'Sharknado')
->decrement('imdb.rating', 0.2);
// end decrement
$this->assertIsInt($result);
}
public function testPush(): void
{
// begin push
$result = DB::collection('movies')
->where('title', 'Office Space')
->push('cast', 'Gary Cole');
// end push
$this->assertIsInt($result);
}
public function testPull(): void
{
// begin pull
$result = DB::collection('movies')
->where('title', 'Iron Man')
->pull('genres', 'Adventure');
// end pull
$this->assertIsInt($result);
}
public function testUnset(): void
{
// begin unset
$result = DB::collection('movies')
->where('title', 'Final Accord')
->unset('tomatoes.viewer');
// end unset
$this->assertIsInt($result);
}
}