Skip to content

Commit

Permalink
Merge branch 'guess-matches' of https://github.com/browner12/framework
Browse files Browse the repository at this point in the history
…into browner12-guess-matches
  • Loading branch information
taylorotwell committed Jan 13, 2020
2 parents 3a6d064 + 7fcf5a6 commit f58e4e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 15 additions & 5 deletions src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,23 @@ protected function getAdditionalInfo($table)
{
$query = $this->database->table($table);

$results = $query->limit($this->show)->get();
$key = array_key_first($this->data);
$value = $this->data[$key];
$similarResults = $query->where($key, $value)->limit($this->show)->get();

if ($results->isEmpty()) {
return 'The table is empty';
}
if ($similarResults->isNotEmpty()) {
$description = 'Found similar results: '.json_encode($similarResults, JSON_PRETTY_PRINT);
} else {
$query = $this->database->table($table);

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

$description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT);
if ($results->isEmpty()) {
return 'The table is empty';
}

$description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT);
}

if ($query->count() > $this->show) {
$description .= sprintf(' and %s others', $query->count() - $this->show);
Expand Down
14 changes: 11 additions & 3 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class FoundationInteractsWithDatabaseTest extends TestCase

protected $table = 'products';

protected $data = ['title' => 'Spark'];
protected $data = [
'title' => 'Spark',
'name' => 'Laravel',
];

protected $connection;

Expand Down Expand Up @@ -54,7 +57,7 @@ public function testSeeInDatabaseFindsNotMatchingResults()
{
$this->expectException(ExpectationFailedException::class);

$this->expectExceptionMessage('Found: '.json_encode([['title' => 'Forge']], JSON_PRETTY_PRINT));
$this->expectExceptionMessage('Found similar results: '.json_encode([['title' => 'Forge']], JSON_PRETTY_PRINT));

$builder = $this->mockCountBuilder(0);

Expand All @@ -68,7 +71,7 @@ public function testSeeInDatabaseFindsManyNotMatchingResults()
{
$this->expectException(ExpectationFailedException::class);

$this->expectExceptionMessage('Found: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.');
$this->expectExceptionMessage('Found similar results: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.');

$builder = $this->mockCountBuilder(0);
$builder->shouldReceive('count')->andReturn(0, 5);
Expand Down Expand Up @@ -193,6 +196,11 @@ protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at
{
$builder = m::mock(Builder::class);

$key = array_key_first($this->data);
$value = $this->data[$key];

$builder->shouldReceive('where')->with($key, $value)->andReturnSelf();

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

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

0 comments on commit f58e4e1

Please sign in to comment.