From a6d7179ee686f99560091fadc8f85176947cc862 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Costa?= Date: Mon, 15 Apr 2024 16:52:29 +0200 Subject: [PATCH] refactor,test(add-paging-to-result-set): added missing tests, refactored ResultSet class, reverted PHPStan configuration --- phpstan.neon | 2 +- src/ResultSet.php | 8 +++++--- tests/ResultSetTest.php | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 07a3a97..d3bd084 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -11,7 +11,7 @@ parameters: - src scanDirectories: - - /usr/share/icinga-php/vendor + - vendor ignoreErrors: - diff --git a/src/ResultSet.php b/src/ResultSet.php index 74b2aec..0e633fd 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -62,8 +62,8 @@ public function getCurrentPage(): int { if ($this->pageSize) { $offset = $this->offset ?: 0; - $position = $this->position + 1; - if ($position && ($position + $offset) > $this->pageSize) { + $position = ($this->position ?: 0) + 1; + if (($position + $offset) > $this->pageSize) { // we are not on the first page anymore, calculating proper page return intval(ceil(($position + $offset) / $this->pageSize)); } @@ -79,6 +79,7 @@ public function getCurrentPage(): int * Set the amount of entries a page should contain (needed for pagination) * * @param ?int $size entries per page + * * @return $this */ public function setPageSize(?int $size) @@ -103,7 +104,7 @@ public static function fromQuery(Query $query) /** * Do not cache query result * - * {@see self::class class} instance can only be iterated once + * ResultSet instance can only be iterated once * * @return $this */ @@ -219,6 +220,7 @@ protected function advance() * Yield entry from dataset * * @param Traversable $traversable + * * @return Generator */ protected function yieldTraversable(Traversable $traversable) diff --git a/tests/ResultSetTest.php b/tests/ResultSetTest.php index 78b1bc5..1baba56 100644 --- a/tests/ResultSetTest.php +++ b/tests/ResultSetTest.php @@ -120,9 +120,45 @@ public function testResultPagingWithoutPageSize() $set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g']))); - foreach ($set as $item) { + foreach ($set as $_) { // this raises an exception as no page size has been set $set->getCurrentPage(); } } + + public function testResultPagingWithOffset() + { + $set = (new ResultSet(new ArrayIterator(['d', 'e', 'f', 'g', 'h', 'i', 'j']), null, 3)) + ->setPageSize(2); + + $count = 0; + foreach ($set as $_) { + ++$count; + + $offsetCount = $count + 3; + if ($offsetCount % 2 === 0) { + // a multiple of two, page should equal to offsetCount / 2 + $this->assertEquals( + $set->getCurrentPage(), + $offsetCount / 2 + ); + } elseif ($offsetCount % 2 === 1) { + $this->assertEquals( + $set->getCurrentPage(), + intval(ceil($offsetCount / 2)) + ); + } + } + } + + public function testResultPagingBeforeIteration() + { + $set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g']))) + ->setPageSize(2); + + $this->assertEquals( + $set->getCurrentPage(), + 1 + ); + } }