Skip to content

Commit

Permalink
Taken into account data reader limit in `OffsetPaginator::getTotalIte…
Browse files Browse the repository at this point in the history
…ms()` (#196)
  • Loading branch information
vjik authored Sep 25, 2024
1 parent 076c98b commit 01c7bc5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
- New #176: Add `OrderHelper` (@vjik)
- New #173, #184: Add `$caseSensitive` parameter to `Like` filter to control whether the search must be case-sensitive
or not (@arogachev)
- Enh #187: Limit set in data reader is now taken into account by offset paginator. Keyset paginator throws an exception
in this case (@samdark)
- Enh #187, #196: Limit set in data reader is now taken into account by offset paginator. Keyset paginator throws
an exception in this case (@samdark, @vjik)
- Chg #187: Add `FilterableDataInterface::getFilter()`, `LimitableDataInterface::getLimit()`,
`OffsetableDataInterface::getOffset()` (@samdark)
- Chg #187: `LimitableDataInterface::withLimit()` now accepts `null` to indicate "no limit". `0` is now a valid limit
Expand Down
3 changes: 2 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
},
"GreaterThan": {
"ignoreSourceCodeByRegex": [
".*if \\(\\$dataReaderLimit !== null && \\$this->getOffset\\(\\) \\+ \\$this->pageSize > \\$dataReaderLimit\\) \\{.*"
".*if \\(\\$dataReaderLimit !== null && \\$this->getOffset\\(\\) \\+ \\$this->pageSize > \\$dataReaderLimit\\) \\{.*",
".*if \\(\\$dataReaderLimit !== null && \\$count > \\$dataReaderLimit\\) \\{.*"
]
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/Paginator/OffsetPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ public function getOffset(): int
*/
public function getTotalItems(): int
{
return $this->dataReader->count();
$count = $this->dataReader->count();

$dataReaderLimit = $this->dataReader->getLimit();
if ($dataReaderLimit !== null && $count > $dataReaderLimit) {
return $dataReaderLimit;
}

return $count;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Paginator/OffsetPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,15 @@ public function testWithNulledPageToken(): void
$this->assertFalse($token->isPrevious);
}

public function testLimitedDataReaderTotalItems(): void
{
$dataReader = (new IterableDataReader(self::DEFAULT_DATASET))->withLimit(3);

$paginator = new OffsetPaginator($dataReader);

$this->assertSame(3, $paginator->getTotalItems());
}

public function testLimitedDataReaderReducedPage(): void
{
$dataReader = (new IterableDataReader(self::DEFAULT_DATASET))->withLimit(3);
Expand All @@ -630,6 +639,7 @@ public function testLimitedDataReaderReducedPage(): void
}

$this->assertSame(1, $count);
$this->assertSame(3, $paginator->getTotalItems());
}

public function testLimitedDataReaderEqualPage(): void
Expand Down

0 comments on commit 01c7bc5

Please sign in to comment.