diff --git a/CHANGELOG.md b/CHANGELOG.md index ce7ab9f..7b81381 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/infection.json.dist b/infection.json.dist index 896d140..ab2dd28 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -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\\) \\{.*" ] } } diff --git a/src/Paginator/OffsetPaginator.php b/src/Paginator/OffsetPaginator.php index da719c6..90ffb2c 100644 --- a/src/Paginator/OffsetPaginator.php +++ b/src/Paginator/OffsetPaginator.php @@ -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; } /** diff --git a/tests/Paginator/OffsetPaginatorTest.php b/tests/Paginator/OffsetPaginatorTest.php index 2656fa5..d91f791 100644 --- a/tests/Paginator/OffsetPaginatorTest.php +++ b/tests/Paginator/OffsetPaginatorTest.php @@ -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); @@ -630,6 +639,7 @@ public function testLimitedDataReaderReducedPage(): void } $this->assertSame(1, $count); + $this->assertSame(3, $paginator->getTotalItems()); } public function testLimitedDataReaderEqualPage(): void