diff --git a/CHANGELOG.md b/CHANGELOG.md index d760cd183..db4b83791 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 3.7.1 - 2023-04-21 - **Bug Fixes** - [spiral/filters] Fixed InputScope to allow retrieval of non-bag input sources + - [spiral/pagination] Fixed problem when paginator doesn't calculate `countPages` correctly in constructor ## 3.7.0 - 2023-04-13 - **Medium Impact Changes** diff --git a/src/Pagination/src/Paginator.php b/src/Pagination/src/Paginator.php index ec9c68fb6..32c9d1dd2 100644 --- a/src/Pagination/src/Paginator.php +++ b/src/Pagination/src/Paginator.php @@ -11,12 +11,14 @@ final class Paginator implements PaginatorInterface, \Countable { private int $pageNumber = 1; private int $countPages = 1; + private int $count; public function __construct( private int $limit = 25, - private int $count = 0, - private readonly ?string $parameter = null + int $count = 0, + private readonly ?string $parameter = null, ) { + $this->setCount($count); } /** @@ -129,7 +131,7 @@ public function previousPage(): ?int private function setCount(int $count): self { $this->count = \max($count, 0); - $this->countPages = $this->count > 0 ? (int) \ceil($this->count / $this->limit) : 1; + $this->countPages = $this->count > 0 ? (int)\ceil($this->count / $this->limit) : 1; return $this; } diff --git a/src/Pagination/tests/PaginatorTest.php b/src/Pagination/tests/PaginatorTest.php index 14692dd58..785702ddb 100644 --- a/src/Pagination/tests/PaginatorTest.php +++ b/src/Pagination/tests/PaginatorTest.php @@ -10,22 +10,22 @@ class PaginatorTest extends TestCase { - public function testInterfaces() + public function testInterfaces(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $this->assertInstanceOf(PaginatorInterface::class, $paginator); } - public function testParameterTracking() + public function testParameterTracking(): void { - $paginator = new Paginator(25, 0, 'request:page'); + $paginator = new Paginator(limit: 25, count: 0, parameter: 'request:page'); $this->assertSame('request:page', $paginator->getParameter()); } - public function testLimit() + public function testLimit(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $this->assertSame(25, $paginator->getLimit()); $newPaginator = $paginator->withLimit(50); @@ -33,9 +33,17 @@ public function testLimit() $this->assertSame(50, $newPaginator->getLimit()); } - public function testCountsAndPages() + public function testLimitWithCounts(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25, count: 100); + + $this->assertSame(100, $paginator->count()); + $this->assertSame(4, $paginator->countPages()); + } + + public function testCountsAndPages(): void + { + $paginator = new Paginator(limit: 25); $this->assertSame(0, $paginator->count()); $this->assertSame($paginator->count(), $paginator->count()); @@ -47,9 +55,9 @@ public function testCountsAndPages() $this->assertSame(0, $paginator->countDisplayed()); } - public function testFirstPage() + public function testFirstPage(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); $this->assertSame(1, $paginator->getPage()); @@ -66,10 +74,9 @@ public function testFirstPage() $this->assertSame(25, $paginator->countDisplayed()); } - - public function testSecondPage() + public function testSecondPage(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(110); $this->assertSame(110, $paginator->count()); @@ -89,9 +96,9 @@ public function testSecondPage() $this->assertSame(25, $paginator->countDisplayed()); } - public function testLastPage() + public function testLastPage(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); $this->assertSame(1, $paginator->getPage()); @@ -114,9 +121,9 @@ public function testLastPage() $this->assertSame(25, $paginator->countDisplayed()); } - public function testNegativePage() + public function testNegativePage(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); $paginator = $paginator->withPage(-1); @@ -127,9 +134,9 @@ public function testNegativePage() $this->assertSame($paginator->count(), count($paginator)); } - public function testNegativeCount() + public function testNegativeCount(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(-100); $paginator = $paginator->withPage(-10); @@ -144,9 +151,9 @@ public function testNegativeCount() $this->assertSame(0, $paginator->countDisplayed()); } - public function testLastPageNumber() + public function testLastPageNumber(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(110); $this->assertSame(110, $paginator->count()); @@ -164,9 +171,9 @@ public function testLastPageNumber() $this->assertSame(10, $paginator->countDisplayed()); } - public function testIsRequired() + public function testIsRequired(): void { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(24); $this->assertFalse($paginator->isRequired());