Skip to content
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
5 changes: 5 additions & 0 deletions src/Doctrine/Odm/Extension/PaginationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public function applyToCollection(Builder $aggregationBuilder, string $resourceC
*/
$repository = $manager->getRepository($resourceClass);

// Limit the documents entering $facet to avoid buffering the whole result set
if (!$doesCount && $limit > 0) {
$aggregationBuilder->limit($offset + $limit);
}

$facet = $aggregationBuilder->facet();
$addFields = $aggregationBuilder->addFields();

Expand Down
51 changes: 43 additions & 8 deletions src/Doctrine/Odm/Tests/Extension/PaginationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ public function testApplyToCollectionWithMaximumItemsPerPage(): void
$extension->applyToCollection($aggregationBuilderProphecy->reveal(), 'Foo', (new GetCollection())->withPaginationEnabled(true)->withPaginationClientEnabled(true)->withPaginationMaximumItemsPerPage(80), $context);
}

public function testApplyToCollectionWithPartialPagination(): void
{
$pagination = new Pagination([
'partial' => true,
'page_parameter_name' => '_page',
]);

$aggregationBuilderProphecy = $this->mockAggregationBuilder(20, 20, true);

$context = ['filters' => ['_page' => 2, 'itemsPerPage' => 20]];

$extension = new PaginationExtension(
$this->managerRegistryProphecy->reveal(),
$pagination
);
$extension->applyToCollection($aggregationBuilderProphecy->reveal(), 'Foo', (new GetCollection())->withPaginationEnabled(true)->withPaginationPartial(true)->withPaginationClientEnabled(true)->withPaginationItemsPerPage(20), $context);
}

public function testSupportsResult(): void
{
$pagination = new Pagination();
Expand Down Expand Up @@ -430,11 +448,14 @@ public function testGetResultWithExecuteOptions(): void
$this->assertInstanceOf(PaginatorInterface::class, $result);
}

private function mockAggregationBuilder(int $expectedOffset, int $expectedLimit): ObjectProphecy
private function mockAggregationBuilder(int $expectedOffset, int $expectedLimit, bool $partial = false): ObjectProphecy
{
$countProphecy = $this->prophesize(Count::class);
$countAggregationBuilderProphecy = $this->prophesize(Builder::class);
$countAggregationBuilderProphecy->count('count')->shouldBeCalled()->willReturn($countProphecy->reveal());

if (!$partial) {
$countAggregationBuilderProphecy->count('count')->shouldBeCalled()->willReturn($countProphecy->reveal());
}

$repositoryProphecy = $this->prophesize(DocumentRepository::class);

Expand All @@ -448,10 +469,17 @@ private function mockAggregationBuilder(int $expectedOffset, int $expectedLimit)

if ($expectedLimit > 0) {
$resultsAggregationBuilderProphecy = $this->prophesize(Builder::class);
$repositoryProphecy->createAggregationBuilder()->shouldBeCalled()->willReturn(
$resultsAggregationBuilderProphecy->reveal(),
$countAggregationBuilderProphecy->reveal()
);

if ($partial) {
$repositoryProphecy->createAggregationBuilder()->shouldBeCalled()->willReturn(
$resultsAggregationBuilderProphecy->reveal()
);
} else {
$repositoryProphecy->createAggregationBuilder()->shouldBeCalled()->willReturn(
$resultsAggregationBuilderProphecy->reveal(),
$countAggregationBuilderProphecy->reveal()
);
}

$skipProphecy = $this->prophesize(Skip::class);
$skipProphecy->limit($expectedLimit)->shouldBeCalled()->willReturn($skipProphecy->reveal());
Expand All @@ -467,15 +495,22 @@ private function mockAggregationBuilder(int $expectedOffset, int $expectedLimit)
$addFieldsProphecy->literal([])->shouldBeCalled()->willReturn($addFieldsProphecy->reveal());
}

$facetProphecy->field('count')->shouldBeCalled()->willReturn($facetProphecy->reveal());
$facetProphecy->pipeline($countProphecy)->shouldBeCalled()->willReturn($facetProphecy->reveal());
if (!$partial) {
$facetProphecy->field('count')->shouldBeCalled()->willReturn($facetProphecy->reveal());
$facetProphecy->pipeline($countProphecy)->shouldBeCalled()->willReturn($facetProphecy->reveal());
}

$addFieldsProphecy->field('__api_first_result__')->shouldBeCalled()->willReturn($addFieldsProphecy->reveal());
$addFieldsProphecy->literal($expectedOffset)->shouldBeCalled()->willReturn($addFieldsProphecy->reveal());
$addFieldsProphecy->field('__api_max_results__')->shouldBeCalled()->willReturn($addFieldsProphecy->reveal());
$addFieldsProphecy->literal($expectedLimit)->shouldBeCalled()->willReturn($addFieldsProphecy->reveal());

$aggregationBuilderProphecy = $this->prophesize(Builder::class);

if ($partial && $expectedLimit > 0) {
$aggregationBuilderProphecy->limit($expectedOffset + $expectedLimit)->shouldBeCalled();
}

$aggregationBuilderProphecy->facet()->shouldBeCalled()->willReturn($facetProphecy->reveal());
$aggregationBuilderProphecy->addFields()->shouldBeCalled()->willReturn($addFieldsProphecy->reveal());

Expand Down
Loading