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.3] Add missing tests for Query Builder chunk() #16263

Merged
merged 5 commits into from
Nov 4, 2016
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
85 changes: 46 additions & 39 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function testFindWithMany()
public function testFirstMethod()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[get,take]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('take')->with(1)->andReturn($builder);
$builder->shouldReceive('take')->with(1)->andReturnSelf();
$builder->shouldReceive('get')->with(['*'])->andReturn(new Collection(['bar']));

$result = $builder->first();
Expand Down Expand Up @@ -160,18 +160,18 @@ public function testChunkWithLastChunkComplete()
$chunk1 = new Collection(['foo1', 'foo2']);
$chunk2 = new Collection(['foo3', 'foo4']);
$chunk3 = new Collection([]);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(3, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->once()->with(3, 2)->andReturnSelf();
$builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);

$callbackExecutionAssertor = m::mock('StdClass');
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk1)->once();
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk2)->once();
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk3)->never();
$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3);

$builder->chunk(2, function ($results) use ($callbackExecutionAssertor) {
$callbackExecutionAssertor->doSomething($results);
$builder->chunk(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
});
}

Expand All @@ -180,16 +180,16 @@ public function testChunkWithLastChunkPartial()
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$chunk1 = new Collection(['foo1', 'foo2']);
$chunk2 = new Collection(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$callbackExecutionAssertor = m::mock('StdClass');
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk1)->once();
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk2)->once();
$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);

$builder->chunk(2, function ($results) use ($callbackExecutionAssertor) {
$callbackExecutionAssertor->doSomething($results);
$builder->chunk(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
});
}

Expand All @@ -198,16 +198,16 @@ public function testChunkCanBeStoppedByReturningFalse()
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPage,get]', [$this->getMockQueryBuilder()]);
$chunk1 = new Collection(['foo1', 'foo2']);
$chunk2 = new Collection(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturn($builder);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->never()->with(2, 2);
$builder->shouldReceive('get')->times(1)->andReturn($chunk1);

$callbackExecutionAssertor = m::mock('StdClass');
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk1)->once();
$callbackExecutionAssertor->shouldReceive('doSomething')->with($chunk2)->never();
$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);

$builder->chunk(2, function ($results) use ($callbackExecutionAssertor) {
$callbackExecutionAssertor->doSomething($results);
$builder->chunk(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);

return false;
});
Expand All @@ -216,32 +216,39 @@ public function testChunkCanBeStoppedByReturningFalse()
public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturn($builder);
$chunk1 = new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = new Collection([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]);
$chunk3 = new Collection([]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);

$builder->shouldReceive('get')->times(3)->andReturn(
new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]),
new Collection([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]),
new Collection([])
);
$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3);

$builder->chunkById(2, function ($results) {
$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithLastChunkPartial()
{
$builder = m::mock('Illuminate\Database\Eloquent\Builder[forPageAfterId,get]', [$this->getMockQueryBuilder()]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturn($builder);
$chunk1 = new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = new Collection([(object) ['someIdField' => 10]]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$builder->shouldReceive('get')->times(2)->andReturn(
new Collection([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]),
new Collection([(object) ['someIdField' => 10]])
);
$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);

$builder->chunkById(2, function ($results) {
$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'someIdField');
}

Expand Down
139 changes: 104 additions & 35 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1535,9 +1535,9 @@ public function testDynamicWhere()
$parameters = ['corge', 'waldo', 'fred'];
$builder = m::mock('Illuminate\Database\Query\Builder')->makePartial();

$builder->shouldReceive('where')->with('foo_bar', '=', $parameters[0], 'and')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('baz', '=', $parameters[1], 'and')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('qux', '=', $parameters[2], 'or')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('foo_bar', '=', $parameters[0], 'and')->once()->andReturnSelf();
$builder->shouldReceive('where')->with('baz', '=', $parameters[1], 'and')->once()->andReturnSelf();
$builder->shouldReceive('where')->with('qux', '=', $parameters[2], 'or')->once()->andReturnSelf();

$this->assertEquals($builder, $builder->dynamicWhere($method, $parameters));
}
Expand All @@ -1548,9 +1548,9 @@ public function testDynamicWhereIsNotGreedy()
$parameters = ['6.1', '4.2', 'Vertical'];
$builder = m::mock('Illuminate\Database\Query\Builder')->makePartial();

$builder->shouldReceive('where')->with('ios_version', '=', '6.1', 'and')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('android_version', '=', '4.2', 'and')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('orientation', '=', 'Vertical', 'or')->once()->andReturn($builder);
$builder->shouldReceive('where')->with('ios_version', '=', '6.1', 'and')->once()->andReturnSelf();
$builder->shouldReceive('where')->with('android_version', '=', '4.2', 'and')->once()->andReturnSelf();
$builder->shouldReceive('where')->with('orientation', '=', 'Vertical', 'or')->once()->andReturnSelf();

$builder->dynamicWhere($method, $parameters);
}
Expand Down Expand Up @@ -1740,50 +1740,119 @@ public function testTableValuedFunctionAsTableInSqlServer()
$this->assertEquals('select * from [users](1,2)', $builder->toSql());
}

public function testChunkPaginatesUsingIdWithLastChunkComplete()
public function testChunkWithLastChunkComplete()
{
$builder = $this->getMockQueryBuilder();
$chunk1 = collect(['foo1', 'foo2']);
$chunk2 = collect(['foo3', 'foo4']);
$chunk3 = collect([]);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->once()->with(3, 2)->andReturnSelf();
$builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3);

$builder->chunk(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
});
}

