From dae724a2e5e67ca5de444c52d1f23a80d5ba8401 Mon Sep 17 00:00:00 2001 From: Vegeeto Date: Fri, 4 Oct 2024 09:10:07 +0200 Subject: [PATCH] Fix deprecated warning using mongodb extension 1.20 or greater in ReadPrederence::getMode() (#187) * Fix deprecated warning using mongodb extension 1.20 or greater in ReadPrederence::getMode() * Update src/Capsule/Collection.php Co-authored-by: Alessandro Lai * Add new configuration on ci workflow * Extract a method to assert read preference mode * Revert test name --------- Co-authored-by: Oriol Parcerisa Co-authored-by: Alessandro Lai --- .github/workflows/ci.yaml | 4 ++++ src/Capsule/Collection.php | 4 ++++ tests/Unit/Capsule/DatabaseTest.php | 12 +++++++++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9bdb6a0..83721ae 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,6 +33,10 @@ jobs: mongo-ext: 1.6.0 mongo-img: 4.2 symfony: "^4.4" + - php: 7.4 + mongo-ext: 1.20.0 + mongo-img: 5.0 + symfony: "^4.4" - php: 8.1 mongo-ext: 1.12.0 mongo-img: 5.0 diff --git a/src/Capsule/Collection.php b/src/Capsule/Collection.php index f49311c..ad51e13 100644 --- a/src/Capsule/Collection.php +++ b/src/Capsule/Collection.php @@ -206,6 +206,10 @@ private function prepareQuery(string $method, $filters, $data, array $options): private function translateReadPreference(ReadPreference $readPreference): string { + if (method_exists(ReadPreference::class, 'getModeString')) { + return $readPreference->getModeString(); + } + switch ($readPreference->getMode()) { case ReadPreference::RP_PRIMARY: return 'primary'; diff --git a/tests/Unit/Capsule/DatabaseTest.php b/tests/Unit/Capsule/DatabaseTest.php index bc1ca9c..02a95ef 100644 --- a/tests/Unit/Capsule/DatabaseTest.php +++ b/tests/Unit/Capsule/DatabaseTest.php @@ -48,6 +48,16 @@ public function test_withOptions(): void $debugInfo = $newDb->__debugInfo(); self::assertSame($manager, $debugInfo['manager']); self::assertEquals('testdb', $debugInfo['databaseName']); - self::assertEquals(ReadPreference::RP_NEAREST, $debugInfo['readPreference']->getMode()); + + $this->assertReadPreferenceMode($debugInfo['readPreference']); + } + + public function assertReadPreferenceMode(ReadPreference $readPreference): void + { + if (method_exists(ReadPreference::class, 'getModeString')) { + self::assertEquals(ReadPreference::NEAREST, $readPreference->getModeString()); + } else { + self::assertEquals(ReadPreference::RP_NEAREST, $readPreference->getMode()); + } } }