Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Fix columns parameter on paginate method #28937

Merged
merged 1 commit into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.'],
]);
}
}