Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ilario-pierbattista authored Dec 7, 2024
2 parents f38fd63 + 2b12512 commit c937922
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions src/Capsule/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ClientConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
string $uri,
string $username = '',
string $password = '',
string $authSource = null,
?string $authSource = null,
array $options = [],
array $driverOptions = []
) {
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/profiler.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@

<!-- controller -->

<service id="facile_mongo_db.profiler_controller" class="Facile\MongoDbBundle\Controller\ProfilerController">
<service id="Facile\MongoDbBundle\Controller\ProfilerController" class="Facile\MongoDbBundle\Controller\ProfilerController">
<argument type="service" id="mongo.explain_query_service" />
<argument type="service" id="profiler" on-invalid="null" />
<tag name="controller.service_arguments" />
</service>

</services>
Expand Down
22 changes: 22 additions & 0 deletions tests/Functional/Capsule/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
25 changes: 17 additions & 8 deletions tests/Unit/Capsule/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,40 @@ 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);

$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);

$debugInfo = $newDb->__debugInfo();
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]];
}
}

0 comments on commit c937922

Please sign in to comment.