Skip to content

Commit

Permalink
Merge pull request #63 from jingu/fix_nb_results_count_issue
Browse files Browse the repository at this point in the history
fix assertion when $nbResults get 0
  • Loading branch information
koriym committed Mar 16, 2022
2 parents 0287577 + 2993842 commit f878980
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Pagerfanta/AuraSqlQueryAdapter.php
Expand Up @@ -15,6 +15,10 @@
use function is_array;
use function is_int;

/**
* @template T
* @implements AdapterInterface<T>
*/
class AuraSqlQueryAdapter implements AdapterInterface
{
private ExtendedPdoInterface $pdo;
Expand Down Expand Up @@ -45,7 +49,7 @@ public function getNbResults(): int
$sth->execute($this->select->getBindValues());
$result = $sth->fetchColumn();
$nbResults = (int) $result;
assert($nbResults > 0);
assert($nbResults >= 0);
assert(is_int($nbResults));

return $nbResults;
Expand Down
8 changes: 5 additions & 3 deletions src/Pagerfanta/ExtendedPdoAdapter.php
Expand Up @@ -20,6 +20,10 @@

use const PHP_EOL;

/**
* @template T
* @implements AdapterInterface<T>
*/
class ExtendedPdoAdapter implements AdapterInterface
{
private ExtendedPdoInterface $pdo;
Expand All @@ -41,8 +45,6 @@ public function __construct(ExtendedPdoInterface $pdo, string $sql, array $param
/**
* {@inheritdoc}
*
* @phpstan-return positive-int
*
* @SuppressWarnings(PHPMD.GotoStatement)
*/
public function getNbResults(): int
Expand All @@ -66,7 +68,7 @@ public function getNbResults(): int
/** @var string $count */
$nbResult = ! $count ? 0 : (int) $count;
assert(is_int($nbResult));
assert($nbResult > 0);
assert($nbResult >= 0);

return $nbResult;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Pagerfanta/AuraSqlQueryAdapterTest.php
Expand Up @@ -28,6 +28,23 @@ public function testGetNbResultsShouldWorkAfterCallingGetSlice()
$this->doTestGetNbResults($adapter);
}

public function testGetNbResultsShouldReturnZero()
{
$select = clone $this->select;
$select->where('p.username = "_DUMMY_"');

$countQueryBuilderModifier = static function (Select $select) {
foreach (array_keys($select->getCols()) as $key) {
$select->removeCol($key);
}

return $select->cols(['COUNT(*) AS total_results'])->limit(1);
};

$adapter = new AuraSqlQueryAdapter($this->pdo, $select, $countQueryBuilderModifier);
$this->assertSame(0, $adapter->getNbResults());
}

public function testGetSlice()
{
$adapter = $this->createAdapterToTestGetSlice();
Expand Down
6 changes: 6 additions & 0 deletions tests/Pagerfanta/ExtendedPdoAdapterTest.php
Expand Up @@ -23,6 +23,12 @@ public function testGetNbResults()
$this->assertSame(50, $nbResult);
}

public function testGetNbResultsShouldReturnZero()
{
$adapter = new ExtendedPdoAdapter($this->pdo, 'SELECT * FROM posts WHERE posts.username = "_DUMMY_"', []);
$this->assertSame(0, $adapter->getNbResults());
}

public function testGetLimitClause()
{
$limitClause = $this->pdoAdapter->getLimitClause(1, 10);
Expand Down

0 comments on commit f878980

Please sign in to comment.