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..f0eb793 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [1.6.2] (2024-12-07) +### Added +* Profiling for countDocuments() and estimatedDocumentCount() (#191) by [@phleauran](https://github.com/phleauran). +### Fixed +* Fixed deprecation warnings using PHP 8.4 +* ProfilerController configuration (explain links). + ## [1.6.1] (2024-10-04) ### Fixed * Fixed deprecation warnings using mongo-ext 1.20 @@ -216,7 +223,8 @@ First unstable release ## 0.1-alpha (2016-06-30) First release -[Unreleased]: https://github.com/facile-it/mongodb-bundle/compare/1.6.1..master +[Unreleased]: https://github.com/facile-it/mongodb-bundle/compare/1.6.2..master +[1.6.1]: https://github.com/facile-it/mongodb-bundle/compare/1.6.1..1.6.2 [1.6.1]: https://github.com/facile-it/mongodb-bundle/compare/1.6.0..1.6.1 [1.6.0]: https://github.com/facile-it/mongodb-bundle/compare/1.5.0..1.6.0 [1.5.0]: https://github.com/facile-it/mongodb-bundle/compare/1.4.0..1.5.0 diff --git a/src/Capsule/Collection.php b/src/Capsule/Collection.php index ad51e13..fdf3074 100644 --- a/src/Capsule/Collection.php +++ b/src/Capsule/Collection.php @@ -60,6 +60,18 @@ public function count($filter = [], array $options = []) return $result; } + /** + * @inheritDoc + */ + public function countDocuments($filter = [], array $options = []) + { + $query = $this->prepareQuery(__FUNCTION__, $filter, [], $options); + $result = parent::countDocuments($query->getFilters(), $query->getOptions()); + $this->notifyQueryExecution($query); + + return $result; + } + /** * @inheritDoc */ @@ -180,6 +192,18 @@ public function distinct($fieldName, $filter = [], array $options = []) return $result; } + /** + * @inheritDoc + */ + public function estimatedDocumentCount(array $options = []) + { + $query = $this->prepareQuery(__FUNCTION__, [], [], $options); + $result = parent::estimatedDocumentCount($options); + $this->notifyQueryExecution($query); + + return $result; + } + /** * @param array|object $filters * @param array|object $data 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/src/Resources/config/profiler.xml b/src/Resources/config/profiler.xml index 404773b..e4f35d3 100644 --- a/src/Resources/config/profiler.xml +++ b/src/Resources/config/profiler.xml @@ -44,9 +44,10 @@ - + + diff --git a/tests/Functional/Capsule/CollectionTest.php b/tests/Functional/Capsule/CollectionTest.php index 69f4bf9..cf4c3cc 100644 --- a/tests/Functional/Capsule/CollectionTest.php +++ b/tests/Functional/Capsule/CollectionTest.php @@ -71,6 +71,17 @@ public function test_count(): void $coll->count(['test' => 1]); } + public function test_countDocuments(): void + { + $manager = $this->getManager(); + $ev = $this->prophesize(EventDispatcherInterface::class); + $this->assertEventsDispatching($ev); + + $coll = new Collection($manager, 'test_client', 'testdb', 'test_collection', [], $ev->reveal()); + + $coll->countDocuments(['test' => 1]); + } + public function test_find(): void { $manager = $this->getManager(); @@ -190,6 +201,17 @@ public function test_distinct(): void $coll->distinct('field'); } + public function test_estimatedDocumentCount(): void + { + $manager = $this->getManager(); + $ev = $this->prophesize(EventDispatcherInterface::class); + $this->assertEventsDispatching($ev); + + $coll = new Collection($manager, 'test_client', 'testdb', 'test_document', [], $ev->reveal()); + + $coll->estimatedDocumentCount(); + } + protected function assertEventsDispatching($ev) { $ev->dispatch(Argument::type(QueryEvent::class), QueryEvent::QUERY_PREPARED)->shouldBeCalled(); 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]]; } }