Skip to content

Commit

Permalink
Merge pull request #28937 from driesvints/fix-columns-param-on-pagina…
Browse files Browse the repository at this point in the history
…te-method

[5.8] Fix columns parameter on paginate method
  • Loading branch information
taylorotwell authored Jun 24, 2019
2 parents 6f6f6ff + 94403f6 commit 7ac7818
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $p
{
$page = $page ?: Paginator::resolveCurrentPage($pageName);

$total = $this->getCountForPagination($columns);
$total = $this->getCountForPagination();

$results = $total ? $this->forPage($page, $perPage)->get($columns) : collect();

Expand Down
33 changes: 30 additions & 3 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,7 @@ public function testPaginate()

$results = collect([['test' => 'foo'], ['test' => 'bar']]);

$builder->shouldReceive('getCountForPagination')->once()->with($columns)->andReturn(2);
$builder->shouldReceive('getCountForPagination')->once()->andReturn(2);
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
$builder->shouldReceive('get')->once()->andReturn($results);

Expand All @@ -2853,7 +2853,7 @@ public function testPaginateWithDefaultArguments()

$results = collect([['test' => 'foo'], ['test' => 'bar']]);

$builder->shouldReceive('getCountForPagination')->once()->with($columns)->andReturn(2);
$builder->shouldReceive('getCountForPagination')->once()->andReturn(2);
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
$builder->shouldReceive('get')->once()->andReturn($results);

Expand Down Expand Up @@ -2884,7 +2884,7 @@ public function testPaginateWhenNoResults()

$results = [];

$builder->shouldReceive('getCountForPagination')->once()->with($columns)->andReturn(0);
$builder->shouldReceive('getCountForPagination')->once()->andReturn(0);
$builder->shouldNotReceive('forPage');
$builder->shouldNotReceive('get');

Expand All @@ -2904,6 +2904,33 @@ public function testPaginateWhenNoResults()
]), $result);
}

public function testPaginateWithSpecificColumns()
{
$perPage = 16;
$columns = ['id', 'name'];
$pageName = 'page-name';
$page = 1;
$builder = $this->getMockQueryBuilder();
$path = 'http://foo.bar?page=3';

$results = collect([['id' => 3, 'name' => 'Taylor'], ['id' => 5, 'name' => 'Mohamed']]);

$builder->shouldReceive('getCountForPagination')->once()->andReturn(2);
$builder->shouldReceive('forPage')->once()->with($page, $perPage)->andReturnSelf();
$builder->shouldReceive('get')->once()->andReturn($results);

Paginator::currentPathResolver(function () use ($path) {
return $path;
});

$result = $builder->paginate($perPage, $columns, $pageName, $page);

$this->assertEquals(new LengthAwarePaginator($results, 2, $perPage, $page, [
'path' => $path,
'pageName' => $pageName,
]), $result);
}

public function testWhereRowValues()
{
$builder = $this->getBuilder();
Expand Down
18 changes: 16 additions & 2 deletions tests/Integration/Database/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
Expand All @@ -18,12 +19,14 @@ protected function setUp(): void
parent::setUp();

Schema::create('posts', function (Blueprint $table) {
$table->string('title');
$table->text('content');
$table->timestamp('created_at');
});

DB::table('posts')->insert([
['created_at' => new Carbon('2017-11-12 13:14:15')],
['created_at' => new Carbon('2018-01-02 03:04:05')],
['title' => 'Foo Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2017-11-12 13:14:15')],
['title' => 'Bar Post', 'content' => 'Lorem Ipsum.', 'created_at' => new Carbon('2018-01-02 03:04:05')],
]);
}

Expand Down Expand Up @@ -59,4 +62,15 @@ public function testWhereTime()
$this->assertSame(1, DB::table('posts')->whereTime('created_at', '03:04:05')->count());
$this->assertSame(1, DB::table('posts')->whereTime('created_at', new Carbon('2018-01-02 03:04:05'))->count());
}

public function testPaginateWithSpecificColumns()
{
$result = DB::table('posts')->paginate(5, ['title', 'content']);

$this->assertInstanceOf(LengthAwarePaginator::class, $result);
$this->assertEquals($result->items(), [
(object) ['title' => 'Foo Post', 'content' => 'Lorem Ipsum.'],
(object) ['title' => 'Bar Post', 'content' => 'Lorem Ipsum.'],
]);
}
}

0 comments on commit 7ac7818

Please sign in to comment.