Skip to content

Commit

Permalink
feat,refactor(add-paging-to-result-set): added test cases for ResultS…
Browse files Browse the repository at this point in the history
…et paging, fixed PHPStan configuration, rebuilt baseline, refactored return type annotation
  • Loading branch information
ncosta-ic committed Apr 15, 2024
1 parent 3c75a3a commit 795acfb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
30 changes: 5 additions & 25 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,12 @@ parameters:
path: src/Resolver.php

-
message: "#^Class ipl\\\\Orm\\\\ResultSet implements generic interface Iterator but does not specify its types\\: TKey, TValue$#"
message: "#^Cannot assign offset mixed to ArrayIterator\\.$#"
count: 1
path: src/ResultSet.php

-
message: "#^Method ipl\\\\Orm\\\\ResultSet\\:\\:__construct\\(\\) has parameter \\$limit with no type specified\\.$#"
message: "#^Class ipl\\\\Orm\\\\ResultSet implements generic interface Iterator but does not specify its types\\: TKey, TValue$#"
count: 1
path: src/ResultSet.php

Expand All @@ -776,17 +776,7 @@ parameters:
path: src/ResultSet.php

-
message: "#^Method ipl\\\\Orm\\\\ResultSet\\:\\:hasMore\\(\\) has no return type specified\\.$#"
count: 1
path: src/ResultSet.php

-
message: "#^Method ipl\\\\Orm\\\\ResultSet\\:\\:hasResult\\(\\) has no return type specified\\.$#"
count: 1
path: src/ResultSet.php

-
message: "#^Method ipl\\\\Orm\\\\ResultSet\\:\\:yieldTraversable\\(\\) has no return type specified\\.$#"
message: "#^Method ipl\\\\Orm\\\\ResultSet\\:\\:key\\(\\) should return int\\|null but returns mixed\\.$#"
count: 1
path: src/ResultSet.php

Expand All @@ -796,22 +786,12 @@ parameters:
path: src/ResultSet.php

-
message: "#^Property ipl\\\\Orm\\\\ResultSet\\:\\:\\$cache has no type specified\\.$#"
count: 1
path: src/ResultSet.php

-
message: "#^Property ipl\\\\Orm\\\\ResultSet\\:\\:\\$generator has no type specified\\.$#"
count: 1
path: src/ResultSet.php

-
message: "#^Property ipl\\\\Orm\\\\ResultSet\\:\\:\\$limit has no type specified\\.$#"
message: "#^Parameter \\#1 \\$offset of method ArrayIterator\\<\\(int\\|string\\),mixed\\>\\:\\:seek\\(\\) expects int, mixed given\\.$#"
count: 1
path: src/ResultSet.php

-
message: "#^Property ipl\\\\Orm\\\\ResultSet\\:\\:\\$position has no type specified\\.$#"
message: "#^Property ipl\\\\Orm\\\\ResultSet\\:\\:\\$cache with generic class ArrayIterator does not specify its types\\: TKey, TValue$#"
count: 1
path: src/ResultSet.php

Expand Down
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:
- vendor
- /usr/share/icinga-php/vendor

ignoreErrors:
-
Expand Down
17 changes: 12 additions & 5 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ResultSet implements Iterator
protected $pageSize;

/**
* Construct the {@see self::class class} object
* Construct the ResultSet object
*
* @param Traversable $traversable
* @param ?int $limit
Expand All @@ -62,9 +62,10 @@ public function getCurrentPage(): int
{
if ($this->pageSize) {
$offset = $this->offset ?: 0;
if ($this->position && ($this->position + $offset) > $this->pageSize) {
$position = $this->position + 1;
if ($position && ($position + $offset) > $this->pageSize) {
// we are not on the first page anymore, calculating proper page
return intval(floor(($this->position + $offset) / $this->pageSize));
return intval(ceil(($position + $offset) / $this->pageSize));
}

// still on the first page
Expand All @@ -77,7 +78,7 @@ public function getCurrentPage(): int
/**
* Set the amount of entries a page should contain (needed for pagination)
*
* @param ?int $size entries per page
* @param ?int $size entries per page
* @return $this
*/
public function setPageSize(?int $size)
Expand Down Expand Up @@ -157,7 +158,13 @@ public function next(): void
}
}

public function key(): ?int
/**
* Return the current item's key
*
* @return ?int
*/
#[\ReturnTypeWillChange]
public function key()
{
if ($this->position === null) {
$this->advance();
Expand Down
44 changes: 44 additions & 0 deletions tests/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ipl\Tests\Orm;

use ArrayIterator;
use BadMethodCallException;
use ipl\Orm\ResultSet;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -81,4 +82,47 @@ public function testResultWithCacheEnabledWithLimit()
['a', 'b', 'a', 'b']
);
}

public function testResultPaging()
{
$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g'])))
->setPageSize(2);

$count = 0;
foreach ($set as $item) {
++$count;

if ($count > 2) {
if ($count % 2 === 0) {
// a multiple of two, page should equal to count / 2
$this->assertEquals(
$set->getCurrentPage(),
$count / 2
);
} elseif ($count % 2 === 1) {
$this->assertEquals(
$set->getCurrentPage(),
intval(ceil($count / 2))
);
}
} else {
$this->assertEquals(
$set->getCurrentPage(),
1
);
}
}
}

public function testResultPagingWithoutPageSize()
{
$this->expectException(BadMethodCallException::class);

$set = (new ResultSet(new ArrayIterator(['a', 'b', 'c', 'd', 'e', 'f', 'g'])));

foreach ($set as $item) {
// this raises an exception as no page size has been set
$set->getCurrentPage();
}
}
}

0 comments on commit 795acfb

Please sign in to comment.