From 81366bfa6e0be67a7eab8839be45600c9eb4c4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 21 Oct 2024 21:59:03 +0200 Subject: [PATCH] Ensure compatibility with mongodb v2 --- .../Storage/Handler/MongoDbSessionHandlerTest.php | 4 ++++ src/Symfony/Component/Lock/Store/MongoDbStore.php | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php index 93c7995dd0ab9..b1c9db75938bc 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php @@ -133,6 +133,8 @@ public function testWrite() $this->assertInstanceOf(\MongoDB\BSON\UTCDateTime::class, $data[$this->options['time_field']]); $this->assertInstanceOf(\MongoDB\BSON\UTCDateTime::class, $data[$this->options['expiry_field']]); $this->assertGreaterThanOrEqual($expectedExpiry, round((string) $data[$this->options['expiry_field']] / 1000)); + + return $this->createMock(\MongoDB\UpdateResult::class); }); $this->assertTrue($this->storage->write('foo', 'bar')); @@ -153,6 +155,8 @@ public function testReplaceSessionData() ->method('updateOne') ->willReturnCallback(function ($criteria, $updateData, $options) use (&$data) { $data = $updateData; + + return $this->createMock(\MongoDB\UpdateResult::class); }); $this->storage->write('foo', 'bar'); diff --git a/src/Symfony/Component/Lock/Store/MongoDbStore.php b/src/Symfony/Component/Lock/Store/MongoDbStore.php index f8683c887e903..6f1717ee50b18 100644 --- a/src/Symfony/Component/Lock/Store/MongoDbStore.php +++ b/src/Symfony/Component/Lock/Store/MongoDbStore.php @@ -14,7 +14,7 @@ use MongoDB\BSON\UTCDateTime; use MongoDB\Client; use MongoDB\Collection; -use MongoDB\Driver\Exception\WriteException; +use MongoDB\Driver\Exception\BulkWriteException; use MongoDB\Driver\ReadPreference; use MongoDB\Exception\DriverRuntimeException; use MongoDB\Exception\InvalidArgumentException as MongoInvalidArgumentException; @@ -209,7 +209,7 @@ public function save(Key $key) try { $this->upsert($key, $this->initialTtl); - } catch (WriteException $e) { + } catch (BulkWriteException $e) { if ($this->isDuplicateKeyException($e)) { throw new LockConflictedException('Lock was acquired by someone else.', 0, $e); } @@ -235,7 +235,7 @@ public function putOffExpiration(Key $key, float $ttl) try { $this->upsert($key, $ttl); - } catch (WriteException $e) { + } catch (BulkWriteException $e) { if ($this->isDuplicateKeyException($e)) { throw new LockConflictedException('Failed to put off the expiration of the lock.', 0, $e); } @@ -268,7 +268,7 @@ public function exists(Key $key): bool '$gt' => $this->createMongoDateTime(microtime(true)), ], ], [ - 'readPreference' => new ReadPreference(\defined(ReadPreference::PRIMARY) ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY), + 'readPreference' => new ReadPreference(\defined(ReadPreference::class.'::PRIMARY') ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY), ]); } @@ -309,7 +309,7 @@ private function upsert(Key $key, float $ttl) ); } - private function isDuplicateKeyException(WriteException $e): bool + private function isDuplicateKeyException(BulkWriteException $e): bool { $code = $e->getCode(); @@ -345,7 +345,7 @@ private function getCollection(): Collection */ private function createMongoDateTime(float $seconds): UTCDateTime { - return new UTCDateTime($seconds * 1000); + return new UTCDateTime((int) ($seconds * 1000)); } /**