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
<?php
namespace MongoDB\Tests\Collection;
use IteratorIterator;
use LogicException;
use MongoDB\BulkWriteResult;
use MongoDB\Collection;
use MongoDB\DeleteResult;
use MongoDB\Driver\Exception\BulkWriteException;
use MongoDB\Driver\Exception\RuntimeException;
use MongoDB\InsertManyResult;
use MongoDB\InsertOneResult;
use MongoDB\Operation\FindOneAndReplace;
use MongoDB\UpdateResult;
use MultipleIterator;
use PHPUnit_Framework_SkippedTestError;
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
use function array_diff_key;
use function array_key_exists;
use function array_map;
use function file_get_contents;
use function glob;
use function json_decode;
use function MongoDB\is_last_pipeline_operator_write;
use function sprintf;
use function str_replace;
use function strtolower;
use function version_compare;
/**
* CRUD spec functional tests.
*
* @see https://github.com/mongodb/specifications/tree/master/source/crud/tests
*/
class CrudSpecFunctionalTest extends FunctionalTestCase
{
use SetUpTearDownTrait;
/** @var Collection */
private $expectedCollection;
private function doSetUp()
{
parent::setUp();
$this->expectedCollection = new Collection($this->manager, $this->getDatabaseName(), $this->getCollectionName() . '.expected');
$this->expectedCollection->drop();
}
/**
* @dataProvider provideSpecificationTests
*/
public function testSpecification(array $initialData, array $test, $minServerVersion, $maxServerVersion)
{
if (isset($minServerVersion) || isset($maxServerVersion)) {
$this->checkServerVersion($minServerVersion, $maxServerVersion);
}
$expectedData = isset($test['outcome']['collection']['data']) ? $test['outcome']['collection']['data'] : null;
$this->initializeData($initialData, $expectedData);
if (isset($test['outcome']['collection']['name'])) {
$outputCollection = new Collection($this->manager, $this->getDatabaseName(), $test['outcome']['collection']['name']);
$outputCollection->drop();
}
$result = null;
$exception = null;
try {
$result = $this->executeOperation($test['operation']);
} catch (RuntimeException $e) {
$exception = $e;
}
$this->executeOutcome($test['operation'], $test['outcome'], $result, $exception);
}
public function provideSpecificationTests()
{
$testArgs = [];
foreach (glob(__DIR__ . '/spec-tests/*/*.json') as $filename) {
$json = json_decode(file_get_contents($filename), true);
$minServerVersion = isset($json['minServerVersion']) ? $json['minServerVersion'] : null;
$maxServerVersion = isset($json['maxServerVersion']) ? $json['maxServerVersion'] : null;
foreach ($json['tests'] as $test) {
$name = str_replace(' ', '_', $test['description']);
$testArgs[$name] = [$json['data'], $test, $minServerVersion, $maxServerVersion];
}
}
return $testArgs;
}
/**
* Assert that the collections contain equivalent documents.
*
* @param Collection $expectedCollection
* @param Collection $actualCollection
*/
private function assertEquivalentCollections($expectedCollection, $actualCollection)
{
$mi = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$mi->attachIterator(new IteratorIterator($expectedCollection->find()));
$mi->attachIterator(new IteratorIterator($actualCollection->find()));
foreach ($mi as $documents) {
list($expectedDocument, $actualDocument) = $documents;
$this->assertSameDocument($expectedDocument, $actualDocument);
}
}
/**
* Checks that the server version is within the allowed bounds (if any).
*
* @param string|null $minServerVersion
* @param string|null $maxServerVersion
* @throws PHPUnit_Framework_SkippedTestError
*/
private function checkServerVersion($minServerVersion, $maxServerVersion)
{
$serverVersion = $this->getServerVersion();
if (isset($minServerVersion) && version_compare($serverVersion, $minServerVersion, '<')) {
$this->markTestSkipped(sprintf('Server version "%s" < minServerVersion "%s"', $serverVersion, $minServerVersion));
}
if (isset($maxServerVersion) && version_compare($serverVersion, $maxServerVersion, '>=')) {
$this->markTestSkipped(sprintf('Server version "%s" >= maxServerVersion "%s"', $serverVersion, $maxServerVersion));
}
}
/**
* Executes an "operation" block.
*
* @param array $operation
* @return mixed
* @throws LogicException if the operation is unsupported
*/
private function executeOperation(array $operation)
{
switch ($operation['name']) {
case 'aggregate':
return $this->collection->aggregate(
$operation['arguments']['pipeline'],
array_diff_key($operation['arguments'], ['pipeline' => 1])
);
case 'bulkWrite':
return $this->collection->bulkWrite(
array_map([$this, 'prepareBulkWriteRequest'], $operation['arguments']['requests']),
isset($operation['arguments']['options']) ? $operation['arguments']['options'] : []
);
case 'count':
case 'countDocuments':
case 'find':
return $this->collection->{$operation['name']}(
isset($operation['arguments']['filter']) ? $operation['arguments']['filter'] : [],
array_diff_key($operation['arguments'], ['filter' => 1])
);
case 'estimatedDocumentCount':
return $this->collection->estimatedDocumentCount($operation['arguments']);
case 'deleteMany':
case 'deleteOne':
case 'findOneAndDelete':
return $this->collection->{$operation['name']}(
$operation['arguments']['filter'],
array_diff_key($operation['arguments'], ['filter' => 1])
);
case 'distinct':
return $this->collection->distinct(
$operation['arguments']['fieldName'],
isset($operation['arguments']['filter']) ? $operation['arguments']['filter'] : [],
array_diff_key($operation['arguments'], ['fieldName' => 1, 'filter' => 1])
);
case 'findOneAndReplace':
$operation['arguments'] = $this->prepareFindAndModifyArguments($operation['arguments']);
// Fall through
case 'replaceOne':
return $this->collection->{$operation['name']}(
$operation['arguments']['filter'],
$operation['arguments']['replacement'],
array_diff_key($operation['arguments'], ['filter' => 1, 'replacement' => 1])
);
case 'findOneAndUpdate':
$operation['arguments'] = $this->prepareFindAndModifyArguments($operation['arguments']);
// Fall through
case 'updateMany':
case 'updateOne':
return $this->collection->{$operation['name']}(
$operation['arguments']['filter'],
$operation['arguments']['update'],
array_diff_key($operation['arguments'], ['filter' => 1, 'update' => 1])
);
case 'insertMany':
return $this->collection->insertMany(
$operation['arguments']['documents'],
isset($operation['arguments']['options']) ? $operation['arguments']['options'] : []
);
case 'insertOne':
return $this->collection->insertOne(
$operation['arguments']['document'],
array_diff_key($operation['arguments'], ['document' => 1])
);
default:
throw new LogicException('Unsupported operation: ' . $operation['name']);
}
}
/**
* Executes an "outcome" block.
*
* @param array $operation
* @param array $outcome
* @param mixed $result
* @param RuntimeException $exception
* @return mixed
* @throws LogicException if the operation is unsupported
*/
private function executeOutcome(array $operation, array $outcome, $result, RuntimeException $exception = null)
{
$expectedError = array_key_exists('error', $outcome) ? $outcome['error'] : false;
if ($expectedError) {
$this->assertNull($result);
$this->assertNotNull($exception);
$result = $this->extractResultFromException($operation, $outcome, $exception);
}
if (array_key_exists('result', $outcome)) {
$this->executeAssertResult($operation, $outcome['result'], $result);
}
if (isset($outcome['collection'])) {
$actualCollection = isset($outcome['collection']['name'])
? new Collection($this->manager, $this->getDatabaseName(), $outcome['collection']['name'])
: $this->collection;
$this->assertEquivalentCollections($this->expectedCollection, $actualCollection);
}
}
/**
* Extracts a result from an exception.
*
* Errors for bulkWrite and insertMany operations may still report a write
* result. This method will attempt to extract such a result so that it can
* be used in executeAssertResult().
*
* If no result can be extracted, null will be returned.
*
* @param array $operation
* @param RuntimeException $exception
* @return mixed
*/
private function extractResultFromException(array $operation, array $outcome, RuntimeException $exception)
{
switch ($operation['name']) {
case 'bulkWrite':
$insertedIds = isset($outcome['result']['insertedIds']) ? $outcome['result']['insertedIds'] : [];
if ($exception instanceof BulkWriteException) {
return new BulkWriteResult($exception->getWriteResult(), $insertedIds);
}
break;
case 'insertMany':
$insertedIds = isset($outcome['result']['insertedIds']) ? $outcome['result']['insertedIds'] : [];
if ($exception instanceof BulkWriteException) {
return new InsertManyResult($exception->getWriteResult(), $insertedIds);
}
break;
}
return null;
}
/**
* Executes the "result" section of an "outcome" block.
*
* @param array $operation
* @param mixed $expectedResult
* @param mixed $actualResult
* @throws LogicException if the operation is unsupported
*/
private function executeAssertResult(array $operation, $expectedResult, $actualResult)
{
switch ($operation['name']) {
case 'aggregate':
/* Returning a cursor for the $out collection is optional per
* the CRUD specification and is not implemented in the library
* since we have no concept of lazy cursors. We will not assert
* the result here; however, assertEquivalentCollections() will
* assert the output collection's contents later.
*/
if (! is_last_pipeline_operator_write($operation['arguments']['pipeline'])) {
$this->assertSameDocuments($expectedResult, $actualResult);
}
break;
case 'bulkWrite':
$this->assertIsArray($expectedResult);
$this->assertInstanceOf(BulkWriteResult::class, $actualResult);
if (isset($expectedResult['deletedCount'])) {
$this->assertSame($expectedResult['deletedCount'], $actualResult->getDeletedCount());
}
if (isset($expectedResult['insertedCount'])) {
$this->assertSame($expectedResult['insertedCount'], $actualResult->getInsertedCount());
}
if (isset($expectedResult['insertedIds'])) {
$this->assertSameDocument(
['insertedIds' => $expectedResult['insertedIds']],
['insertedIds' => $actualResult->getInsertedIds()]
);
}
if (isset($expectedResult['matchedCount'])) {
$this->assertSame($expectedResult['matchedCount'], $actualResult->getMatchedCount());
}
if (isset($expectedResult['modifiedCount'])) {
$this->assertSame($expectedResult['modifiedCount'], $actualResult->getModifiedCount());
}
if (isset($expectedResult['upsertedCount'])) {
$this->assertSame($expectedResult['upsertedCount'], $actualResult->getUpsertedCount());
}
if (isset($expectedResult['upsertedIds'])) {
$this->assertSameDocument(
['upsertedIds' => $expectedResult['upsertedIds']],
['upsertedIds' => $actualResult->getUpsertedIds()]
);
}
break;
case 'count':
case 'countDocuments':
case 'estimatedDocumentCount':
$this->assertSame($expectedResult, $actualResult);
break;
case 'distinct':
$this->assertSameDocument(
['values' => $expectedResult],
['values' => $actualResult]
);
break;
case 'find':
$this->assertSameDocuments($expectedResult, $actualResult);
break;
case 'deleteMany':
case 'deleteOne':
$this->assertIsArray($expectedResult);
$this->assertInstanceOf(DeleteResult::class, $actualResult);
if (isset($expectedResult['deletedCount'])) {
$this->assertSame($expectedResult['deletedCount'], $actualResult->getDeletedCount());
}
break;
case 'findOneAndDelete':
case 'findOneAndReplace':
case 'findOneAndUpdate':
$this->assertSameDocument(
['result' => $expectedResult],
['result' => $actualResult]
);
break;
case 'insertMany':
$this->assertIsArray($expectedResult);
$this->assertInstanceOf(InsertManyResult::class, $actualResult);
if (isset($expectedResult['insertedCount'])) {
$this->assertSame($expectedResult['insertedCount'], $actualResult->getInsertedCount());
}
if (isset($expectedResult['insertedIds'])) {
$this->assertSameDocument(
['insertedIds' => $expectedResult['insertedIds']],
['insertedIds' => $actualResult->getInsertedIds()]
);
}
break;
case 'insertOne':
$this->assertIsArray($expectedResult);
$this->assertInstanceOf(InsertOneResult::class, $actualResult);
if (isset($expectedResult['insertedCount'])) {
$this->assertSame($expectedResult['insertedCount'], $actualResult->getInsertedCount());
}
if (isset($expectedResult['insertedId'])) {
$this->assertSameDocument(
['insertedId' => $expectedResult['insertedId']],
['insertedId' => $actualResult->getInsertedId()]
);
}
break;
case 'replaceOne':
case 'updateMany':
case 'updateOne':
$this->assertIsArray($expectedResult);
$this->assertInstanceOf(UpdateResult::class, $actualResult);
if (isset($expectedResult['matchedCount'])) {
$this->assertSame($expectedResult['matchedCount'], $actualResult->getMatchedCount());
}
if (isset($expectedResult['modifiedCount'])) {
$this->assertSame($expectedResult['modifiedCount'], $actualResult->getModifiedCount());
}
if (isset($expectedResult['upsertedCount'])) {
$this->assertSame($expectedResult['upsertedCount'], $actualResult->getUpsertedCount());
}
if (array_key_exists('upsertedId', $expectedResult)) {
$this->assertSameDocument(
['upsertedId' => $expectedResult['upsertedId']],
['upsertedId' => $actualResult->getUpsertedId()]
);
}
break;
default:
throw new LogicException('Unsupported operation: ' . $operation['name']);
}
}
/**
* Initializes data in the test collections.
*
* @param array $initialData
* @param array $expectedData
*/
private function initializeData(array $initialData, array $expectedData = null)
{
if (! empty($initialData)) {
$this->collection->insertMany($initialData);
}
if (! empty($expectedData)) {
$this->expectedCollection->insertMany($expectedData);
}
}
/**
* Prepares a request element for a bulkWrite operation.
*
* @param array $request
* @return array
*/
private function prepareBulkWriteRequest(array $request)
{
switch ($request['name']) {
case 'deleteMany':
case 'deleteOne':
return [
$request['name'] => [
$request['arguments']['filter'],
array_diff_key($request['arguments'], ['filter' => 1]),
],
];
case 'insertOne':
return [ 'insertOne' => [ $request['arguments']['document'] ]];
case 'replaceOne':
return [
'replaceOne' => [
$request['arguments']['filter'],
$request['arguments']['replacement'],
array_diff_key($request['arguments'], ['filter' => 1, 'replacement' => 1]),
],
];
case 'updateMany':
case 'updateOne':
return [
$request['name'] => [
$request['arguments']['filter'],
$request['arguments']['update'],
array_diff_key($request['arguments'], ['filter' => 1, 'update' => 1]),
],
];
default:
throw new LogicException('Unsupported bulk write request: ' . $request['name']);
}
}
/**
* Prepares arguments for findOneAndReplace and findOneAndUpdate operations.
*
* @param array $arguments
* @return array
*/
private function prepareFindAndModifyArguments(array $arguments)
{
if (isset($arguments['returnDocument'])) {
$arguments['returnDocument'] = 'after' === strtolower($arguments['returnDocument'])
? FindOneAndReplace::RETURN_DOCUMENT_AFTER
: FindOneAndReplace::RETURN_DOCUMENT_BEFORE;
}
return $arguments;
}
}