Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add profiling for countDocuments() and estimatedDocumentCount() #191

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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