Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit fe80d5d

Browse files
committed
fix: fixes offset calculation for paginated Doctrine collections
The code was erroneously using the calculated `$pageCount`, which represented the total number of pages in the collection, instead of the `$perPage` value when calculating the result offset. This was due to missing assertions in the unit tests. The assertions have been added, and the code corrected accordingly.
1 parent fbd05cf commit fe80d5d

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/ResourceGenerator/ExtractCollectionTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ private function extractDoctrinePaginator(
118118
return $this->createPaginatedCollectionResource(
119119
$pageCount,
120120
$data,
121-
function (int $page) use ($query, $pageCount) {
122-
$query->setFirstResult($pageCount * ($page - 1));
121+
function (int $page) use ($query, $perPage) {
122+
$query->setFirstResult($perPage * ($page - 1));
123123
},
124124
$collection,
125125
$metadata,

test/ResourceGenerator/DoctrinePaginatorTest.php

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ public function testCreatesLinksForQueryBasedPagination()
153153
->method('getMaxResults')
154154
->with()
155155
->willReturn(15);
156+
$query->expects($this->once())
157+
->method('setFirstResult')
158+
->with(30);
156159
$this->paginator->getQuery()->willReturn($query);
157160
$this->paginator->count()->willReturn(100);
158161

@@ -218,6 +221,9 @@ public function testCreatesLinksForRouteBasedPagination()
218221
->method('getMaxResults')
219222
->with()
220223
->willReturn(15);
224+
$query->expects($this->once())
225+
->method('setFirstResult')
226+
->with(30);
221227
$this->paginator->getQuery()->willReturn($query);
222228
$this->paginator->count()->willReturn(100);
223229

0 commit comments

Comments
 (0)