diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 269293a76590..166e5cc860a8 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -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. * diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 6b5127ce8980..394110dc5f79 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -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); @@ -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));