From e7ee50db1c4b34bdf4337103ab5cf6e5bd60b79c Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Sat, 7 Dec 2024 07:06:51 +0100 Subject: [PATCH] Test against PHP 8.4 (#189) * Test against PHP 8.4 * Fix PHP 8.4 deprecations * Resolve MongoDB deprecations in tests * Add changelog entry --- .github/workflows/ci.yaml | 3 +++ CHANGELOG.md | 2 ++ src/Command/AbstractCommand.php | 2 +- src/Models/ClientConfiguration.php | 2 +- tests/Unit/Capsule/DatabaseTest.php | 25 +++++++++++++++++-------- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 36b7d76..6a7abca 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -48,6 +48,9 @@ jobs: - php: 8.3 mongo-ext: 1.19.0 mongo-img: 7.0 + - php: 8.4 + mongo-ext: 1.20.0 + mongo-img: 7.0 steps: - name: Checkout diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa4544..08d5389 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +* Fixed deprecation warnings using PHP 8.4 ## [1.6.1] (2024-10-04) ### Fixed diff --git a/src/Command/AbstractCommand.php b/src/Command/AbstractCommand.php index 4314534..54259f4 100644 --- a/src/Command/AbstractCommand.php +++ b/src/Command/AbstractCommand.php @@ -28,7 +28,7 @@ abstract class AbstractCommand extends Command /** * AbstractCommand constructor. */ - public function __construct(ContainerInterface $container, string $name = null) + public function __construct(ContainerInterface $container, ?string $name = null) { parent::__construct($name); $this->container = $container; diff --git a/src/Models/ClientConfiguration.php b/src/Models/ClientConfiguration.php index 1c694b6..0e6c7ff 100644 --- a/src/Models/ClientConfiguration.php +++ b/src/Models/ClientConfiguration.php @@ -27,7 +27,7 @@ public function __construct( string $uri, string $username = '', string $password = '', - string $authSource = null, + ?string $authSource = null, array $options = [], array $driverOptions = [] ) { diff --git a/tests/Unit/Capsule/DatabaseTest.php b/tests/Unit/Capsule/DatabaseTest.php index 02a95ef..cb00bcb 100644 --- a/tests/Unit/Capsule/DatabaseTest.php +++ b/tests/Unit/Capsule/DatabaseTest.php @@ -33,7 +33,12 @@ public function test_selectCollection(): void self::assertEquals('testdb', $debugInfo['databaseName']); } - public function test_withOptions(): void + /** + * @dataProvider readPreferenceDataProvider + * + * @param int|string $readPreference + */ + public function test_withOptions($readPreference): void { $manager = new Manager('mongodb://localhost'); $logger = $this->prophesize(EventDispatcherInterface::class); @@ -41,7 +46,7 @@ public function test_withOptions(): void $db = new Database($manager, 'client_name', 'testdb', [], $logger->reveal()); self::assertInstanceOf(\MongoDB\Database::class, $db); - $newDb = $db->withOptions(['readPreference' => new ReadPreference(ReadPreference::RP_NEAREST)]); + $newDb = $db->withOptions(['readPreference' => new ReadPreference($readPreference)]); self::assertInstanceOf(Database::class, $newDb); @@ -49,15 +54,19 @@ public function test_withOptions(): void self::assertSame($manager, $debugInfo['manager']); self::assertEquals('testdb', $debugInfo['databaseName']); - $this->assertReadPreferenceMode($debugInfo['readPreference']); + if (method_exists(ReadPreference::class, 'getModeString')) { + self::assertEquals(ReadPreference::NEAREST, $debugInfo['readPreference']->getModeString()); + } else { + self::assertEquals(ReadPreference::RP_NEAREST, $debugInfo['readPreference']->getMode()); + } } - public function assertReadPreferenceMode(ReadPreference $readPreference): void + public static function readPreferenceDataProvider(): array { - if (method_exists(ReadPreference::class, 'getModeString')) { - self::assertEquals(ReadPreference::NEAREST, $readPreference->getModeString()); - } else { - self::assertEquals(ReadPreference::RP_NEAREST, $readPreference->getMode()); + if (! method_exists(ReadPreference::class, 'getModeString')) { + return [[ReadPreference::RP_NEAREST]]; } + + return [[ReadPreference::NEAREST]]; } }