13
13
14
14
namespace ApiPlatform \Doctrine \Orm \Tests ;
15
15
16
+ use ApiPlatform \Doctrine \Orm \Extension \DoctrinePaginatorFactory ;
16
17
use ApiPlatform \Doctrine \Orm \Paginator ;
17
18
use ApiPlatform \Doctrine \Orm \Tests \Fixtures \Query ;
18
19
use ApiPlatform \Metadata \Exception \InvalidArgumentException ;
20
+ use Doctrine \Common \Collections \ArrayCollection ;
19
21
use Doctrine \ORM \Tools \Pagination \Paginator as DoctrinePaginator ;
20
22
use PHPUnit \Framework \TestCase ;
23
+ use Prophecy \Argument ;
21
24
use Prophecy \PhpUnit \ProphecyTrait ;
22
25
23
26
class PaginatorTest extends TestCase
@@ -27,13 +30,14 @@ class PaginatorTest extends TestCase
27
30
/**
28
31
* @dataProvider initializeProvider
29
32
*/
30
- public function testInitialize (int $ firstResult , int $ maxResults , int $ totalItems , int $ currentPage , int $ lastPage ): void
33
+ public function testInitialize (int $ firstResult , int $ maxResults , int $ totalItems , int $ currentPage , int $ lastPage, bool $ hasNextPage ): void
31
34
{
32
35
$ paginator = $ this ->getPaginator ($ firstResult , $ maxResults , $ totalItems );
33
36
34
37
$ this ->assertSame ((float ) $ currentPage , $ paginator ->getCurrentPage ());
35
38
$ this ->assertSame ((float ) $ lastPage , $ paginator ->getLastPage ());
36
39
$ this ->assertSame ((float ) $ maxResults , $ paginator ->getItemsPerPage ());
40
+ $ this ->assertSame ($ hasNextPage , $ paginator ->hasNextPage ());
37
41
}
38
42
39
43
public function testInitializeWithQueryFirstResultNotApplied (): void
@@ -90,11 +94,60 @@ private function getPaginatorWithMalformedQuery(bool $maxResults = false): void
90
94
new Paginator ($ doctrinePaginator ->reveal ());
91
95
}
92
96
97
+ public function testHasNextPageShouldNotMakeQueryIfTotalPagesHasBeenCalled (): void
98
+ {
99
+ $ query = $ this ->prophesize (Query::class);
100
+ $ query ->getFirstResult ()->willReturn (1 )->shouldBeCalled ();
101
+ $ query ->getMaxResults ()->willReturn (15 )->shouldBeCalled ();
102
+ $ query ->setMaxResults (1 )->shouldNotBeCalled ();
103
+
104
+ $ doctrinePaginator = $ this ->prophesize (DoctrinePaginator::class);
105
+
106
+ $ doctrinePaginator ->getQuery ()->willReturn ($ query ->reveal ())->shouldBeCalled ();
107
+ $ doctrinePaginator ->count ()->willReturn (42 );
108
+
109
+ $ doctrinePaginator ->getIterator ()->will (fn (): \ArrayIterator => new \ArrayIterator ());
110
+
111
+ $ paginator = new Paginator ($ doctrinePaginator ->reveal ());
112
+ $ paginator ->getTotalItems ();
113
+ $ this ->assertTrue ($ paginator ->hasNextPage ());
114
+ }
115
+
116
+ public function testHasNextPageShouldMakeQueryIfTotalPagesHasNotBeenCalled (): void
117
+ {
118
+ $ query = $ this ->prophesize (Query::class);
119
+ $ query ->getFirstResult ()->willReturn (1 )->shouldBeCalled ();
120
+ $ query ->getMaxResults ()->willReturn (15 )->shouldBeCalled ();
121
+ $ query ->getParameters ()->willReturn (new ArrayCollection ())->shouldBeCalled ();
122
+ $ query ->setParameters (Argument::any ())->willReturn ($ query ->reveal ())->shouldBeCalled ();
123
+ $ query ->setCacheable (false )->willReturn ($ query ->reveal ())->shouldBeCalled ();
124
+ $ query ->setMaxResults (1 )->shouldBeCalled ();
125
+ $ query ->getHints ()->willReturn ([])->shouldBeCalled ();
126
+ $ query ->setFirstResult (Argument::any ())->willReturn ($ query ->reveal ())->shouldBeCalled ();
127
+
128
+ $ doctrinePaginator = $ this ->prophesize (DoctrinePaginator::class);
129
+
130
+ $ doctrinePaginator ->getQuery ()->willReturn ($ query ->reveal ())->shouldBeCalled ();
131
+ $ doctrinePaginator ->count ()->willReturn (42 );
132
+ $ doctrinePaginator ->getFetchJoinCollection ()->willReturn (false );
133
+
134
+ $ doctrinePaginator ->getIterator ()->will (fn (): \ArrayIterator => new \ArrayIterator ());
135
+
136
+ $ secondDoctrinePaginator = $ this ->prophesize (DoctrinePaginator::class);
137
+ $ secondDoctrinePaginator ->getIterator ()->will (fn (): \ArrayIterator => new \ArrayIterator ());
138
+ $ doctrinePaginatorFactory = $ this ->prophesize (DoctrinePaginatorFactory::class);
139
+ $ doctrinePaginatorFactory ->getPaginator (Argument::any (), Argument::any ())->willReturn ($ secondDoctrinePaginator ->reveal ());
140
+
141
+ $ paginator = new Paginator ($ doctrinePaginator ->reveal ());
142
+ $ paginator ->setDoctrinePaginatorFactory ($ doctrinePaginatorFactory ->reveal ());
143
+ $ this ->assertFalse ($ paginator ->hasNextPage ());
144
+ }
145
+
93
146
public static function initializeProvider (): array
94
147
{
95
148
return [
96
- 'First of three pages of 15 items each ' => [0 , 15 , 42 , 1 , 3 ],
97
- 'Second of two pages of 10 items each ' => [10 , 10 , 20 , 2 , 2 ],
149
+ 'First of three pages of 15 items each ' => [0 , 15 , 42 , 1 , 3 , true ],
150
+ 'Second of two pages of 10 items each ' => [10 , 10 , 20 , 2 , 2 , false ],
98
151
];
99
152
}
100
153
}
0 commit comments