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

[8.x] Add model support for database assertions #37459

Merged
merged 1 commit into from
May 24, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ trait InteractsWithDatabase
/**
* Assert that a given where condition exists in the database.
*
* @param string $table
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param array $data
* @param string|null $connection
* @return $this
*/
protected function assertDatabaseHas($table, array $data, $connection = null)
{
$this->assertThat(
$table, new HasInDatabase($this->getConnection($connection), $data)
$this->getTable($table), new HasInDatabase($this->getConnection($connection), $data)
);

return $this;
Expand All @@ -34,7 +34,7 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
/**
* Assert that a given where condition does not exist in the database.
*
* @param string $table
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param array $data
* @param string|null $connection
* @return $this
Expand All @@ -45,23 +45,23 @@ protected function assertDatabaseMissing($table, array $data, $connection = null
new HasInDatabase($this->getConnection($connection), $data)
);

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

return $this;
}

/**
* Assert the count of table entries.
*
* @param string $table
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param int $count
* @param string|null $connection
* @return $this
*/
protected function assertDatabaseCount($table, int $count, $connection = null)
{
$this->assertThat(
$table, new CountInDatabase($this->getConnection($connection), $count)
$this->getTable($table), new CountInDatabase($this->getConnection($connection), $count)
);

return $this;
Expand All @@ -81,7 +81,7 @@ protected function assertDeleted($table, array $data = [], $connection = null)
return $this->assertDatabaseMissing($table->getTable(), [$table->getKeyName() => $table->getKey()], $table->getConnectionName());
}

$this->assertDatabaseMissing($table, $data, $connection);
$this->assertDatabaseMissing($this->getTable($table), $data, $connection);

return $this;
}
Expand All @@ -102,7 +102,7 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul
}

$this->assertThat(
$table, new SoftDeletedInDatabase($this->getConnection($connection), $data, $deletedAtColumn)
$this->getTable($table), new SoftDeletedInDatabase($this->getConnection($connection), $data, $deletedAtColumn)
);

return $this;
Expand Down Expand Up @@ -152,6 +152,21 @@ protected function getConnection($connection = null)
return $database->connection($connection);
}

/**
* Get the table name.
*
* @param \Illuminate\Database\Eloquent\Model|string $table
* @return string
*/
protected function getTable($table)
{
if (is_subclass_of($table, Model::class)) {
return (new $table)->getTable();
}

return $table;
}

/**
* Seed a given database connection.
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function testSeeInDatabaseFindsResults()
$this->assertDatabaseHas($this->table, $this->data);
}

public function testAssertDatabaseHasSupportModels()
{
$this->mockCountBuilder(1);

$this->assertDatabaseHas(ProductStub::class, $this->data);
$this->assertDatabaseHas(new ProductStub, $this->data);
}

public function testSeeInDatabaseDoesNotFindResults()
{
$this->expectException(ExpectationFailedException::class);
Expand Down Expand Up @@ -91,6 +99,14 @@ public function testDontSeeInDatabaseDoesNotFindResults()
$this->assertDatabaseMissing($this->table, $this->data);
}

public function testAssertDatabaseMissingSupportModels()
{
$this->mockCountBuilder(0);

$this->assertDatabaseMissing(ProductStub::class, $this->data);
$this->assertDatabaseMissing(new ProductStub, $this->data);
}

public function testDontSeeInDatabaseFindsResults()
{
$this->expectException(ExpectationFailedException::class);
Expand All @@ -110,6 +126,14 @@ public function testAssertTableEntriesCount()
$this->assertDatabaseCount($this->table, 1);
}

public function testAssertDatabaseCountSupportModels()
{
$this->mockCountBuilder(1);

$this->assertDatabaseCount(ProductStub::class, 1);
$this->assertDatabaseCount(new ProductStub, 1);
}

public function testAssertTableEntriesCountWrong()
{
$this->expectException(ExpectationFailedException::class);
Expand Down Expand Up @@ -168,6 +192,13 @@ public function testAssertSoftDeletedInDatabaseFindsResults()
$this->assertSoftDeleted($this->table, $this->data);
}

public function testAssertSoftDeletedSupportModelStrings()
{
$this->mockCountBuilder(1);

$this->assertSoftDeleted(ProductStub::class, $this->data);
}

public function testAssertSoftDeletedInDatabaseDoesNotFindResults()
{
$this->expectException(ExpectationFailedException::class);
Expand Down Expand Up @@ -208,6 +239,13 @@ public function testAssertSoftDeletedInDatabaseDoesNotFindModelWithCustomColumnR
$this->assertSoftDeleted(new CustomProductStub($this->data));
}

public function testGetTableNameFromModel()
{
$this->assertEquals($this->table, $this->getTable(ProductStub::class));
$this->assertEquals($this->table, $this->getTable(new ProductStub));
$this->assertEquals($this->table, $this->getTable($this->table));
}

protected function mockCountBuilder($countResult, $deletedAtColumn = 'deleted_at')
{
$builder = m::mock(Builder::class);
Expand Down