diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 73d5f9771b5b..115589b17936 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -33,6 +33,14 @@ protected function assertDatabaseHas($table, array $data = [], $connection = nul return $this; } + if (array_is_list($data) && array_all($data, fn ($row) => is_array($row))) { + foreach ($data as $row) { + $this->assertDatabaseHas($table, $row, $connection); + } + + return $this; + } + if ($table instanceof Model) { $data = [ $table->getKeyName() => $table->getKey(), @@ -65,6 +73,14 @@ protected function assertDatabaseMissing($table, array $data = [], $connection = return $this; } + if (array_is_list($data) && array_all($data, fn ($row) => is_array($row))) { + foreach ($data as $row) { + $this->assertDatabaseMissing($table, $row, $connection); + } + + return $this; + } + if ($table instanceof Model) { $data = [ $table->getKeyName() => $table->getKey(), diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 9253952fda0c..6311839184c8 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -61,6 +61,21 @@ public function testAssertDatabaseHasConstrainsToModel() $this->assertDatabaseHas(new ProductStub(['id' => 1]), $data); } + public function testAssertDatabaseSupportsArrays() + { + $builder = m::mock(Builder::class); + $builder->shouldReceive('where')->with(['title' => 'Spark', 'name' => 'Laravel'])->once()->andReturnSelf(); + $builder->shouldReceive('where')->with(['title' => 'Forge', 'name' => 'Laravel'])->once()->andReturnSelf(); + $builder->shouldReceive('exists')->twice()->andReturn(true); + + $this->connection->shouldReceive('table')->with($this->table)->andReturn($builder); + + $this->assertDatabaseHas($this->table, [ + ['title' => 'Spark', 'name' => 'Laravel'], + ['title' => 'Forge', 'name' => 'Laravel'], + ]); + } + public function testSeeInDatabaseDoesNotFindResults() { $this->expectException(ExpectationFailedException::class); @@ -103,6 +118,21 @@ public function testSeeInDatabaseFindsManyNotMatchingResults() $this->assertDatabaseHas($this->table, $this->data); } + public function testAssertDatabaseMissingSupportsArrays() + { + $builder = m::mock(Builder::class); + $builder->shouldReceive('where')->with(['title' => 'Spark', 'name' => 'Laravel'])->once()->andReturnSelf(); + $builder->shouldReceive('where')->with(['title' => 'Forge', 'name' => 'Laravel'])->once()->andReturnSelf(); + $builder->shouldReceive('exists')->twice()->andReturn(false); + + $this->connection->shouldReceive('table')->with($this->table)->andReturn($builder); + + $this->assertDatabaseMissing($this->table, [ + ['title' => 'Spark', 'name' => 'Laravel'], + ['title' => 'Forge', 'name' => 'Laravel'], + ]); + } + public function testDontSeeInDatabaseDoesNotFindResults() { $this->mockCountBuilder(false);