bendamqui / dbunit-table
Facade to fetch dbunit fixtures without querying the database.
This package's canonical repository appears to be gone and the package has been frozen as a result.
Requires
- php: >=7.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15@dev
- phpunit/phpunit: ^7.5
- victorjonsson/markdowndocs: dev-master
This package is auto-updated.
Last update: 2020-03-26 23:41:35 UTC
README
Description
One of the challenge when using DbUnit is to write tests that will stay valid even when the data fixtures get modified. As the application grows new tests that will require modifications to the fixtures are meant to happen. Having a direct access to the data fixtures from the tests is often a good way to solve this problem.
This facade allow to do just that without hitting the database to avoid slowing down the tests. It simply
receive an instance of PHPUnit\DbUnit\DataSet\ITable
which is where DbUnit store the data fixtures that
are used to seed the database before each test. It then provides methods that allow a more precise access
to the data then the original ITable interface in order to keep the test code cleaner.
Installation
composer require bendamqui/dbunit-table
Basic Setup example
use PHPUnit\Framework\TestCase; use Bendamqui\DbUnit\FixtureUtil; class DbUnitUsersTest extends TestCase { /** * @var FixtureUtil */ private $users_table; /** * @var array */ private $data = []; /** * @var YourApp */ private $app; public function setUp() { parent::setUp(); $this->users_table = new FixtureUtil($this->data); } }
Examples
Below are a few basic examples of how to take advantage of the facade to write tests that will still be valid if the fixture changes while keeping your code clean and avoiding unnecessary call to the database.
public function testGetAllUsers() { $expected = $this->users_table->getRowCount(); $response = $this->app->get('users'); $this->assertCount($expected, $response); }
public function testPendingUserCannotLogIn() { $pending_users = $this->users_table->getWhere(['status' => 'pending']); foreach ($pending_users as $user) { $response = $this->app->post('login', ['email' => $user['email'], 'pass' => $user['pass']]); $this->assertEquals(401, $response->getCode(), 'Pending user should not be able to log in.'); } $this->assertGreaterThan(0, count($pending_users)); }
public function testCannotUpdateUserWithAnInvalidEmail() { // Get a valid payload to update a user and override the email field. $payload = $this->users_table->get(['email' => 'invalid_email']); $response = $this->app->put('user', $payload); $this->assertEquals(422, $response->getCode(), 'User update with invalid email should receive a bad request response'); }
API
Visibility | Function |
---|---|
public | get(array $override=array(), int $row=0) : array Get one row. |
public | getAll(array $override=array()) : array Get all rows. |
public | getAllRaw() : array Get all rows in raw format (skip post processing). |
public | getByPrimaryKey(mixed $id, array/mixed $override=array()) : array Get one row by primary key |
public | getRaw(int $row=0) : array Get a row by its row number (skip post processing). |
public | getRowCount() : int Get the number of row in the table |
public | getValue(mixed $column, int $row=0) : mixed Get the value of a given row/column in the Table. |
public | getWhere(array $filters=array()) : array Get many rows using filters in the form of key value. Perform AND only. |
public | setHidden(array $hidden) : void Set a list of columns that should not be returned when fetching row(s) |
public | setPrimaryKey(string $primary_key) : void Set the primary key of the table. Is set to 'id' by default. |