hotel-quickly / nette-phpunit
Nette + PHPunit + Multiple dbs + fixtures
Installs: 2 235
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 24
Forks: 1
Open Issues: 0
pkg:composer/hotel-quickly/nette-phpunit
Requires
- phpunit/dbunit: ^2.0
- phpunit/phpunit: ^5.1
Requires (Dev)
- nette/di: ^2.3
This package is not auto-updated.
Last update: 2025-10-26 00:03:58 UTC
README
- Support multiple database connections
- Support multiple fixtures formats loading (for each connection)
Getting started
Installation
Run the following commands to install this package
composer require --dev hotel-quickly/nette-phpunit
Usage
In order to use Database test case, you must extend your base class from HQ\Test\AbstractDbTestCase and implement these 3 methods
- getContainer- This method will return- Nette\Di\Containerinstance.
- getConnections- Return an array of- HQ\Test\Connection\AbstractConnection(connectionName, baseSchema, ...)connections object.
- connectionName - A database connection name, which will be used as a prefix of fixture file.
- baseSchema - Database schema which includes CREATE TABLE ...sql statements.
- getBaseFixtureDir- Return a base directory for searching base fixture file (see
use HQ\Test\AbstractDbTestCase; use HQ\Test\Connection\NetteConnection; class YourBaseDbTestCase extends AbstractDbTestCase { protected function getContainer() { return require dirname(__DIR__) . '/app/bootstrap.php'; } protected function getConnections() { return [ new NetteConnection('default', __DIR__ . '/../tests/skeleton.sql', $this->container->getService('database.default.context')) ]; } public function getBaseFixtureDir() { return __DIR__; } }
Working with fixtures
Loading fixtures
By default, fixtures will be automatically loaded from 3 places, in the following orders
- base fixtures
- class fixtures
- instance fixtures
1. base fixtures
This is a primary fixtures that will be loaded for all test cases (think of it as initial data for the whole apps)
test
  +- YourBaseDbTestCase.php
  +- <connection-name>-fixtures.{yaml, json, php} e.g. default-fixtures.json 
2. class fixtures
This will be loaded each time test case was invoked.
test
  +- User
    +- UserTest.php
    +- <connection-name>-fixtures.{yaml, json, php} e.g. default-fixtures.json
3. instance fixtures
This is pretty much the same as class fixtures, except that we define the fixture inside TestCase itself (so only php array is supported.
class MyTest extends MyBaseDbTestCase { public function getFixtures() { return [ '<connection-name>' => [ ... ] ] } }
####Fixture formats + structure
This library currently supports 3 fixture formats e.g. json, yaml and php.
All formats has the same fixture structure i.e.
[
  "<table-name>": [
     [
        "<field-name1>": "<value1>",
        "<field-name2>": "<value2>"
     ],
     ...
   ]
]
json example
{ "user": [ {"username": "john"}, {"username": "jane"} ] }
yaml example
user: - username: john - username: jane
php example - NOTE - Don't forget to put return statement
return [ 'user' => [ ['username' => 'john'], ['username' => 'jane'], ] ]
Improving testing speed
Due to the overhead of creating database schema at the beginning of the tests (only once). This might slow down your test suites speed. You can avoid this problem by exporting UNITTEST_CREATE_SCHEMA=false env and then run unit test as usual. This will make it skips database schema create overhead.
Common problems
- InvalidArgumentException: There is already a table named account with different table definition.
This error indicates that your fixtures for same dataset (table e.g. user) has different column structure.
For example, you have default-fixtures.yaml that contains user table and it has the following fields
user: - username: john
But somewhere in other fixtures, you have same user table with different fields e.g.
user: - email: john
This will cause the above error. To fix this, just make sure you define the same fields on each fixtures.
- ErrorException: ini_set(): Cannot change zlib.output_compression - headers already sent
This happens when Nette has php ini zlib.output_compression enabled in the container. Removing this config during testing will fix this error