An eloquent-based database testing library for phpunit, inspired by dbunit.
This works well with Slim Framework (and possibly others) which allows you to create an application, then dispatch multiple requests to it. By using database transactions for each test, you do not need to "tear down" data from prior tests, nor deal with artifacts left over from failed tests.
- runs test cases inside a database transaction
- provides assertions
- provides a
seed
method to quickly populate database tables - support for null/not null on assertions
Install via composer:
$ composer require eloqunit/eloqunit
Instead of extending PHPUnit\Framework\TestCase
, have your tests extend Eloqunit\TestCase
.
You need to provide a getDatabase()
method, which returns a Illuminate\Database\Capsule\Manager
, assertions, seeding etc
are executed against this database.
This only works if the database is the same one as used in the the system under test (eg, they are shared through a DI container).
Seed $table with rows contained in $data.
$this->seed('mytable', [
['id' => '1', 'name' => 'row.one', 'active' => true, 'foo' => null],
['2', 'row.two', false, 'bar'],
]);
Seed multiple tables. Array keys represent table names, and array values represent table rows.
$this->seedTables(
'mytable' => [
['id' => 1, 'name' => 'row.one', 'active' => true],
[2, 'row.two', true],
[3, 'row.three', true],
],
'myothertable' => [
['key' => 'foo', 'description' => 'description of foo'],
['bar', 'bar description'],
['baz', 'baz description'],
],
);
Assert that $table
contains $expected
number of rows. Optionally, filtered by $where.
$this->assertRowCount(1, 'mytable', ['active' => true, 'foo' => Eloqunit\Constraint::IsNull]);
$this->assertRowCount(1, 'mytable', ['active' => false, 'foo' => ELoqunit\Constraint::IsNotNull];
$this->assertRowCount(2, 'mytable');
Assert that a row exists in $table. Optionally, filtered by $where.
$this->assertRowExists('mytable', ['id' => 1]);
$this->assertRowExists('mytable', ['foo' => Eloqunit\Constraint::IsNotNull]);
Assert that the first row in $table which matches $where, contains fields matching $fields.
$this->assertRowMatches('mytable', ['id' => 1], ['name' => 'row.one', 'active' => true]);