Skip to content
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 @@ -24,8 +24,15 @@ trait InteractsWithDatabase
* @param string|null $connection
* @return $this
*/
protected function assertDatabaseHas($table, array $data, $connection = null)
protected function assertDatabaseHas($table, array $data = [], $connection = null)
{
if ($table instanceof Model) {
$data = [
$table->getKeyName() => $table->getKey(),
...$data,
];
}

$this->assertThat(
$this->getTable($table), new HasInDatabase($this->getConnection($connection, $table), $data)
);
Expand All @@ -41,8 +48,15 @@ protected function assertDatabaseHas($table, array $data, $connection = null)
* @param string|null $connection
* @return $this
*/
protected function assertDatabaseMissing($table, array $data, $connection = null)
protected function assertDatabaseMissing($table, array $data = [], $connection = null)
{
if ($table instanceof Model) {
$data = [
$table->getKeyName() => $table->getKey(),
...$data,
];
}

$constraint = new ReverseConstraint(
new HasInDatabase($this->getConnection($connection, $table), $data)
);
Expand Down Expand Up @@ -157,11 +171,7 @@ protected function assertNotSoftDeleted($table, array $data = [], $connection =
*/
protected function assertModelExists($model)
{
return $this->assertDatabaseHas(
$model->getTable(),
[$model->getKeyName() => $model->getKey()],
$model->getConnectionName()
);
return $this->assertDatabaseHas($model);
}

/**
Expand All @@ -172,11 +182,7 @@ protected function assertModelExists($model)
*/
protected function assertModelMissing($model)
{
return $this->assertDatabaseMissing(
$model->getTable(),
[$model->getKeyName() => $model->getKey()],
$model->getConnectionName()
);
return $this->assertDatabaseMissing($model);
}

/**
Expand Down
34 changes: 30 additions & 4 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,25 @@ public function testSeeInDatabaseFindsResults()
$this->assertDatabaseHas($this->table, $this->data);
}

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

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

public function testAssertDatabaseHasConstrainsToModel()
{
$data = $this->data;

$this->data = [
'id' => 1,
...$this->data,
];

$this->mockCountBuilder(1);

$this->assertDatabaseHas(new ProductStub(['id' => 1]), $data);
}

public function testSeeInDatabaseDoesNotFindResults()
Expand Down Expand Up @@ -102,12 +115,25 @@ public function testDontSeeInDatabaseDoesNotFindResults()
$this->assertDatabaseMissing($this->table, $this->data);
}

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

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

public function testAssertDatabaseMissingConstrainsToModel()
{
$data = $this->data;

$this->data = [
'id' => 1,
...$this->data,
];

$this->mockCountBuilder(0);

$this->assertDatabaseMissing(new ProductStub(['id' => 1]), $data);
}

public function testDontSeeInDatabaseFindsResults()
Expand Down