-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
MockCollection.php
719 lines (620 loc) · 22.7 KB
/
MockCollection.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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
<?php
namespace Helmich\MongoMock;
use ArrayIterator;
use Helmich\MongoMock\Log\Index;
use Helmich\MongoMock\Log\Query;
use MongoDB\BSON;
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\Regex;
use MongoDB\Collection;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Model\IndexInfoIteratorIterator;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Operation\FindOneAndUpdate;
use PHPUnit\Framework\Constraint\Constraint;
/**
* A mocked MongoDB collection
*
* This class mimicks the behaviour of a MongoDB collection (and also extends
* the actual `MongoDB\Collection` class and can be used as a drop-in
* replacement). All operations are performed in-memory and are not persisted.
*
* NOTE: This class is not complete! Many methods are missing and I will only
* implement them as soon as I need them. Feel free to open an issue or (better)
* a pull request if you need something.
*
* @package Helmich\MongoMock
*/
class MockCollection extends Collection
{
const TYPE_BSON = [
5 => BSON\Binary::class,
128 => BSON\Decimal128::class,
13 => BSON\JavaScript::class,
127 => BSON\MaxKey::class,
-1 => BSON\MinKey::class,
7 => BSON\ObjectId::class,
11 => BSON\Regex::class,
17 => BSON\Timestamp::class,
9 => BSON\UTCDateTime::class
];
const TYPE = [
1 => 'double',
2 => 'string',
3 => 'object',
4 => 'array',
8 => 'boolean',
10 => 'NULL',
16 => 'integer',
18 => 'integer'
];
public $queries = [];
public $documents = [];
public $indices = [];
public $dropped = false;
/** @var string */
private $name;
/** @var MockDatabase|null */
private $db;
/** @var array */
private $options = [];
/** @var TypeMapper */
private $typeMapper;
/**
* @param string $name
* @param MockDatabase $db
*/
public function __construct(string $name = 'collection', MockDatabase $db = null, array $options = [])
{
$this->name = $name;
$this->db = $db;
$this->options = $options;
if ($db !== null) {
$this->options = array_merge($db->getOptions(), $options);
} else {
$this->options = $options;
}
$this->typeMapper = TypeMapper::createWithDefault($this->options['typeMap'] ?? []);
}
public function insertOne($document, array $options = [])
{
if (!isset($document['_id'])) {
$document['_id'] = new ObjectID();
} else {
// make sure document with the same id does not exist
foreach ($this->documents as $doc) {
// if document with the same id already exists
if ($doc['_id'] == $document['_id']) {
throw new DriverRuntimeException();
}
}
}
if (!$document instanceof BSONDocument) {
$document = new BSONDocument($document);
}
// Possible double instantiation of BSONDocument?
// With or without the below, it seems that the
// BSONDocument class recurses automatically.
// e.g. $document->bsonSerialize() will give the same result
// so I couldn't write a test to capture the need or not for this.
// I'm commenting it out anyway.
// $document = new BSONDocument($document);
$this->documents[] = $document;
return new MockInsertOneResult($document['_id']);
}
public function insertMany(array $documents, array $options = [])
{
$insertedIds = array_map(function ($doc) use ($options) {
return $this->insertOne($doc, $options)->getInsertedId();
}, $documents);
return new MockInsertManyResult($insertedIds);
}
public function deleteMany($filter, array $options = [])
{
$matcher = $this->matcherFromQuery($filter);
$count = 0;
foreach ($this->documents as $i => $doc) {
if ($matcher($doc)) {
unset($this->documents[$i]);
$count++;
}
}
$this->documents = array_values($this->documents);
return new MockDeleteResult($count, $count);
}
public function updateOne($filter, $update, array $options = [])
{
$matcher = $this->matcherFromQuery($filter);
foreach ($this->documents as $i => &$doc) {
if ($matcher($doc)) {
$this->updateCore($doc, $update);
return new MockUpdateResult(1, 1);
}
}
$result = $this->updateUpsert($filter, $update, $options, false);
$upserted = $result === null ? [] : $result->getInsertedIds();
return new MockUpdateResult(0, 0, $upserted);
}
public function updateMany($filter, $update, array $options = [])
{
$matcher = $this->matcherFromQuery($filter);
$matched = 0;
foreach ($this->documents as $i => &$doc) {
if (!$matcher($doc)) {
continue;
}
$this->updateCore($doc, $update);
$matched++;
}
$result = $this->updateUpsert($filter, $update, $options, $matched !== 0);
$upserted = $result === null ? [] : $result->getInsertedIds();
return new MockUpdateResult($matched, $matched, $upserted);
}
private function updateUpsert($filter, $update, $options, $anyUpdates)
{
if (array_key_exists('upsert', $options)) {
if ($options['upsert'] && !$anyUpdates) {
if (array_key_exists('$set', $update)) {
$documents = [array_merge($filter, $update['$set'])];
} else {
$documents = [$update];
}
$this->insertMany($documents, $options);
}
}
}
private function updateCore(&$doc, $update)
{
// The update operators are required, as exemplified here:
// http://mongodb.github.io/mongo-php-library/tutorial/crud/
$supported = ['$set', '$unset', '$inc', '$push'];
$unsupported = array_diff(array_keys($update), $supported);
if (count($unsupported) > 0) {
throw new Exception("Unsupported update operators found: " . implode(', ', $unsupported));
}
foreach ($update['$set'] ?? [] as $k => $v) {
$dot = strpos($k, ".");
if ($dot !== false) {
$tmp = &$doc;
$keys = explode(".", $k);
if ($keys !== null) {
foreach ($keys as $key) {
$tmp = &$tmp[$key];
}
$tmp = $v;
}
} else {
$doc[$k] = $v;
}
}
foreach ($update['$unset'] ?? [] as $k => $v) {
if (isset($doc[$k])) {
unset($doc[$k]);
}
}
foreach ($update['$inc'] ?? [] as $k => $v) {
if (isset($doc[$k]) && is_integer($v) && is_integer($doc[$k])) {
$doc[$k] += $v;
}
}
foreach ($update['$push'] ?? [] as $k => $v) {
if (isset($doc[$k]) && is_array($doc[$k])) {
$doc[$k][] = $v;
}
}
}
public function find($filter = [], array $options = []): MockCursor
{
$typeMapper = $this->typeMapper;
if (isset($options['typeMap'])) {
$typeMapper = $typeMapper->mergeWith(new TypeMapper($options['typeMap']));
}
// record query for future assertions
$this->queries[] = new Query($filter, $options);
$matcher = $this->matcherFromQuery($filter);
$skip = $options['skip'] ?? 0;
$collectionCopy = array_values($this->documents);
if (isset($options['sort'])) {
usort($collectionCopy, function ($a, $b) use ($options): int {
foreach ($options['sort'] as $key => $dir) {
$av = $a[$key];
$bv = $b[$key];
if (is_object($av)) {
$av = "" . $av;
}
if (is_object($bv)) {
$bv = "" . $bv;
}
if ($av > $bv) {
return $dir;
} else {
if ($av < $bv) {
return -$dir;
}
}
}
return 0;
});
}
if (isset($options['limit'])) {
$limit = $options['limit'] !== 0 ? $options['limit'] : null;
}
$cursor = [];
foreach ($collectionCopy as $doc) {
if ($matcher($doc)) {
if ($skip-- > 0) {
continue;
}
if (isset($limit)) {
if ($limit === 0) {
break;
}
$limit--;
}
$cursor[] = $typeMapper->map($doc);
}
}
return new MockCursor($cursor);
}
public function findOne($filter = [], array $options = [])
{
$results = $this->find($filter, $options);
foreach ($results as $result) {
return $result;
}
return null;
}
public function count($filter = [], array $options = [])
{
$count = 0;
$matcher = $this->matcherFromQuery($filter);
foreach ($this->documents as $i => $doc) {
if ($matcher($doc)) {
$count++;
}
}
return $count;
}
public function countDocuments($filter = [], array $options = [])
{
return $this->count($filter, $options);
}
public function createIndex($key, array $options = [])
{
$name = '';
if (is_string($key)) {
$name = $key . '_1';
} elseif (is_array($key)) {
foreach ($key as $field => $enabled) {
if (strlen($name) !== 0) {
$name .= '_';
}
$name .= $field . '_1';
}
}
$this->indices[$name] = new Index($key, $options);
}
public function drop(array $options = [])
{
$this->documents = [];
$this->dropped = true;
}
public function aggregate(array $pipeline, array $options = [])
{
// TODO: Implement this function
}
public function bulkWrite(array $operations, array $options = [])
{
// TODO: Implement this function
}
public function createIndexes(array $indexes, array $options = [])
{
foreach ($indexes as $index) {
$key = $index['key'];
unset($index['key']);
$this->createIndex($key, $index);
}
}
public function deleteOne($filter, array $options = [])
{
$matcher = $this->matcherFromQuery($filter);
foreach ($this->documents as $i => $doc) {
if ($matcher($doc)) {
unset($this->documents[$i]);
$this->documents = array_values($this->documents);
return;
}
}
}
public function distinct($fieldName, $filter = [], array $options = [])
{
$values = [];
$matcher = $this->matcherFromQuery($filter);
foreach ($this->documents as $document){
if ($matcher($document) && isset($document[$fieldName])) {
$values[] = $document[$fieldName];
}
}
return array_unique($values);
}
public function dropIndex($indexName, array $options = [])
{
// TODO: Implement this function
}
public function dropIndexes(array $options = [])
{
// TODO: Implement this function
}
public function findOneAndDelete($filter, array $options = [])
{
// TODO: Implement this function
}
public function findOneAndReplace($filter, $replacement, array $options = [])
{
// TODO: Implement this function
}
public function findOneAndUpdate($filter, $update, array $options = [])
{
if (!isset($options['returnDocument'])) {
// Standard behaviour according to https://docs.mongodb.com/php-library/v1.2/reference/method/MongoDBCollection-findOneAndUpdate/#definition
$options['returnDocument'] = FindOneAndUpdate::RETURN_DOCUMENT_BEFORE;
}
$returnDocument = null;
if ($options['returnDocument'] === FindOneAndUpdate::RETURN_DOCUMENT_BEFORE) {
$returnDocument = $this->findOne($filter, $options);
}
$this->updateOne($filter, $update, $options);
if ($options['returnDocument'] === FindOneAndUpdate::RETURN_DOCUMENT_AFTER) {
return $this->findOne($filter, $options);
} elseif ($returnDocument !== null) {
return $returnDocument;
}
throw new Exception('Given option value "' . $options['returnDocument'] . '" for findOneAndUpdate() "returnDocument" option is invalid');
}
public function getCollectionName()
{
return $this->name;
}
public function getDatabaseName()
{
if ($this->db === null) {
throw new Exception('database required to call getDatabaseName()');
} else {
return (string)$this->db;
}
}
public function getNamespace()
{
// TODO: Implement this function
}
public function listIndexes(array $options = [])
{
$indices = [];
$dbName = $this->db ? $this->db->getDatabaseName() : "unknown";
foreach ($this->indices as $name => $index) {
$indices[] = [
'v' => 1,
'unique' => isset($index->getOptions()['unique']) ? $index->getOptions()['unique'] : false,
'key' => $index->getKey(),
'name' => $name,
'ns' => $dbName . '.' . $this->name,
];
}
return new IndexInfoIteratorIterator(new ArrayIterator($indices));
}
public function replaceOne($filter, $replacement, array $options = [])
{
// TODO: Implement this function
}
public function withOptions(array $options = [])
{
// TODO: Implement this function
}
private function buildRecursiveMatcherQuery(array $query): array
{
$matchers = [];
foreach ($query as $field => $value) {
if ($field === '$and' || $field === '$or' || $field === '$nor' || is_numeric($field)) {
$matchers[$field] = $this->buildRecursiveMatcherQuery($value);
} else {
$matchers[$field] = $this->matcherFromConstraint($value);
}
}
return $matchers;
}
private function matcherFromQuery(array $query): callable
{
$matchers = $this->buildRecursiveMatcherQuery($query);
$orig_matchers = $matchers;
return $is_match = function ($doc, $compare = null) use (&$is_match, &$matchers, $orig_matchers): bool {
if ($compare === null) {
$matchers = $orig_matchers;
}
foreach ($matchers as $field => $matcher) {
if ($field === '$and') {
if (!is_array($matcher) || count($matcher) === 0) {
throw new Exception('$and expression must be a nonempty array');
}
foreach ($matcher as $sub) {
$matchers = $sub;
if (!$is_match($doc, $field)) {
return false;
}
}
return true;
} elseif ($field === '$or') {
if (!is_array($matcher) || count($matcher) === 0) {
throw new Exception('$or expression must be a nonempty array');
}
foreach ($matcher as $sub) {
$matchers = $sub;
if ($is_match($doc, $field)) {
return true;
}
}
return false;
} elseif ($field === '$nor') {
if (!is_array($matcher) || count($matcher) === 0) {
throw new Exception('$nor expression must be a nonempty array');
}
foreach ($matcher as $sub) {
$matchers = $sub;
if ($is_match($doc, $field)) {
return false;
}
}
return true;
} elseif ($field === '$isolated') {
return true;
} else {
// needed for case of $exists query filter and field is inexistant
$val = $this->getArrayValue($doc, $field);
if (!$matcher($val)) {
return false;
}
}
}
return true;
};
}
private function getArrayValue($array, string $path, string $separator = '.')
{
if (isset($array[$path])) {
return $array[$path];
}
$keys = explode($separator, $path);
foreach ($keys as $key) {
if (!isset($array[$key])) {
//needed for case of $exists query filter and field is inexistant
return null;
}
$array = $array[$key];
}
return $array;
}
private function matcherFromConstraint($constraint): callable
{
if (is_callable($constraint)) {
return $constraint;
}
if ($constraint instanceof Constraint) {
return function ($val) use ($constraint): bool {
return $constraint->evaluate($val, '', true);
};
}
if ($constraint instanceof ObjectID) {
return function ($val) use ($constraint): bool {
return ("" . $constraint) == ("" . $val);
};
}
if ($constraint instanceof Regex) {
return function ($val) use ($constraint): bool {
$pattern = str_replace('#', '\\#', $constraint->getPattern());
return preg_match('#' . $pattern . '#' . $constraint->getFlags(), $val);
};
}
if (is_array($constraint)) {
return $match = function ($val) use (&$constraint, &$match): bool {
$result = true;
foreach ($constraint as $type => $operand) {
switch ($type) {
// Mongo operators (subset)
case '$gt':
$result = ($val > $operand);
break;
case '$gte':
$result = ($val >= $operand);
break;
case '$lt':
$result = ($val < $operand);
break;
case '$lte':
$result = ($val <= $operand);
break;
case '$eq':
$result = ($val === $operand);
break;
case '$ne':
$result = ($val != $operand);
break;
case '$in':
$result = (!is_array($val))
? in_array($val, $operand)
: array_reduce(
$operand,
function ($acc, $op) use ($val) {
return ($acc || in_array($op, $val));
},
false
);
break;
case '$elemMatch':
if (is_array($val)) {
$matcher = $this->matcherFromQuery($operand);
foreach ($val as $v) {
$result = $matcher($v);
if ($result === true) {
break;
}
}
} else {
$constraint = $operand;
$result = $match($val);
}
break;
case '$exists':
$result = $val !== null;
break;
case '$size':
$result = count($val) === $operand;
break;
case '$type':
$result = $this->compareType($operand, $val);
break;
case '$not':
if (is_array($operand)) {
$matcher = $this->matcherFromConstraint($operand);
$result = !$matcher($val);
} else {
$result = !$operand;
}
break;
// Custom operators
case '$instanceOf':
$result = is_a($val, $operand);
break;
default:
throw new Exception("Constraint operator '" . $type . "' not yet implemented in MockCollection");
}
if (!$result) {
break;
}
}
return $result;
};
}
return function ($val) use ($constraint): bool {
if (is_string($constraint) && $constraint == '$exists') {
// note that for inexistant fields, val is overridden to be null
return !is_null($val);
}
if ($val instanceof Binary && is_string($constraint)) {
return $val->getData() == $constraint;
}
if (is_array($val) && is_scalar($constraint)) {
return in_array($constraint, $val, true);
}
return $val == $constraint;
};
}
protected function compareType(int $type, $value): bool
{
if ($value instanceof BSON\Type) {
return isset(self::TYPE_BSON[$type]) && is_a($value, self::TYPE_BSON[$type]);
} else {
return isset(self::TYPE[$type]) && gettype($value) === self::TYPE[$type];
}
}
}