Skip to content

Commit ad943f8

Browse files
committed
Test FindAndModify with replacement doc resembling a pipeline
Add a comment explaining how the conditional object cast in FindAndModify::createCommandDocument() affects BSON encoding for the update option.
1 parent ae2b853 commit ad943f8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/Operation/FindAndModify.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,14 @@ private function createCommandDocument(): array
295295
if (isset($this->options['update'])) {
296296
/** @psalm-var array|object */
297297
$update = $this->options['update'];
298+
/* A non-empty pipeline will encode as a BSON array, so leave it
299+
* as-is. Cast anything else to an object since a BSON document is
300+
* likely expected. This includes empty arrays, which historically
301+
* can be used to represent empty replacement documents.
302+
*
303+
* This also allows an empty pipeline expressed as a PackedArray or
304+
* Serializable to still encode as a BSON array, since the object
305+
* cast will have no effect. */
298306
$cmd['update'] = is_pipeline($update) ? $update : (object) $update;
299307
}
300308

tests/Operation/FindAndModifyFunctionalTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function provideQueryDocuments(): array
5151
* @dataProvider provideReplacementDocuments
5252
* @dataProvider provideUpdateDocuments
5353
* @dataProvider provideUpdatePipelines
54+
* @dataProvider provideReplacementDocumentLikePipeline
5455
*/
5556
public function testUpdateDocuments($update, $expectedUpdate): void
5657
{
@@ -73,6 +74,16 @@ function (array $event) use ($expectedUpdate): void {
7374
);
7475
}
7576

77+
public function provideReplacementDocumentLikePipeline(): array
78+
{
79+
return [
80+
'replacement_like_pipeline' => [
81+
(object) ['0' => ['$set' => ['x' => 1]]],
82+
(object) ['0' => (object) ['$set' => (object) ['x' => 1]]],
83+
],
84+
];
85+
}
86+
7687
/** @see https://jira.mongodb.org/browse/PHPLIB-344 */
7788
public function testManagerReadConcernIsOmitted(): void
7889
{

0 commit comments

Comments
 (0)