Skip to content

Commit

Permalink
[6.x] Apply limit to database rather than collection (laravel#30148)
Browse files Browse the repository at this point in the history
* Apply limit to database rather than collection

For HasInDatabase.php

* Fix tests

* Add to SoftDeleted trait as well

* Update HasInDatabase.php

* Update SoftDeletedInDatabase.php
  • Loading branch information
morrislaptop authored and i-bajrai committed Oct 4, 2019
1 parent 97cd318 commit b7bd16c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
10 changes: 6 additions & 4 deletions src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,18 @@ public function failureDescription($table): string
*/
protected function getAdditionalInfo($table)
{
$results = $this->database->table($table)->get();
$query = $this->database->table($table);

$results = $query->limit($this->show)->get();

if ($results->isEmpty()) {
return 'The table is empty';
}

$description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT);
$description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT);

if ($results->count() > $this->show) {
$description .= sprintf(' and %s others', $results->count() - $this->show);
if ($query->count() > $this->show) {
$description .= sprintf(' and %s others', $query->count() - $this->show);
}

return $description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,18 @@ public function failureDescription($table): string
*/
protected function getAdditionalInfo($table)
{
$results = $this->database->table($table)->get();
$query = $this->database->table($table);

$results = $query->limit($this->show)->get();

if ($results->isEmpty()) {
return 'The table is empty';
}

$description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT);
$description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT);

if ($results->count() > $this->show) {
$description .= sprintf(' and %s others', $results->count() - $this->show);
if ($query->count() > $this->show) {
$description .= sprintf(' and %s others', $query->count() - $this->show);
}

return $description;
Expand Down
7 changes: 5 additions & 2 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ public function testSeeInDatabaseFindsManyNotMatchingResults()
$this->expectExceptionMessage('Found: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.');

$builder = $this->mockCountBuilder(0);
$builder->shouldReceive('count')->andReturn(0, 5);

$builder->shouldReceive('take')->andReturnSelf();
$builder->shouldReceive('get')->andReturn(
collect(array_fill(0, 5, 'data'))
collect(array_fill(0, 3, 'data'))
);

$this->assertDatabaseHas($this->table, $this->data);
Expand Down Expand Up @@ -150,11 +151,13 @@ protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at
{
$builder = m::mock(Builder::class);

$builder->shouldReceive('limit')->andReturnSelf();

$builder->shouldReceive('where')->with($this->data)->andReturnSelf();

$builder->shouldReceive('whereNotNull')->with($deletedAtColumn)->andReturnSelf();

$builder->shouldReceive('count')->andReturn($countResult);
$builder->shouldReceive('count')->andReturn($countResult)->byDefault();

$this->connection->shouldReceive('table')
->with($this->table)
Expand Down

0 comments on commit b7bd16c

Please sign in to comment.