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

Commit

Permalink
fix: fixes offset calculation for paginated Doctrine collections
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weierophinney committed Feb 11, 2019
1 parent fbd05cf commit fe80d5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ResourceGenerator/ExtractCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ private function extractDoctrinePaginator(
return $this->createPaginatedCollectionResource(
$pageCount,
$data,
function (int $page) use ($query, $pageCount) {
$query->setFirstResult($pageCount * ($page - 1));
function (int $page) use ($query, $perPage) {
$query->setFirstResult($perPage * ($page - 1));
},
$collection,
$metadata,
Expand Down
6 changes: 6 additions & 0 deletions test/ResourceGenerator/DoctrinePaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ public function testCreatesLinksForQueryBasedPagination()
->method('getMaxResults')
->with()
->willReturn(15);
$query->expects($this->once())
->method('setFirstResult')
->with(30);
$this->paginator->getQuery()->willReturn($query);
$this->paginator->count()->willReturn(100);

Expand Down Expand Up @@ -218,6 +221,9 @@ public function testCreatesLinksForRouteBasedPagination()
->method('getMaxResults')
->with()
->willReturn(15);
$query->expects($this->once())
->method('setFirstResult')
->with(30);
$this->paginator->getQuery()->willReturn($query);
$this->paginator->count()->willReturn(100);

Expand Down

0 comments on commit fe80d5d

Please sign in to comment.