Skip to content

Commit

Permalink
test: add tests for zero proximity pager, empty pager, and test curre…
Browse files Browse the repository at this point in the history
…nt count for all pagers (#12)
  • Loading branch information
priyadi authored Apr 2, 2024
1 parent e99893e commit 4e0e5a6
Show file tree
Hide file tree
Showing 10 changed files with 513 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.5.3

* test: add tests for zero proximity pager, empty pager, and test current count for all
pagers
* fix(`OffsetPage`): fix `OutOfBoundsException` on an empty first page.

## 0.5.2

* refactor: `PagerItemInterface` methods now returns itself, instead of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getPageable(): PageableInterface
private function getResult(): array
{
if ($this->result !== null) {
if (\count($this->result) === 0) {
if (\count($this->result) === 0 && $this->pageNumber !== 1) {
throw new OutOfBoundsException('The page does not exist.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function testLastPage(string $pageableGeneratorClass): void
currentPageNumber: 21,
previousPageNumbers: [17, 18, 19, 20],
nextPageNumbers: [],
currentCount: 3,
);
}
}
51 changes: 51 additions & 0 deletions tests/src/IntegrationTests/Pager/EmptyPagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/rekapager package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Rekapager\Tests\IntegrationTests\Pager;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Rekalogika\Rekapager\Tests\IntegrationTests\DataProvider\PageableGeneratorProvider;

class EmptyPagerTest extends PagerTestCase
{
protected function getSetName(): string
{
return 'empty';
}

#[DataProviderExternal(PageableGeneratorProvider::class, 'all')]
public function testFirstPage(string $pageableGeneratorClass): void
{
$pageable = $this->createPageableFromGenerator($pageableGeneratorClass);
$pager = $this->createPagerFromPageable($pageable);

$this->assertPager(
$pager,
proximity: 2,
hasPrevious: false,
hasNext: false,
hasFirst: false,
hasLast: false,
hasGapToFirstPage: false,
hasGapToLastPage: false,
numOfPreviousNeighboringPages: 0,
numOfNextNeighboringPages: 0,
firstPageNumber: null,
lastPageNumber: null,
currentPageNumber: 1,
previousPageNumbers: [],
nextPageNumbers: [],
currentCount: 0,
);
}
}
56 changes: 56 additions & 0 deletions tests/src/IntegrationTests/Pager/EmptyZeroProximityPagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/rekapager package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* For the full copyright and license information, please view the LICENSE file
* that was distributed with this source code.
*/

namespace Rekalogika\Rekapager\Tests\IntegrationTests\Pager;

use PHPUnit\Framework\Attributes\DataProviderExternal;
use Rekalogika\Rekapager\Tests\IntegrationTests\DataProvider\PageableGeneratorProvider;

class EmptyZeroProximityPagerTest extends PagerTestCase
{
protected function getSetName(): string
{
return 'empty';
}

protected function getProximity(): int
{
return 0;
}

#[DataProviderExternal(PageableGeneratorProvider::class, 'all')]
public function testFirstPage(string $pageableGeneratorClass): void
{
$pageable = $this->createPageableFromGenerator($pageableGeneratorClass);
$pager = $this->createPagerFromPageable($pageable);

$this->assertPager(
$pager,
proximity: 0,
hasPrevious: false,
hasNext: false,
hasFirst: false,
hasLast: false,
hasGapToFirstPage: false,
hasGapToLastPage: false,
numOfPreviousNeighboringPages: 0,
numOfNextNeighboringPages: 0,
firstPageNumber: null,
lastPageNumber: null,
currentPageNumber: 1,
previousPageNumbers: [],
nextPageNumbers: [],
currentCount: 0,
);
}
}
12 changes: 12 additions & 0 deletions tests/src/IntegrationTests/Pager/KeysetPagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function testFirstPage(string $pageableGeneratorClass): void
currentPageNumber: 1,
previousPageNumbers: [],
nextPageNumbers: [2, 3, 4, 5],
currentCount: 5,
);
}

Expand All @@ -66,6 +67,7 @@ public function testSecondPage(string $pageableGeneratorClass): void
currentPageNumber: 2,
previousPageNumbers: [],
nextPageNumbers: [3, 4, 5],
currentCount: 5,
);
}

Expand All @@ -92,6 +94,7 @@ public function testThirdPage(string $pageableGeneratorClass): void
currentPageNumber: 3,
previousPageNumbers: [2],
nextPageNumbers: [4, 5],
currentCount: 5,
);
}

Expand All @@ -118,6 +121,7 @@ public function testFourthPage(string $pageableGeneratorClass): void
currentPageNumber: 4,
previousPageNumbers: [2, 3],
nextPageNumbers: [5, 6],
currentCount: 5,
);
}

Expand All @@ -144,6 +148,7 @@ public function testFifthPage(string $pageableGeneratorClass): void
currentPageNumber: 5,
previousPageNumbers: [2, 3, 4],
nextPageNumbers: [6, 7],
currentCount: 5,
);
}

Expand All @@ -170,6 +175,7 @@ public function testSixthPage(string $pageableGeneratorClass): void
currentPageNumber: 6,
previousPageNumbers: [4, 5],
nextPageNumbers: [7, 8],
currentCount: 5,
);
}

Expand All @@ -196,6 +202,7 @@ public function testLastPage(string $pageableGeneratorClass): void
currentPageNumber: -1,
previousPageNumbers: [-5, -4, -3, -2],
nextPageNumbers: [],
currentCount: 5,
);
}

Expand All @@ -222,6 +229,7 @@ public function testSecondLastPage(string $pageableGeneratorClass): void
currentPageNumber: -2,
previousPageNumbers: [-5, -4, -3,],
nextPageNumbers: [],
currentCount: 5,
);
}

Expand All @@ -248,6 +256,7 @@ public function testThirdLastPage(string $pageableGeneratorClass): void
currentPageNumber: -3,
previousPageNumbers: [-5, -4],
nextPageNumbers: [-2],
currentCount: 5,
);
}

Expand All @@ -274,6 +283,7 @@ public function testFourthLastPage(string $pageableGeneratorClass): void
currentPageNumber: -4,
previousPageNumbers: [-6, -5],
nextPageNumbers: [-3, -2],
currentCount: 5,
);
}

Expand All @@ -300,6 +310,7 @@ public function testFifthLastPage(string $pageableGeneratorClass): void
currentPageNumber: -5,
previousPageNumbers: [-7, -6],
nextPageNumbers: [-4, -3, -2],
currentCount: 5,
);
}

Expand All @@ -326,6 +337,7 @@ public function testSixthLastPage(string $pageableGeneratorClass): void
currentPageNumber: -6,
previousPageNumbers: [-8, -7],
nextPageNumbers: [-5, -4],
currentCount: 5,
);
}
}
Loading

0 comments on commit 4e0e5a6

Please sign in to comment.