Skip to content

Commit

Permalink
Add profiling for countDocuments() and estimatedDocumentCount() (#191)
Browse files Browse the repository at this point in the history
Co-authored-by: phleauran <[email protected]>
  • Loading branch information
phleauran and phleauran authored Dec 7, 2024
1 parent e7ee50d commit a46aca6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
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
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

0 comments on commit a46aca6

Please sign in to comment.