Skip to content

Commit

Permalink
refactor,test(add-paging-to-result-set): added missing tests, refacto…
Browse files Browse the repository at this point in the history
…red ResultSet class, reverted PHPStan configuration
  • Loading branch information
ncosta-ic committed Apr 15, 2024
1 parent 795acfb commit a6d7179
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ parameters:
- src

scanDirectories:
- /usr/share/icinga-php/vendor
- vendor

ignoreErrors:
-
Expand Down
8 changes: 5 additions & 3 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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)
Expand All @@ -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
*/
Expand Down Expand Up @@ -219,6 +220,7 @@ protected function advance()
* Yield entry from dataset
*
* @param Traversable $traversable
*
* @return Generator
*/
protected function yieldTraversable(Traversable $traversable)
Expand Down
38 changes: 37 additions & 1 deletion tests/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit a6d7179

Please sign in to comment.