From bc1057b2960d9e7fec84fceb49e31614ec425e15 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Thu, 21 Nov 2019 13:36:50 -0500 Subject: [PATCH] Add assertDeleted for database testing --- .../Concerns/InteractsWithDatabase.php | 19 +++++++++ .../FoundationInteractsWithDatabaseTest.php | 42 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php index 909523cdf71d..1da32f90af0e 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php +++ b/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php @@ -53,6 +53,25 @@ protected function assertDatabaseMissing($table, array $data, $connection = null * @param \Illuminate\Database\Eloquent\Model|string $table * @param array $data * @param string|null $connection + * @return $this + */ + protected function assertDeleted($table, array $data = [], $connection = null) + { + if ($table instanceof Model) { + return $this->assertDatabaseMissing($table->getTable(), [$table->getKeyName() => $table->getKey()], $table->getConnectionName()); + } + + $this->assertDatabaseMissing($table, $data, $connection); + + return $this; + } + + /** + * Assert the given record has been "soft deleted". + * + * @param \Illuminate\Database\Eloquent\Model|string $table + * @param array $data + * @param string|null $connection * @param string|null $deletedAtColumn * @return $this */ diff --git a/tests/Foundation/FoundationInteractsWithDatabaseTest.php b/tests/Foundation/FoundationInteractsWithDatabaseTest.php index 5a3d7c7c0191..6edccb58ac32 100644 --- a/tests/Foundation/FoundationInteractsWithDatabaseTest.php +++ b/tests/Foundation/FoundationInteractsWithDatabaseTest.php @@ -100,6 +100,48 @@ public function testDontSeeInDatabaseFindsResults() $this->assertDatabaseMissing($this->table, $this->data); } + public function testAssertDeletedPassesWhenDoesNotFindResults() + { + $this->mockCountBuilder(0); + + $this->assertDatabaseMissing($this->table, $this->data); + } + + public function testAssertDeletedFailsWhenFindsResults() + { + $this->expectException(ExpectationFailedException::class); + + $builder = $this->mockCountBuilder(1); + + $builder->shouldReceive('get')->andReturn(collect([$this->data])); + + $this->assertDatabaseMissing($this->table, $this->data); + } + + public function testAssertDeletedPassesWhenDoesNotFindModelResults() + { + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(0); + + $builder->shouldReceive('get')->andReturn(collect()); + + $this->assertDeleted(new ProductStub($this->data)); + } + + public function testAssertDeletedFailsWhenFindsModelResults() + { + $this->expectException(ExpectationFailedException::class); + + $this->data = ['id' => 1]; + + $builder = $this->mockCountBuilder(1); + + $builder->shouldReceive('get')->andReturn(collect([$this->data])); + + $this->assertDeleted(new ProductStub($this->data)); + } + public function testAssertSoftDeletedInDatabaseFindsResults() { $this->mockCountBuilder(1);