diff --git a/src/Operation/Aggregate.php b/src/Operation/Aggregate.php index 1916f8141..43f964410 100644 --- a/src/Operation/Aggregate.php +++ b/src/Operation/Aggregate.php @@ -229,10 +229,12 @@ public function __construct(string $databaseName, ?string $collectionName, array unset($options['batchSize']); } - /* Ignore batchSize for writes, since no documents are returned and a - * batchSize of zero could prevent the pipeline from executing. */ if ($this->isWrite) { + /* Ignore batchSize for writes, since no documents are returned and + * a batchSize of zero could prevent the pipeline from executing. */ unset($options['batchSize']); + } else { + unset($options['writeConcern']); } $this->databaseName = $databaseName; @@ -366,16 +368,12 @@ private function executeCommand(Server $server, Command $command): Cursor { $options = []; - foreach (['readConcern', 'readPreference', 'session'] as $option) { + foreach (['readConcern', 'readPreference', 'session', 'writeConcern'] as $option) { if (isset($this->options[$option])) { $options[$option] = $this->options[$option]; } } - if ($this->isWrite && isset($this->options['writeConcern'])) { - $options['writeConcern'] = $this->options['writeConcern']; - } - if (! $this->isWrite) { return $server->executeReadCommand($this->databaseName, $command, $options); } diff --git a/tests/Collection/CollectionFunctionalTest.php b/tests/Collection/CollectionFunctionalTest.php index 4a8b5bf6b..eeb4af16c 100644 --- a/tests/Collection/CollectionFunctionalTest.php +++ b/tests/Collection/CollectionFunctionalTest.php @@ -21,7 +21,7 @@ use function call_user_func; use function is_scalar; use function json_encode; -use function strchr; +use function str_contains; use function usort; use function version_compare; @@ -434,16 +434,30 @@ public function testMapReduce(): void public function collectionMethodClosures() { return [ - [ + 'read-only aggregate' => [ function ($collection, $session, $options = []): void { $collection->aggregate( [['$match' => ['_id' => ['$lt' => 3]]]], ['session' => $session] + $options ); + }, 'r', + ], + + /* Disabled, as write aggregations are not supported in transactions + 'read-write aggregate' => [ + function ($collection, $session, $options = []): void { + $collection->aggregate( + [ + ['$match' => ['_id' => ['$lt' => 3]]], + ['$merge' => $collection . '_out'], + ], + ['session' => $session] + $options + ); }, 'rw', ], + */ - [ + 'bulkWrite insertOne' => [ function ($collection, $session, $options = []): void { $collection->bulkWrite( [['insertOne' => [['test' => 'foo']]]], @@ -453,7 +467,7 @@ function ($collection, $session, $options = []): void { ], /* Disabled, as count command can't be used in transactions - [ + 'count' => [ function($collection, $session, $options = []) { $collection->count( [], @@ -463,7 +477,7 @@ function($collection, $session, $options = []) { ], */ - [ + 'countDocuments' => [ function ($collection, $session, $options = []): void { $collection->countDocuments( [], @@ -473,7 +487,7 @@ function ($collection, $session, $options = []): void { ], /* Disabled, as it's illegal to use createIndex command in transactions - [ + 'createIndex' => [ function($collection, $session, $options = []) { $collection->createIndex( ['test' => 1], @@ -483,7 +497,7 @@ function($collection, $session, $options = []) { ], */ - [ + 'deleteMany' => [ function ($collection, $session, $options = []): void { $collection->deleteMany( ['test' => 'foo'], @@ -492,7 +506,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'deleteOne' => [ function ($collection, $session, $options = []): void { $collection->deleteOne( ['test' => 'foo'], @@ -501,7 +515,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'distinct' => [ function ($collection, $session, $options = []): void { $collection->distinct( '_id', @@ -512,7 +526,7 @@ function ($collection, $session, $options = []): void { ], /* Disabled, as it's illegal to use drop command in transactions - [ + 'drop' => [ function($collection, $session, $options = []) { $collection->drop( ['session' => $session] + $options @@ -522,7 +536,7 @@ function($collection, $session, $options = []) { */ /* Disabled, as it's illegal to use dropIndexes command in transactions - [ + 'dropIndex' => [ function($collection, $session, $options = []) { $collection->dropIndex( '_id_1', @@ -532,7 +546,7 @@ function($collection, $session, $options = []) { ], */ /* Disabled, as it's illegal to use dropIndexes command in transactions - [ + 'dropIndexes' => [ function($collection, $session, $options = []) { $collection->dropIndexes( ['session' => $session] + $options @@ -542,7 +556,7 @@ function($collection, $session, $options = []) { */ /* Disabled, as count command can't be used in transactions - [ + 'estimatedDocumentCount' => [ function($collection, $session, $options = []) { $collection->estimatedDocumentCount( ['session' => $session] + $options @@ -551,7 +565,7 @@ function($collection, $session, $options = []) { ], */ - [ + 'find' => [ function ($collection, $session, $options = []): void { $collection->find( ['test' => 'foo'], @@ -560,7 +574,7 @@ function ($collection, $session, $options = []): void { }, 'r', ], - [ + 'findOne' => [ function ($collection, $session, $options = []): void { $collection->findOne( ['test' => 'foo'], @@ -569,7 +583,7 @@ function ($collection, $session, $options = []): void { }, 'r', ], - [ + 'findOneAndDelete' => [ function ($collection, $session, $options = []): void { $collection->findOneAndDelete( ['test' => 'foo'], @@ -578,7 +592,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'findOneAndReplace' => [ function ($collection, $session, $options = []): void { $collection->findOneAndReplace( ['test' => 'foo'], @@ -588,7 +602,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'findOneAndUpdate' => [ function ($collection, $session, $options = []): void { $collection->findOneAndUpdate( ['test' => 'foo'], @@ -598,7 +612,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'insertMany' => [ function ($collection, $session, $options = []): void { $collection->insertMany( [ @@ -610,7 +624,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'insertOne' => [ function ($collection, $session, $options = []): void { $collection->insertOne( ['test' => 'foo'], @@ -620,7 +634,7 @@ function ($collection, $session, $options = []): void { ], /* Disabled, as it's illegal to use listIndexes command in transactions - [ + 'listIndexes' => [ function($collection, $session, $options = []) { $collection->listIndexes( ['session' => $session] + $options @@ -630,7 +644,7 @@ function($collection, $session, $options = []) { */ /* Disabled, as it's illegal to use mapReduce command in transactions - [ + 'mapReduce' => [ function($collection, $session, $options = []) { $collection->mapReduce( new \MongoDB\BSON\Javascript('function() { emit(this.state, this.pop); }'), @@ -642,7 +656,7 @@ function($collection, $session, $options = []) { ], */ - [ + 'replaceOne' => [ function ($collection, $session, $options = []): void { $collection->replaceOne( ['test' => 'foo'], @@ -652,7 +666,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'updateMany' => [ function ($collection, $session, $options = []): void { $collection->updateMany( ['test' => 'foo'], @@ -662,7 +676,7 @@ function ($collection, $session, $options = []): void { }, 'w', ], - [ + 'updateOne' => [ function ($collection, $session, $options = []): void { $collection->updateOne( ['test' => 'foo'], @@ -673,7 +687,7 @@ function ($collection, $session, $options = []): void { ], /* Disabled, as it's illegal to use change streams in transactions - [ + 'watch' => [ function($collection, $session, $options = []) { $collection->watch( [], @@ -685,32 +699,28 @@ function($collection, $session, $options = []) { ]; } - public function collectionReadMethodClosures() + public function collectionReadMethodClosures(): array { return array_filter( $this->collectionMethodClosures(), function ($rw) { - if (strchr($rw[1], 'r') !== false) { - return true; - } + return str_contains($rw[1], 'r'); } ); } - public function collectionWriteMethodClosures() + public function collectionWriteMethodClosures(): array { return array_filter( $this->collectionMethodClosures(), function ($rw) { - if (strchr($rw[1], 'w') !== false) { - return true; - } + return str_contains($rw[1], 'w'); } ); } /** @dataProvider collectionMethodClosures */ - public function testMethodDoesNotInheritReadWriteConcernInTranasaction(Closure $method): void + public function testMethodDoesNotInheritReadWriteConcernInTransaction(Closure $method): void { $this->skipIfTransactionsAreNotSupported();