Skip to content

Commit b756c5e

Browse files
bug symfony#58619 [HttpFoundation][Lock] Ensure compatibility with ext-mongodb v2 (GromNaN)
This PR was merged into the 5.4 branch. Discussion ---------- [HttpFoundation][Lock] Ensure compatibility with ext-mongodb v2 | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT The extension `mongodb` and the library `mongodb/mongodb` will soon have a version 2.0 that brings breaking changes (see [extension changes](https://jira.mongodb.org/browse/PHPC-2445) and [library changes](https://jira.mongodb.org/browse/PHPLIB-1332)). This PR ensures compatibility with the upcoming version for Symfony 5.4. - Return types added to `MongoDB\Collection::updateOne()`, the closure in the mock `willReturnCallback` must return an object. mongodb/mongo-php-library#1391 - `MongoDB\Driver\Exception\WriteException` removed in favor of `BulkWriteException` mongodb/mongo-php-driver#1685. No need to keep catching `WriteException` since a the driver bulk API have always be used. - `float` support to construct `MongoDB\BSON\UTCDateTime` is removed mongodb/mongo-php-driver#1709 Commits ------- 81366bf Ensure compatibility with mongodb v2
2 parents af21e7e + 81366bf commit b756c5e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MongoDbSessionHandlerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public function testWrite()
133133
$this->assertInstanceOf(\MongoDB\BSON\UTCDateTime::class, $data[$this->options['time_field']]);
134134
$this->assertInstanceOf(\MongoDB\BSON\UTCDateTime::class, $data[$this->options['expiry_field']]);
135135
$this->assertGreaterThanOrEqual($expectedExpiry, round((string) $data[$this->options['expiry_field']] / 1000));
136+
137+
return $this->createMock(\MongoDB\UpdateResult::class);
136138
});
137139

138140
$this->assertTrue($this->storage->write('foo', 'bar'));
@@ -153,6 +155,8 @@ public function testReplaceSessionData()
153155
->method('updateOne')
154156
->willReturnCallback(function ($criteria, $updateData, $options) use (&$data) {
155157
$data = $updateData;
158+
159+
return $this->createMock(\MongoDB\UpdateResult::class);
156160
});
157161

158162
$this->storage->write('foo', 'bar');

src/Symfony/Component/Lock/Store/MongoDbStore.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use MongoDB\BSON\UTCDateTime;
1515
use MongoDB\Client;
1616
use MongoDB\Collection;
17-
use MongoDB\Driver\Exception\WriteException;
17+
use MongoDB\Driver\Exception\BulkWriteException;
1818
use MongoDB\Driver\ReadPreference;
1919
use MongoDB\Exception\DriverRuntimeException;
2020
use MongoDB\Exception\InvalidArgumentException as MongoInvalidArgumentException;
@@ -209,7 +209,7 @@ public function save(Key $key)
209209

210210
try {
211211
$this->upsert($key, $this->initialTtl);
212-
} catch (WriteException $e) {
212+
} catch (BulkWriteException $e) {
213213
if ($this->isDuplicateKeyException($e)) {
214214
throw new LockConflictedException('Lock was acquired by someone else.', 0, $e);
215215
}
@@ -235,7 +235,7 @@ public function putOffExpiration(Key $key, float $ttl)
235235

236236
try {
237237
$this->upsert($key, $ttl);
238-
} catch (WriteException $e) {
238+
} catch (BulkWriteException $e) {
239239
if ($this->isDuplicateKeyException($e)) {
240240
throw new LockConflictedException('Failed to put off the expiration of the lock.', 0, $e);
241241
}
@@ -268,7 +268,7 @@ public function exists(Key $key): bool
268268
'$gt' => $this->createMongoDateTime(microtime(true)),
269269
],
270270
], [
271-
'readPreference' => new ReadPreference(\defined(ReadPreference::PRIMARY) ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY),
271+
'readPreference' => new ReadPreference(\defined(ReadPreference::class.'::PRIMARY') ? ReadPreference::PRIMARY : ReadPreference::RP_PRIMARY),
272272
]);
273273
}
274274

@@ -309,7 +309,7 @@ private function upsert(Key $key, float $ttl)
309309
);
310310
}
311311

312-
private function isDuplicateKeyException(WriteException $e): bool
312+
private function isDuplicateKeyException(BulkWriteException $e): bool
313313
{
314314
$code = $e->getCode();
315315

@@ -345,7 +345,7 @@ private function getCollection(): Collection
345345
*/
346346
private function createMongoDateTime(float $seconds): UTCDateTime
347347
{
348-
return new UTCDateTime($seconds * 1000);
348+
return new UTCDateTime((int) ($seconds * 1000));
349349
}
350350

351351
/**

0 commit comments

Comments
 (0)