14
14
namespace ApiPlatform \Doctrine \Odm \Tests \Extension ;
15
15
16
16
use ApiPlatform \Doctrine \Odm \Extension \PaginationExtension ;
17
- use ApiPlatform \Doctrine \Odm \Paginator ;
18
17
use ApiPlatform \Doctrine \Odm \Tests \DoctrineMongoDbOdmSetup ;
19
18
use ApiPlatform \Doctrine \Odm \Tests \Fixtures \Document \Dummy ;
20
19
use ApiPlatform \Metadata \Exception \InvalidArgumentException ;
23
22
use ApiPlatform \State \Pagination \PaginatorInterface ;
24
23
use ApiPlatform \State \Pagination \PartialPaginatorInterface ;
25
24
use Doctrine \ODM \MongoDB \Aggregation \Builder ;
25
+ use Doctrine \ODM \MongoDB \Aggregation \Stage \AddFields ;
26
26
use Doctrine \ODM \MongoDB \Aggregation \Stage \Count ;
27
27
use Doctrine \ODM \MongoDB \Aggregation \Stage \Facet ;
28
- use Doctrine \ODM \MongoDB \Aggregation \Stage \MatchStage as AggregationMatch ;
29
28
use Doctrine \ODM \MongoDB \Aggregation \Stage \Skip ;
30
29
use Doctrine \ODM \MongoDB \DocumentManager ;
31
30
use Doctrine \ODM \MongoDB \Iterator \Iterator ;
@@ -42,6 +41,7 @@ class PaginationExtensionTest extends TestCase
42
41
{
43
42
use ProphecyTrait;
44
43
44
+ /** @var ObjectProphecy<ManagerRegistry> */
45
45
private ObjectProphecy $ managerRegistryProphecy ;
46
46
47
47
/**
@@ -322,11 +322,14 @@ public function testGetResult(): void
322
322
$ iteratorProphecy = $ this ->prophesize (Iterator::class);
323
323
$ iteratorProphecy ->toArray ()->willReturn ([
324
324
[
325
+ 'results ' => [],
325
326
'count ' => [
326
327
[
327
328
'count ' => 9 ,
328
329
],
329
330
],
331
+ '__api_first_result__ ' => 3 ,
332
+ '__api_max_results__ ' => 6 ,
330
333
],
331
334
]);
332
335
@@ -344,6 +347,12 @@ public function testGetResult(): void
344
347
],
345
348
],
346
349
],
350
+ [
351
+ '$addFields ' => [
352
+ '__api_first_result__ ' => ['$literal ' => 3 ],
353
+ '__api_max_results__ ' => ['$literal ' => 6 ],
354
+ ],
355
+ ],
347
356
]);
348
357
349
358
$ paginationExtension = new PaginationExtension (
@@ -370,11 +379,14 @@ public function testGetResultWithExecuteOptions(): void
370
379
$ iteratorProphecy = $ this ->prophesize (Iterator::class);
371
380
$ iteratorProphecy ->toArray ()->willReturn ([
372
381
[
382
+ 'results ' => [],
373
383
'count ' => [
374
384
[
375
385
'count ' => 9 ,
376
386
],
377
387
],
388
+ '__api_first_result__ ' => 3 ,
389
+ '__api_max_results__ ' => 6 ,
378
390
],
379
391
]);
380
392
@@ -392,6 +404,12 @@ public function testGetResultWithExecuteOptions(): void
392
404
],
393
405
],
394
406
],
407
+ [
408
+ '$addFields ' => [
409
+ '__api_first_result__ ' => ['$literal ' => 3 ],
410
+ '__api_max_results__ ' => ['$literal ' => 6 ],
411
+ ],
412
+ ],
395
413
]);
396
414
397
415
$ paginationExtension = new PaginationExtension (
@@ -407,43 +425,52 @@ public function testGetResultWithExecuteOptions(): void
407
425
408
426
private function mockAggregationBuilder (int $ expectedOffset , int $ expectedLimit ): ObjectProphecy
409
427
{
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
-
423
428
$ countProphecy = $ this ->prophesize (Count::class);
424
-
425
429
$ countAggregationBuilderProphecy = $ this ->prophesize (Builder::class);
426
430
$ countAggregationBuilderProphecy ->count ('count ' )->shouldBeCalled ()->willReturn ($ countProphecy ->reveal ());
427
431
428
432
$ repositoryProphecy = $ this ->prophesize (DocumentRepository::class);
429
- $ repositoryProphecy ->createAggregationBuilder ()->shouldBeCalled ()->willReturn (
430
- $ resultsAggregationBuilderProphecy ->reveal (),
431
- $ countAggregationBuilderProphecy ->reveal ()
432
- );
433
433
434
434
$ objectManagerProphecy = $ this ->prophesize (DocumentManager::class);
435
435
$ objectManagerProphecy ->getRepository ('Foo ' )->shouldBeCalled ()->willReturn ($ repositoryProphecy ->reveal ());
436
436
437
437
$ this ->managerRegistryProphecy ->getManagerForClass ('Foo ' )->shouldBeCalled ()->willReturn ($ objectManagerProphecy ->reveal ());
438
438
439
439
$ 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 ());
444
470
445
471
$ aggregationBuilderProphecy = $ this ->prophesize (Builder::class);
446
472
$ aggregationBuilderProphecy ->facet ()->shouldBeCalled ()->willReturn ($ facetProphecy ->reveal ());
473
+ $ aggregationBuilderProphecy ->addFields ()->shouldBeCalled ()->willReturn ($ addFieldsProphecy ->reveal ());
447
474
448
475
return $ aggregationBuilderProphecy ;
449
476
}
0 commit comments