diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 2378f815094d..517796c1d893 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -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) ); @@ -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) ); @@ -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); } /** @@ -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); } /** diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 34047e1a7db7..2ebb6a99118d 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -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() @@ -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()