Skip to content

Commit

Permalink
clean up a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 7, 2016
1 parent 12325c9 commit 14e9dad
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ trait InteractsWithDatabase
* @param string $table
* @param array $data
* @param string $connection
* @param bool $reverse
* @return $this
*/
protected function seeInDatabase($table, array $data, $connection = null, $reverse = false)
protected function seeInDatabase($table, array $data, $connection = null)
{
$constraint = new HasInDatabase($data, $this->getConnection($connection));

if ($reverse) {
$constraint = new ReverseConstraint($constraint);
}

$this->assertThat($table, $constraint);
$this->assertThat(
$table, new HasInDatabase($this->getConnection($connection), $data)
);

return $this;
}
Expand All @@ -39,7 +34,13 @@ protected function seeInDatabase($table, array $data, $connection = null, $rever
*/
protected function missingFromDatabase($table, array $data, $connection = null)
{
return $this->notSeeInDatabase($table, $data, $connection);
$constraint = new ReverseConstraint(
new HasInDatabase($this->getConnection($connection), $data)
);

$this->assertThat($table, $constraint);

return $this;
}

/**
Expand All @@ -65,7 +66,7 @@ protected function dontSeeInDatabase($table, array $data, $connection = null)
*/
protected function notSeeInDatabase($table, array $data, $connection = null)
{
return $this->seeInDatabase($table, $data, $connection, true);
return $this->missingFromDatabase($table, $data, $connection);
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Illuminate/Foundation/Testing/Constraints/HasInDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class HasInDatabase extends PHPUnit_Framework_Constraint
protected $show = 3;

/**
* Database connection.
* The database connection.
*
* @var \Illuminate\Database\Collection
*/
protected $database;

/**
* Data that will be used to narrow the search in the database table.
* The data that will be used to narrow the search in the database table.
*
* @var array
*/
Expand All @@ -31,10 +31,11 @@ class HasInDatabase extends PHPUnit_Framework_Constraint
/**
* Create a new constraint instance.
*
* @param array $data
* @param \Illuminate\Database\Collection $database
* @param array $data
* @return void
*/
public function __construct(array $data, Connection $database)
public function __construct(Connection $database, array $data)
{
$this->data = $data;

Expand Down Expand Up @@ -80,7 +81,7 @@ protected function getAdditionalInfo($table)
return 'The table is empty';
}

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

if ($results->count() > $this->show) {
$description .= sprintf(' and %s others', $results->count() - $this->show);
Expand Down
17 changes: 11 additions & 6 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,33 +40,37 @@ public function testSeeInDatabaseDoesNotFindResults()
{
$builder = $this->mockCountBuilder(0);

$builder->shouldReceive('get')->once()->andReturn(collect());
$builder->shouldReceive('get')->andReturn(collect());

$this->seeInDatabase($this->table, $this->data);
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Found: [{"title":"Forge"}].
*/
public function testSeeInDatabaseFindsNotMatchingResults()
{
$this->expectExceptionMessage('Found: '.json_encode([['title' => 'Forge']], JSON_PRETTY_PRINT));

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

$builder->shouldReceive('get')->once()->andReturn(collect([['title' => 'Forge']]));
$builder->shouldReceive('take')->andReturnSelf();
$builder->shouldReceive('get')->andReturn(collect([['title' => 'Forge']]));

$this->seeInDatabase($this->table, $this->data);
}

/**
* @expectedException \PHPUnit_Framework_ExpectationFailedException
* @expectedExceptionMessage Found: ["data","data","data"] and 2 others.
*/
public function testSeeInDatabaseFindsManyNotMatchingResults()
{
$this->expectExceptionMessage('Found: '.json_encode(['data', 'data', 'data'], JSON_PRETTY_PRINT).' and 2 others.');

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

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

Expand All @@ -88,7 +92,8 @@ public function testDontSeeInDatabaseFindsResults()
{
$builder = $this->mockCountBuilder(1);

$builder->shouldReceive('get')->once()->andReturn(collect([$this->data]));
$builder->shouldReceive('take')->andReturnSelf();
$builder->shouldReceive('get')->andReturn(collect([$this->data]));

$this->dontSeeInDatabase($this->table, $this->data);
}
Expand Down

0 comments on commit 14e9dad

Please sign in to comment.