Skip to content

Commit

Permalink
Fix deprecated warning using mongodb extension 1.20 or greater in Rea…
Browse files Browse the repository at this point in the history
…dPrederence::getMode()
  • Loading branch information
Oriol Parcerisa committed Sep 30, 2024
1 parent ead2444 commit a49500b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Capsule/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ private function prepareQuery(string $method, $filters, $data, array $options):

private function translateReadPreference(ReadPreference $readPreference): string
{
if (version_compare(phpversion('mongodb'), '1.20.0', '>=')) {
return $readPreference->getModeString();
}

switch ($readPreference->getMode()) {
case ReadPreference::RP_PRIMARY:
return 'primary';
Expand Down
31 changes: 30 additions & 1 deletion tests/Unit/Capsule/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public function test_selectCollection(): void
self::assertEquals('testdb', $debugInfo['databaseName']);
}

public function test_withOptions(): void
public function test_withOptions_using_mongodb_extension_lower_than_1_20_0(): void
{
if (version_compare(phpversion('mongodb'), '1.20.0', '>=')) {
$this->markTestSkipped('This test requires mongodb extension version 1.20.0 or later.');
}

$manager = new Manager('mongodb://localhost');
$logger = $this->prophesize(EventDispatcherInterface::class);

Expand All @@ -50,4 +54,29 @@ public function test_withOptions(): void
self::assertEquals('testdb', $debugInfo['databaseName']);
self::assertEquals(ReadPreference::RP_NEAREST, $debugInfo['readPreference']->getMode());
}

/**
* @requires extension mongodb 1.20.0
*/
public function test_withOptions_using_mongodb_extension_1_20_0_or_greater(): void
{
if (version_compare(phpversion('mongodb'), '1.20.0', '<')) {
$this->markTestSkipped('This test requires mongodb extension version 1.20.0 or later.');
}

$manager = new Manager('mongodb://localhost');
$logger = $this->prophesize(EventDispatcherInterface::class);

$db = new Database($manager, 'client_name', 'testdb', [], $logger->reveal());
self::assertInstanceOf(\MongoDB\Database::class, $db);

$newDb = $db->withOptions(['readPreference' => new ReadPreference(ReadPreference::NEAREST)]);

self::assertInstanceOf(Database::class, $newDb);

$debugInfo = $newDb->__debugInfo();
self::assertSame($manager, $debugInfo['manager']);
self::assertEquals('testdb', $debugInfo['databaseName']);
self::assertEquals(ReadPreference::NEAREST, $debugInfo['readPreference']->getModeString());
}
}

0 comments on commit a49500b

Please sign in to comment.