Skip to content

Commit e3d68a7

Browse files
committed
Handle null values in findAndModify responses
1 parent ed56c09 commit e3d68a7

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

psalm-baseline.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@
436436
<code><![CDATA[$cmd['upsert']]]></code>
437437
<code><![CDATA[$options['session']]]></code>
438438
<code><![CDATA[$options['writeConcern']]]></code>
439+
<code>$value</code>
439440
</MixedAssignment>
440441
<MixedInferredReturnType>
441442
<code>array|object|null</code>
@@ -445,7 +446,8 @@
445446
<code>isInTransaction</code>
446447
</MixedMethodCall>
447448
<MixedReturnStatement>
448-
<code><![CDATA[$this->options['codec']->decode($result->get('value'))]]></code>
449+
<code><![CDATA[$value === null ? $value : $this->options['codec']->decode($value)]]></code>
450+
<code><![CDATA[$value === null ? $value : $this->options['codec']->decode($value)]]></code>
449451
<code><![CDATA[is_object($result) ? ($result->value ?? null) : null]]></code>
450452
<code><![CDATA[is_object($result) ? ($result->value ?? null) : null]]></code>
451453
</MixedReturnStatement>

src/Operation/FindAndModify.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ public function execute(Server $server)
262262
$result = current($cursor->toArray());
263263
assert($result instanceof Document);
264264

265-
return $this->options['codec']->decode($result->get('value'));
265+
$value = $result->get('value');
266+
267+
return $value === null ? $value : $this->options['codec']->decode($value);
266268
}
267269

268270
if (isset($this->options['typeMap'])) {

tests/Operation/FindAndModifyFunctionalTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,20 @@ public function testFindOneAndDeleteWithCodec(): void
296296
self::assertEquals(TestObject::createDecodedForFixture(1), $result);
297297
}
298298

299+
public function testFindOneAndDeleteNothingWithCodec(): void
300+
{
301+
// When the query does not match any documents, the operation returns null
302+
$operation = new FindAndModify(
303+
$this->getDatabaseName(),
304+
$this->getCollectionName(),
305+
['remove' => true, 'codec' => new TestDocumentCodec()],
306+
);
307+
308+
$result = $operation->execute($this->getPrimaryServer());
309+
310+
self::assertNull($result);
311+
}
312+
299313
public function testFindOneAndUpdateWithCodec(): void
300314
{
301315
$this->createFixtures(1);
@@ -311,6 +325,20 @@ public function testFindOneAndUpdateWithCodec(): void
311325
self::assertEquals(TestObject::createDecodedForFixture(1), $result);
312326
}
313327

328+
public function testFindOneAndUpdateNothingWithCodec(): void
329+
{
330+
// When the query does not match any documents, the operation returns null
331+
$operation = new FindAndModify(
332+
$this->getDatabaseName(),
333+
$this->getCollectionName(),
334+
['update' => ['$set' => ['x.foo' => 'baz']], 'codec' => new TestDocumentCodec()],
335+
);
336+
337+
$result = $operation->execute($this->getPrimaryServer());
338+
339+
self::assertNull($result);
340+
}
341+
314342
public function testFindOneAndReplaceWithCodec(): void
315343
{
316344
$this->createFixtures(1);
@@ -326,6 +354,20 @@ public function testFindOneAndReplaceWithCodec(): void
326354
self::assertEquals(TestObject::createDecodedForFixture(1), $result);
327355
}
328356

357+
public function testFindOneAndReplaceNothingWithCodec(): void
358+
{
359+
// When the query does not match any documents, the operation returns null
360+
$operation = new FindAndModify(
361+
$this->getDatabaseName(),
362+
$this->getCollectionName(),
363+
['update' => ['_id' => 1], 'codec' => new TestDocumentCodec()],
364+
);
365+
366+
$result = $operation->execute($this->getPrimaryServer());
367+
368+
self::assertNull($result);
369+
}
370+
329371
/**
330372
* Create data fixtures.
331373
*/

0 commit comments

Comments
 (0)