Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database interaction assertions #1

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(static function () {
$files = [
'InteractsWithAuthentication.php',
'InteractsWithDatabase.php',
];

foreach ($files as $file) {
Expand Down
75 changes: 75 additions & 0 deletions src/InteractsWithDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

use Illuminate\Foundation\Testing\TestCase;

if (!function_exists('assertDatabaseHas')) {
/**
* Assert that a table in the database contains the given data.
*
* @param string $table
* @param array<string, mixed> $data
* @param string|null $connection
*/
function assertDatabaseHas($table, array $data, $connection = null): TestCase
{
return test()->assertDatabaseHas($table, $data, $connection);
}
}

if (!function_exists('assertDatabaseMissing')) {
/**
* Assert that a table in the database does not contain the given data.
*
* @param string $table
* @param array<string, mixed> $data
* @param string|null $connection
*/
function assertDatabaseMissing($table, array $data, $connection = null): TestCase
{
return test()->assertDatabaseHas($table, $data, $connection);
}
}

if (!function_exists('assertDatabaseCount')) {
/**
* Assert the count of table entries.
*
* @param string $table
* @param string|null $connection
*/
function assertDatabaseCount($table, int $count, $connection = null): TestCase
{
return test()->assertDatabaseCount($table, $count, $connection);
}
}

if (!function_exists('assertDeleted')) {
/**
* Assert that the given record has been deleted.
*
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param array<string, mixed> $data
* @param string|null $connection
*/
function assertDeleted($table, array $data = [], $connection = null): TestCase
{
return test()->assertDeleted($table, $data, $connection);
}
}

if (!function_exists('assertSoftDeleted')) {
/**
* Assert that the given record has been "soft deleted".
*
* @param \Illuminate\Database\Eloquent\Model|string $table
* @param array<string, mixed> $data
* @param string|null $connection
* @param string|null $deletedAtColumn
*/
function assertSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at'): TestCase
{
return test()->assertSoftDeleted($table, $data, $connection, $deletedAtColumn);
}
}
16 changes: 16 additions & 0 deletions tests/InteractsWithDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

it('has assertDatabaseHas')
->assertTrue(function_exists('assertDatabaseHas'));

it('has assertDatabaseMissing')
->assertTrue(function_exists('assertDatabaseMissing'));

it('has assertDatabaseCount')
->assertTrue(function_exists('assertDatabaseCount'));

it('has assertDeleted')
->assertTrue(function_exists('assertDeleted'));

it('has assertSoftDeleted')
->assertTrue(function_exists('assertSoftDeleted'));