Skip to content

Commit 9858507

Browse files
jasonmccrearytaylorotwell
authored andcommitted
Add assertDeleted for database testing (#30648)
1 parent e0d4aa3 commit 9858507

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,25 @@ protected function assertDatabaseMissing($table, array $data, $connection = null
5353
* @param \Illuminate\Database\Eloquent\Model|string $table
5454
* @param array $data
5555
* @param string|null $connection
56+
* @return $this
57+
*/
58+
protected function assertDeleted($table, array $data = [], $connection = null)
59+
{
60+
if ($table instanceof Model) {
61+
return $this->assertDatabaseMissing($table->getTable(), [$table->getKeyName() => $table->getKey()], $table->getConnectionName());
62+
}
63+
64+
$this->assertDatabaseMissing($table, $data, $connection);
65+
66+
return $this;
67+
}
68+
69+
/**
70+
* Assert the given record has been "soft deleted".
71+
*
72+
* @param \Illuminate\Database\Eloquent\Model|string $table
73+
* @param array $data
74+
* @param string|null $connection
5675
* @param string|null $deletedAtColumn
5776
* @return $this
5877
*/

tests/Foundation/FoundationInteractsWithDatabaseTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,48 @@ public function testDontSeeInDatabaseFindsResults()
100100
$this->assertDatabaseMissing($this->table, $this->data);
101101
}
102102

103+
public function testAssertDeletedPassesWhenDoesNotFindResults()
104+
{
105+
$this->mockCountBuilder(0);
106+
107+
$this->assertDatabaseMissing($this->table, $this->data);
108+
}
109+
110+
public function testAssertDeletedFailsWhenFindsResults()
111+
{
112+
$this->expectException(ExpectationFailedException::class);
113+
114+
$builder = $this->mockCountBuilder(1);
115+
116+
$builder->shouldReceive('get')->andReturn(collect([$this->data]));
117+
118+
$this->assertDatabaseMissing($this->table, $this->data);
119+
}
120+
121+
public function testAssertDeletedPassesWhenDoesNotFindModelResults()
122+
{
123+
$this->data = ['id' => 1];
124+
125+
$builder = $this->mockCountBuilder(0);
126+
127+
$builder->shouldReceive('get')->andReturn(collect());
128+
129+
$this->assertDeleted(new ProductStub($this->data));
130+
}
131+
132+
public function testAssertDeletedFailsWhenFindsModelResults()
133+
{
134+
$this->expectException(ExpectationFailedException::class);
135+
136+
$this->data = ['id' => 1];
137+
138+
$builder = $this->mockCountBuilder(1);
139+
140+
$builder->shouldReceive('get')->andReturn(collect([$this->data]));
141+
142+
$this->assertDeleted(new ProductStub($this->data));
143+
}
144+
103145
public function testAssertSoftDeletedInDatabaseFindsResults()
104146
{
105147
$this->mockCountBuilder(1);

0 commit comments

Comments
 (0)