From c9f0531dd76ffb53c8ab3891100fa02759137c59 Mon Sep 17 00:00:00 2001 From: butschster Date: Sat, 22 Apr 2023 12:26:38 +0400 Subject: [PATCH 1/3] Fixes problem when paginator doesn't calculate `countPages` correctly in constructor fixed #900 --- src/Pagination/src/Paginator.php | 8 ++++--- src/Pagination/tests/PaginatorTest.php | 31 ++++++++++++++++---------- 2 files changed, 24 insertions(+), 15 deletions(-) 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..e4bb7c8eb 100644 --- a/src/Pagination/tests/PaginatorTest.php +++ b/src/Pagination/tests/PaginatorTest.php @@ -12,20 +12,20 @@ class PaginatorTest extends TestCase { public function testInterfaces() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $this->assertInstanceOf(PaginatorInterface::class, $paginator); } public function testParameterTracking() { - $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() { - $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 testLimitWithCounts() + { + $paginator = new Paginator(limit: 25, count: 100); + + $this->assertSame(100, $paginator->count()); + $this->assertSame(4, $paginator->countPages()); + } + public function testCountsAndPages() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $this->assertSame(0, $paginator->count()); $this->assertSame($paginator->count(), $paginator->count()); @@ -49,7 +57,7 @@ public function testCountsAndPages() public function testFirstPage() { - $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() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(110); $this->assertSame(110, $paginator->count()); @@ -91,7 +98,7 @@ public function testSecondPage() public function testLastPage() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); $this->assertSame(1, $paginator->getPage()); @@ -116,7 +123,7 @@ public function testLastPage() public function testNegativePage() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); $paginator = $paginator->withPage(-1); @@ -129,7 +136,7 @@ public function testNegativePage() public function testNegativeCount() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(-100); $paginator = $paginator->withPage(-10); @@ -146,7 +153,7 @@ public function testNegativeCount() public function testLastPageNumber() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(110); $this->assertSame(110, $paginator->count()); @@ -166,7 +173,7 @@ public function testLastPageNumber() public function testIsRequired() { - $paginator = new Paginator(25); + $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(24); $this->assertFalse($paginator->isRequired()); From 3c124bb0b3204ee98b5ba4a74f5d6f3d2ee815dc Mon Sep 17 00:00:00 2001 From: butschster Date: Sat, 22 Apr 2023 12:29:55 +0400 Subject: [PATCH 2/3] Updates changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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** From 14842b50c2eca04eea60d345a0f62d67c210ae60 Mon Sep 17 00:00:00 2001 From: butschster Date: Tue, 25 Apr 2023 21:05:50 +0400 Subject: [PATCH 3/3] Adds return types in unit tests --- src/Pagination/tests/PaginatorTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Pagination/tests/PaginatorTest.php b/src/Pagination/tests/PaginatorTest.php index e4bb7c8eb..785702ddb 100644 --- a/src/Pagination/tests/PaginatorTest.php +++ b/src/Pagination/tests/PaginatorTest.php @@ -10,20 +10,20 @@ class PaginatorTest extends TestCase { - public function testInterfaces() + public function testInterfaces(): void { $paginator = new Paginator(limit: 25); $this->assertInstanceOf(PaginatorInterface::class, $paginator); } - public function testParameterTracking() + public function testParameterTracking(): void { $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(limit: 25); @@ -33,7 +33,7 @@ public function testLimit() $this->assertSame(50, $newPaginator->getLimit()); } - public function testLimitWithCounts() + public function testLimitWithCounts(): void { $paginator = new Paginator(limit: 25, count: 100); @@ -41,7 +41,7 @@ public function testLimitWithCounts() $this->assertSame(4, $paginator->countPages()); } - public function testCountsAndPages() + public function testCountsAndPages(): void { $paginator = new Paginator(limit: 25); @@ -55,7 +55,7 @@ public function testCountsAndPages() $this->assertSame(0, $paginator->countDisplayed()); } - public function testFirstPage() + public function testFirstPage(): void { $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); @@ -74,7 +74,7 @@ public function testFirstPage() $this->assertSame(25, $paginator->countDisplayed()); } - public function testSecondPage() + public function testSecondPage(): void { $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(110); @@ -96,7 +96,7 @@ public function testSecondPage() $this->assertSame(25, $paginator->countDisplayed()); } - public function testLastPage() + public function testLastPage(): void { $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); @@ -121,7 +121,7 @@ public function testLastPage() $this->assertSame(25, $paginator->countDisplayed()); } - public function testNegativePage() + public function testNegativePage(): void { $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(100); @@ -134,7 +134,7 @@ public function testNegativePage() $this->assertSame($paginator->count(), count($paginator)); } - public function testNegativeCount() + public function testNegativeCount(): void { $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(-100); @@ -151,7 +151,7 @@ public function testNegativeCount() $this->assertSame(0, $paginator->countDisplayed()); } - public function testLastPageNumber() + public function testLastPageNumber(): void { $paginator = new Paginator(limit: 25); $paginator = $paginator->withCount(110); @@ -171,7 +171,7 @@ public function testLastPageNumber() $this->assertSame(10, $paginator->countDisplayed()); } - public function testIsRequired() + public function testIsRequired(): void { $paginator = new Paginator(limit: 25);