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 @@ -108,6 +108,36 @@ protected function assertSoftDeleted($table, array $data = [], $connection = nul
return $this;
}

/**
* Assert the given model exists in the database.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return $this
*/
protected function assertModelExists($model)
{
return $this->assertDatabaseHas(
$model->getTable(),
[$model->getKeyName() => $model->getKey()],
$model->getConnectionName()
);
}

/**
* Assert the given model does not exist in the database.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return $this
*/
protected function assertModelMissing($model)
{
return $this->assertDatabaseMissing(
$model->getTable(),
[$model->getKeyName() => $model->getKey()],
$model->getConnectionName()
);
}

/**
* Determine if the argument is a soft deletable model.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ public function testAssertDeletedPassesWhenDoesNotFindModelResults()
$this->assertDeleted(new ProductStub($this->data));
}

public function testAssertModelMissingPassesWhenDoesNotFindModelResults()
{
$this->data = ['id' => 1];

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

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

$this->assertModelMissing(new ProductStub($this->data));
}

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

public function testAssertExistsPassesWhenFindsResults()
{
$this->data = ['id' => 1];

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

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

$this->assertModelExists(new ProductStub($this->data));
}

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