Skip to content

Commit

Permalink
[8.x] Handle cursor paginator when no items are found (#42963)
Browse files Browse the repository at this point in the history
* handle cursor paginator when no items are found

* StyleCI suugested fix
  • Loading branch information
rodrigopedra authored Jun 27, 2022
1 parent e630199 commit b266b75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Illuminate/Pagination/AbstractCursorPaginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public function previousCursor()
return null;
}

if ($this->items->isEmpty()) {
return null;
}

return $this->getCursorForItem($this->items->first(), false);
}

Expand All @@ -170,6 +174,10 @@ public function nextCursor()
return null;
}

if ($this->items->isEmpty()) {
return null;
}

return $this->getCursorForItem($this->items->last(), true);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Pagination/CursorPaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Support\Collection;
use PHPUnit\Framework\TestCase;

class CursorPaginatorTest extends TestCase
Expand Down Expand Up @@ -76,6 +77,26 @@ public function testCanTransformPaginatorItems()
$this->assertSame([['id' => 6], ['id' => 7]], $p->items());
}

public function testReturnEmptyCursorWhenItemsAreEmpty()
{
$cursor = new Cursor(['id' => 25], true);

$p = new CursorPaginator(Collection::make(), 25, $cursor, [
'path' => 'http://website.com/test',
'cursorName' => 'cursor',
'parameters' => ['id'],
]);

$this->assertInstanceOf(CursorPaginator::class, $p);
$this->assertSame([
'data' => [],
'path' => 'http://website.com/test',
'per_page' => 25,
'next_page_url' => null,
'prev_page_url' => null,
], $p->toArray());
}

protected function getCursor($params, $isNext = true)
{
return (new Cursor($params, $isNext))->encode();
Expand Down

0 comments on commit b266b75

Please sign in to comment.