1414namespace ApiPlatform \Doctrine \Odm \Tests \Extension ;
1515
1616use ApiPlatform \Doctrine \Odm \Extension \PaginationExtension ;
17- use ApiPlatform \Doctrine \Odm \Paginator ;
1817use ApiPlatform \Doctrine \Odm \Tests \DoctrineMongoDbOdmSetup ;
1918use ApiPlatform \Doctrine \Odm \Tests \Fixtures \Document \Dummy ;
2019use ApiPlatform \Metadata \Exception \InvalidArgumentException ;
2322use ApiPlatform \State \Pagination \PaginatorInterface ;
2423use ApiPlatform \State \Pagination \PartialPaginatorInterface ;
2524use Doctrine \ODM \MongoDB \Aggregation \Builder ;
25+ use Doctrine \ODM \MongoDB \Aggregation \Stage \AddFields ;
2626use Doctrine \ODM \MongoDB \Aggregation \Stage \Count ;
2727use Doctrine \ODM \MongoDB \Aggregation \Stage \Facet ;
28- use Doctrine \ODM \MongoDB \Aggregation \Stage \MatchStage as AggregationMatch ;
2928use Doctrine \ODM \MongoDB \Aggregation \Stage \Skip ;
3029use Doctrine \ODM \MongoDB \DocumentManager ;
3130use Doctrine \ODM \MongoDB \Iterator \Iterator ;
@@ -42,6 +41,7 @@ class PaginationExtensionTest extends TestCase
4241{
4342 use ProphecyTrait;
4443
44+ /** @var ObjectProphecy<ManagerRegistry> */
4545 private ObjectProphecy $ managerRegistryProphecy ;
4646
4747 /**
@@ -322,11 +322,14 @@ public function testGetResult(): void
322322 $ iteratorProphecy = $ this ->prophesize (Iterator::class);
323323 $ iteratorProphecy ->toArray ()->willReturn ([
324324 [
325+ 'results ' => [],
325326 'count ' => [
326327 [
327328 'count ' => 9 ,
328329 ],
329330 ],
331+ '__api_first_result__ ' => 3 ,
332+ '__api_max_results__ ' => 6 ,
330333 ],
331334 ]);
332335
@@ -344,6 +347,12 @@ public function testGetResult(): void
344347 ],
345348 ],
346349 ],
350+ [
351+ '$addFields ' => [
352+ '__api_first_result__ ' => ['$literal ' => 3 ],
353+ '__api_max_results__ ' => ['$literal ' => 6 ],
354+ ],
355+ ],
347356 ]);
348357
349358 $ paginationExtension = new PaginationExtension (
@@ -370,11 +379,14 @@ public function testGetResultWithExecuteOptions(): void
370379 $ iteratorProphecy = $ this ->prophesize (Iterator::class);
371380 $ iteratorProphecy ->toArray ()->willReturn ([
372381 [
382+ 'results ' => [],
373383 'count ' => [
374384 [
375385 'count ' => 9 ,
376386 ],
377387 ],
388+ '__api_first_result__ ' => 3 ,
389+ '__api_max_results__ ' => 6 ,
378390 ],
379391 ]);
380392
@@ -392,6 +404,12 @@ public function testGetResultWithExecuteOptions(): void
392404 ],
393405 ],
394406 ],
407+ [
408+ '$addFields ' => [
409+ '__api_first_result__ ' => ['$literal ' => 3 ],
410+ '__api_max_results__ ' => ['$literal ' => 6 ],
411+ ],
412+ ],
395413 ]);
396414
397415 $ paginationExtension = new PaginationExtension (
@@ -407,43 +425,52 @@ public function testGetResultWithExecuteOptions(): void
407425
408426 private function mockAggregationBuilder (int $ expectedOffset , int $ expectedLimit ): ObjectProphecy
409427 {
410- $ skipProphecy = $ this ->prophesize (Skip::class);
411- if ($ expectedLimit > 0 ) {
412- $ skipProphecy ->limit ($ expectedLimit )->shouldBeCalled ();
413- } else {
414- $ matchProphecy = $ this ->prophesize (AggregationMatch::class);
415- $ matchProphecy ->field (Paginator::LIMIT_ZERO_MARKER_FIELD )->shouldBeCalled ()->willReturn ($ matchProphecy ->reveal ());
416- $ matchProphecy ->equals (Paginator::LIMIT_ZERO_MARKER )->shouldBeCalled ()->willReturn ($ matchProphecy ->reveal ());
417- $ skipProphecy ->match ()->shouldBeCalled ()->willReturn ($ matchProphecy ->reveal ());
418- }
419-
420- $ resultsAggregationBuilderProphecy = $ this ->prophesize (Builder::class);
421- $ resultsAggregationBuilderProphecy ->skip ($ expectedOffset )->shouldBeCalled ()->willReturn ($ skipProphecy ->reveal ());
422-
423428 $ countProphecy = $ this ->prophesize (Count::class);
424-
425429 $ countAggregationBuilderProphecy = $ this ->prophesize (Builder::class);
426430 $ countAggregationBuilderProphecy ->count ('count ' )->shouldBeCalled ()->willReturn ($ countProphecy ->reveal ());
427431
428432 $ repositoryProphecy = $ this ->prophesize (DocumentRepository::class);
429- $ repositoryProphecy ->createAggregationBuilder ()->shouldBeCalled ()->willReturn (
430- $ resultsAggregationBuilderProphecy ->reveal (),
431- $ countAggregationBuilderProphecy ->reveal ()
432- );
433433
434434 $ objectManagerProphecy = $ this ->prophesize (DocumentManager::class);
435435 $ objectManagerProphecy ->getRepository ('Foo ' )->shouldBeCalled ()->willReturn ($ repositoryProphecy ->reveal ());
436436
437437 $ this ->managerRegistryProphecy ->getManagerForClass ('Foo ' )->shouldBeCalled ()->willReturn ($ objectManagerProphecy ->reveal ());
438438
439439 $ facetProphecy = $ this ->prophesize (Facet::class);
440- $ facetProphecy ->pipeline ($ skipProphecy )->shouldBeCalled ()->willReturn ($ facetProphecy );
441- $ facetProphecy ->pipeline ($ countProphecy )->shouldBeCalled ()->willReturn ($ facetProphecy );
442- $ facetProphecy ->field ('count ' )->shouldBeCalled ()->willReturn ($ facetProphecy );
443- $ facetProphecy ->field ('results ' )->shouldBeCalled ()->willReturn ($ facetProphecy );
440+ $ addFieldsProphecy = $ this ->prophesize (AddFields::class);
441+
442+ if ($ expectedLimit > 0 ) {
443+ $ resultsAggregationBuilderProphecy = $ this ->prophesize (Builder::class);
444+ $ repositoryProphecy ->createAggregationBuilder ()->shouldBeCalled ()->willReturn (
445+ $ resultsAggregationBuilderProphecy ->reveal (),
446+ $ countAggregationBuilderProphecy ->reveal ()
447+ );
448+
449+ $ skipProphecy = $ this ->prophesize (Skip::class);
450+ $ skipProphecy ->limit ($ expectedLimit )->shouldBeCalled ()->willReturn ($ skipProphecy ->reveal ());
451+ $ resultsAggregationBuilderProphecy ->skip ($ expectedOffset )->shouldBeCalled ()->willReturn ($ skipProphecy ->reveal ());
452+ $ facetProphecy ->field ('results ' )->shouldBeCalled ()->willReturn ($ facetProphecy );
453+ $ facetProphecy ->pipeline ($ skipProphecy )->shouldBeCalled ()->willReturn ($ facetProphecy ->reveal ());
454+ } else {
455+ $ repositoryProphecy ->createAggregationBuilder ()->shouldBeCalled ()->willReturn (
456+ $ countAggregationBuilderProphecy ->reveal ()
457+ );
458+
459+ $ addFieldsProphecy ->field ('results ' )->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
460+ $ addFieldsProphecy ->literal ([])->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
461+ }
462+
463+ $ facetProphecy ->field ('count ' )->shouldBeCalled ()->willReturn ($ facetProphecy ->reveal ());
464+ $ facetProphecy ->pipeline ($ countProphecy )->shouldBeCalled ()->willReturn ($ facetProphecy ->reveal ());
465+
466+ $ addFieldsProphecy ->field ('__api_first_result__ ' )->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
467+ $ addFieldsProphecy ->literal ($ expectedOffset )->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
468+ $ addFieldsProphecy ->field ('__api_max_results__ ' )->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
469+ $ addFieldsProphecy ->literal ($ expectedLimit )->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
444470
445471 $ aggregationBuilderProphecy = $ this ->prophesize (Builder::class);
446472 $ aggregationBuilderProphecy ->facet ()->shouldBeCalled ()->willReturn ($ facetProphecy ->reveal ());
473+ $ aggregationBuilderProphecy ->addFields ()->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
447474
448475 return $ aggregationBuilderProphecy ;
449476 }
0 commit comments