Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecated warning using mongodb extension 1.20 or greater in ReadPrederence::getMode() #187

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Vegeeto marked this conversation as resolved.
Show resolved Hide resolved

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
Vegeeto marked this conversation as resolved.
Show resolved Hide resolved
{
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());
}
}