Skip to content

Commit

Permalink
Add ability to use simplePaginate with Laravel Scout. (#443)
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Jan 13, 2021
1 parent bcc1ee3 commit 1218be8
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,43 @@ public function get()
* @param int $perPage
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginate($perPage = null, $pageName = 'page', $page = null)
{
$engine = $this->engine();

$page = $page ?: Paginator::resolveCurrentPage($pageName);

$perPage = $perPage ?: $this->model->getPerPage();

$results = $this->model->newCollection($engine->map(
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
)->all());

$total = $engine->getTotalCount($rawResults);

$hasMorePages = ($perPage * $page) < $engine->getTotalCount($rawResults);

$paginator = Container::getInstance()->makeWith(Paginator::class, [
'items' => $results,
'perPage' => $perPage,
'currentPage' => $page,
'options' => [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
],
])->hasMorePagesWhen($hasMorePages);

return $paginator->appends('query', $this->query);
}

/**
* Paginate the given query into a paginator.
*
* @param int $perPage
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = null, $pageName = 'page', $page = null)
Expand Down
83 changes: 80 additions & 3 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,89 @@ public function test_pagination_correctly_handles_paginated_results()
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());

$engine->shouldReceive('paginate');
$engine->shouldReceive('map')->andReturn($results = Collection::make([new stdClass]));
$engine->shouldReceive('getTotalCount');
$engine->shouldReceive('map')->andReturn($results = Collection::times(15, function () {
return new stdClass;
}));
$engine->shouldReceive('getTotalCount')->andReturn(16);

$model->shouldReceive('newCollection')->andReturn($results);

$builder->paginate();
$paginated = $builder->paginate();

$this->assertSame($results->all(), $paginated->items());
$this->assertSame(16, $paginated->total());
$this->assertSame(15, $paginated->perPage());
$this->assertSame(1, $paginated->currentPage());
$this->assertSame([
'path' => 'http://localhost/foo',
'pageName' => 'page',
], $paginated->getOptions());
}

public function test_simple_pagination_correctly_handles_paginated_results()
{
Paginator::currentPageResolver(function () {
return 1;
});
Paginator::currentPathResolver(function () {
return 'http://localhost/foo';
});

$builder = new Builder($model = m::mock(), 'zonda');
$model->shouldReceive('getPerPage')->andReturn(15);
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());

$engine->shouldReceive('paginate');
$engine->shouldReceive('map')->andReturn($results = Collection::times(15, function () {
return new stdClass;
}));
$engine->shouldReceive('getTotalCount')->andReturn(16);

$model->shouldReceive('newCollection')->andReturn($results);

$paginated = $builder->simplePaginate();

$this->assertSame($results->all(), $paginated->items());
$this->assertTrue($paginated->hasMorePages());
$this->assertSame(15, $paginated->perPage());
$this->assertSame(1, $paginated->currentPage());
$this->assertSame([
'path' => 'http://localhost/foo',
'pageName' => 'page',
], $paginated->getOptions());
}

public function test_simple_pagination_correctly_handles_paginated_results_without_more_pages()
{
Paginator::currentPageResolver(function () {
return 1;
});
Paginator::currentPathResolver(function () {
return 'http://localhost/foo';
});

$builder = new Builder($model = m::mock(), 'zonda');
$model->shouldReceive('getPerPage')->andReturn(15);
$model->shouldReceive('searchableUsing')->andReturn($engine = m::mock());

$engine->shouldReceive('paginate');
$engine->shouldReceive('map')->andReturn($results = Collection::times(10, function () {
return new stdClass;
}));
$engine->shouldReceive('getTotalCount')->andReturn(10);

$model->shouldReceive('newCollection')->andReturn($results);

$paginated = $builder->simplePaginate();

$this->assertSame($results->all(), $paginated->items());
$this->assertFalse($paginated->hasMorePages());
$this->assertSame(15, $paginated->perPage());
$this->assertSame(1, $paginated->currentPage());
$this->assertSame([
'path' => 'http://localhost/foo',
'pageName' => 'page',
], $paginated->getOptions());
}

public function test_macroable()
Expand Down

0 comments on commit 1218be8

Please sign in to comment.