Skip to content

Commit 2422da7

Browse files
committed
Explicit result iteration and require filter/pipeline args
Remove automatic result iteration in Operation::execute(), since ChangeStreams will no longer be the lone exception once createFindCursor is implemented. Additionally, filter/pipeline args need not default since they are required by the CRUD and change stream specs and present in all valid spec tests. The previous behavior would conflict with a forthcoming "createFindCursor fails if filter is not specified" test.
1 parent aed48fb commit 2422da7

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

tests/UnifiedSpecTests/Operation.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use PHPUnit\Framework\AssertionFailedError;
1919
use stdClass;
2020
use Throwable;
21-
use Traversable;
2221
use function array_diff_key;
2322
use function array_key_exists;
2423
use function array_map;
@@ -204,10 +203,6 @@ private function execute()
204203
Assert::fail('Unsupported entity type: ' . get_class($object));
205204
}
206205

207-
if ($result instanceof Traversable && ! $result instanceof ChangeStream) {
208-
return iterator_to_array($result);
209-
}
210-
211206
return $result;
212207
}
213208

@@ -249,16 +244,16 @@ private function executeForClient(Client $client)
249244
switch ($this->name) {
250245
case 'createChangeStream':
251246
$changeStream = $client->watch(
252-
$args['pipeline'] ?? [],
247+
$args['pipeline'],
253248
array_diff_key($args, ['pipeline' => 1])
254249
);
255250
$changeStream->rewind();
256251

257252
return $changeStream;
258253
case 'listDatabaseNames':
259-
return $client->listDatabaseNames($args);
254+
return iterator_to_array($client->listDatabaseNames($args));
260255
case 'listDatabases':
261-
return $client->listDatabases($args);
256+
return iterator_to_array($client->listDatabases($args));
262257
default:
263258
Assert::fail('Unsupported client operation: ' . $this->name);
264259
}
@@ -270,18 +265,18 @@ private function executeForCollection(Collection $collection)
270265

271266
switch ($this->name) {
272267
case 'aggregate':
273-
return $collection->aggregate(
268+
return iterator_to_array($collection->aggregate(
274269
$args['pipeline'],
275270
array_diff_key($args, ['pipeline' => 1])
276-
);
271+
));
277272
case 'bulkWrite':
278273
return $collection->bulkWrite(
279274
array_map('self::prepareBulkWriteRequest', $args['requests']),
280275
array_diff_key($args, ['requests' => 1])
281276
);
282277
case 'createChangeStream':
283278
$changeStream = $collection->watch(
284-
$args['pipeline'] ?? [],
279+
$args['pipeline'],
285280
array_diff_key($args, ['pipeline' => 1])
286281
);
287282
$changeStream->rewind();
@@ -299,9 +294,8 @@ private function executeForCollection(Collection $collection)
299294
);
300295
case 'count':
301296
case 'countDocuments':
302-
case 'find':
303297
return $collection->{$this->name}(
304-
$args['filter'] ?? [],
298+
$args['filter'],
305299
array_diff_key($args, ['filter' => 1])
306300
);
307301
case 'estimatedDocumentCount':
@@ -321,11 +315,16 @@ private function executeForCollection(Collection $collection)
321315

322316
return $collection->distinct(
323317
$args['fieldName'],
324-
$args['filter'] ?? [],
318+
$args['filter'],
325319
array_diff_key($args, ['fieldName' => 1, 'filter' => 1])
326320
);
327321
case 'drop':
328322
return $collection->drop($args);
323+
case 'find':
324+
return iterator_to_array($collection->find(
325+
$args['filter'],
326+
array_diff_key($args, ['filter' => 1])
327+
));
329328
case 'findOne':
330329
return $collection->findOne($args['filter'], array_diff_key($args, ['filter' => 1]));
331330
case 'findOneAndReplace':
@@ -378,7 +377,7 @@ private function executeForCollection(Collection $collection)
378377
array_diff_key($args, ['document' => 1])
379378
);
380379
case 'listIndexes':
381-
return $collection->listIndexes($args);
380+
return iterator_to_array($collection->listIndexes($args));
382381
case 'mapReduce':
383382
return $collection->mapReduce(
384383
$args['map'],
@@ -397,13 +396,13 @@ private function executeForDatabase(Database $database)
397396

398397
switch ($this->name) {
399398
case 'aggregate':
400-
return $database->aggregate(
399+
return iterator_to_array($database->aggregate(
401400
$args['pipeline'],
402401
array_diff_key($args, ['pipeline' => 1])
403-
);
402+
));
404403
case 'createChangeStream':
405404
$changeStream = $database->watch(
406-
$args['pipeline'] ?? [],
405+
$args['pipeline'],
407406
array_diff_key($args, ['pipeline' => 1])
408407
);
409408
$changeStream->rewind();
@@ -420,9 +419,9 @@ private function executeForDatabase(Database $database)
420419
array_diff_key($args, ['collection' => 1])
421420
);
422421
case 'listCollectionNames':
423-
return $database->listCollectionNames($args);
422+
return iterator_to_array($database->listCollectionNames($args));
424423
case 'listCollections':
425-
return $database->listCollections($args);
424+
return iterator_to_array($database->listCollections($args));
426425
case 'runCommand':
427426
return $database->command(
428427
$args['command'],

0 commit comments

Comments
 (0)