From 297b93de0774e0b357d7ca1417b45e510db078e8 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Mon, 30 Sep 2019 17:52:01 +0100 Subject: [PATCH 1/5] Apply limit to database rather than collection For HasInDatabase.php --- .../Foundation/Testing/Constraints/HasInDatabase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php index b88f34222872..66867e30ac05 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php @@ -75,13 +75,13 @@ public function failureDescription($table): string */ protected function getAdditionalInfo($table) { - $results = $this->database->table($table)->get(); + $results = $this->database->table($table)->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); From 6579aa4548338cb465d9621e619f86db900c4563 Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Mon, 30 Sep 2019 22:42:05 +0100 Subject: [PATCH 2/5] Fix tests --- .../Foundation/Testing/Constraints/HasInDatabase.php | 7 ++++--- tests/Foundation/FoundationInteractsWithDatabaseTest.php | 7 +++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php index 66867e30ac05..618024b59082 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php @@ -75,7 +75,8 @@ public function failureDescription($table): string */ protected function getAdditionalInfo($table) { - $results = $this->database->table($table)->limit($this->show)->get(); + $query = $this->database->table($table); + $results = $query->limit($this->show)->get(); if ($results->isEmpty()) { return 'The table is empty'; @@ -83,8 +84,8 @@ protected function getAdditionalInfo($table) $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; diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 0947bdc665d0..5a3d7c7c0191 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -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); @@ -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) From 69ee18ebb5f8aeb72468832b0d59df1e06f38c0c Mon Sep 17 00:00:00 2001 From: Craig Morris Date: Tue, 1 Oct 2019 09:42:57 +0100 Subject: [PATCH 3/5] Add to SoftDeleted trait as well --- .../Testing/Constraints/SoftDeletedInDatabase.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php index 2e0d738b279c..8a3d750aa114 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php @@ -88,16 +88,17 @@ 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; From bafda27b2b8bd367204a714f8e5ffec3a86ddc58 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 3 Oct 2019 11:26:25 -0500 Subject: [PATCH 4/5] Update HasInDatabase.php --- src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php index 618024b59082..35bf40be721b 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php @@ -76,6 +76,7 @@ public function failureDescription($table): string protected function getAdditionalInfo($table) { $query = $this->database->table($table); + $results = $query->limit($this->show)->get(); if ($results->isEmpty()) { From 349f592899884f9b98fb2191dd063a3c0b81eb75 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 3 Oct 2019 11:26:40 -0500 Subject: [PATCH 5/5] Update SoftDeletedInDatabase.php --- .../Foundation/Testing/Constraints/SoftDeletedInDatabase.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php index 8a3d750aa114..f99b9311fb9d 100644 --- a/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php +++ b/src/Illuminate/Foundation/Testing/Constraints/SoftDeletedInDatabase.php @@ -89,6 +89,7 @@ public function failureDescription($table): string protected function getAdditionalInfo($table) { $query = $this->database->table($table); + $results = $query->limit($this->show)->get(); if ($results->isEmpty()) {