Skip to content

Commit

Permalink
Add assertDeleted for database testing (#30648)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored and taylorotwell committed Nov 22, 2019
1 parent e0d4aa3 commit 9858507
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
42 changes: 42 additions & 0 deletions tests/Foundation/FoundationInteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9858507

Please sign in to comment.