File tree Expand file tree Collapse file tree 3 files changed +24
-2
lines changed Expand file tree Collapse file tree 3 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -295,6 +295,14 @@ private function createCommandDocument(): array
295
295
if (isset ($ this ->options ['update ' ])) {
296
296
/** @psalm-var array|object */
297
297
$ 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. */
298
306
$ cmd ['update ' ] = is_pipeline ($ update ) ? $ update : (object ) $ update ;
299
307
}
300
308
Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ public function provideQueryDocuments(): array
51
51
* @dataProvider provideReplacementDocuments
52
52
* @dataProvider provideUpdateDocuments
53
53
* @dataProvider provideUpdatePipelines
54
+ * @dataProvider provideReplacementDocumentLikePipeline
54
55
*/
55
56
public function testUpdateDocuments ($ update , $ expectedUpdate ): void
56
57
{
@@ -73,6 +74,19 @@ function (array $event) use ($expectedUpdate): void {
73
74
);
74
75
}
75
76
77
+ public function provideReplacementDocumentLikePipeline (): array
78
+ {
79
+ /* Note: this expected value differs from UpdateFunctionalTest because
80
+ * FindAndModify is not affected by libmongoc's pipeline detection for
81
+ * update commands (see: CDRIVER-4658). */
82
+ return [
83
+ 'replacement_like_pipeline ' => [
84
+ (object ) ['0 ' => ['$set ' => ['x ' => 1 ]]],
85
+ (object ) ['0 ' => (object ) ['$set ' => (object ) ['x ' => 1 ]]],
86
+ ],
87
+ ];
88
+ }
89
+
76
90
/** @see https://jira.mongodb.org/browse/PHPLIB-344 */
77
91
public function testManagerReadConcernIsOmitted (): void
78
92
{
Original file line number Diff line number Diff line change @@ -79,8 +79,8 @@ function (array $event) use ($expectedUpdate): void {
79
79
80
80
public function provideReplacementDocumentLikePipeline (): array
81
81
{
82
- /* libmongoc encodes this replacement document as a BSON array because
83
- * it resembles an update pipeline (see: CDRIVER-4658). */
82
+ /* Note: libmongoc encodes this replacement document as a BSON array
83
+ * because it resembles an update pipeline (see: CDRIVER-4658). */
84
84
return [
85
85
'replacement_like_pipeline ' => [
86
86
(object ) ['0 ' => ['$set ' => ['x ' => 1 ]]],
You can’t perform that action at this time.
0 commit comments