public function testChunkWithLastChunkPartial()
{
$builder = $this->getMockQueryBuilder();
$chunk1 = collect(['foo1', 'foo2']);
$chunk2 = collect(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->once()->with(2, 2)->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);

$builder->chunk(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
});
}

public function testChunkCanBeStoppedByReturningFalse()
{
$builder = $this->getMockQueryBuilder();
$chunk1 = collect(['foo1', 'foo2']);
$chunk2 = collect(['foo3']);
$builder->shouldReceive('forPage')->once()->with(1, 2)->andReturnSelf();
$builder->shouldReceive('forPage')->never()->with(2, 2);
$builder->shouldReceive('get')->times(1)->andReturn($chunk1);

$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturn($builder);
$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);

$builder->shouldReceive('get')->times(3)->andReturn(
collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]),
collect([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]),
collect([])
);
$builder->chunk(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);

$builder->chunkById(2, function ($results) {
return false;
});
}

public function testChunkPaginatesUsingIdWithLastChunkComplete()
{
$builder = $this->getMockQueryBuilder();
$chunk1 = collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = collect([(object) ['someIdField' => 10], (object) ['someIdField' => 11]]);
$chunk3 = collect([]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 11, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(3)->andReturn($chunk1, $chunk2, $chunk3);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk3);

$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithLastChunkPartial()
{
$builder = $this->getMockQueryBuilder();

$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturn($builder);

$builder->shouldReceive('get')->times(2)->andReturn(
collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]),
collect([(object) ['someIdField' => 10]])
);

$builder->chunkById(2, function ($results) {
$chunk1 = collect([(object) ['someIdField' => 1], (object) ['someIdField' => 2]]);
$chunk2 = collect([(object) ['someIdField' => 10]]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'someIdField')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 2, 'someIdField')->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk2);

$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'someIdField');
}

public function testChunkPaginatesUsingIdWithAlias()
{
$builder = $this->getMockQueryBuilder();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'table.id')->andReturn($builder);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 10, 'table.id')->andReturn($builder);
$builder->shouldReceive('get')->times(2)->andReturn(
collect([(object) ['table_id' => 1], (object) ['table_id' => 10]]),
collect([])
);
$builder->chunkById(2, function ($results) {
$chunk1 = collect([(object) ['table_id' => 1], (object) ['table_id' => 10]]);
$chunk2 = collect([]);
$builder->shouldReceive('forPageAfterId')->once()->with(2, 0, 'table.id')->andReturnSelf();
$builder->shouldReceive('forPageAfterId')->once()->with(2, 10, 'table.id')->andReturnSelf();
$builder->shouldReceive('get')->times(2)->andReturn($chunk1, $chunk2);

$callbackAssertor = m::mock('StdClass');
$callbackAssertor->shouldReceive('doSomething')->once()->with($chunk1);
$callbackAssertor->shouldReceive('doSomething')->never()->with($chunk2);

$builder->chunkById(2, function ($results) use ($callbackAssertor) {
$callbackAssertor->doSomething($results);
}, 'table.id', 'table_id');
}

Expand All @@ -1799,7 +1868,7 @@ public function testPaginate()
$results = collect([['test' => 'foo'], ['test' => 'bar']]);

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

Paginator::currentPathResolver(function () use ($path) {
Expand All @@ -1826,7 +1895,7 @@ public function testPaginateWithDefaultArguments()
$results = collect([['test' => 'foo'], ['test' => 'bar']]);

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

Paginator::currentPageResolver(function () use ($path) {
Expand Down