Skip to content

Commit

Permalink
Update README + include UNITTEST_CREATE_SCHEMA flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rezigned committed Jan 13, 2016
1 parent 0cbb0b8 commit f616df8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ In order to use Database test case, you must extend your base class from `HQ\Tes
1. `getContainer` - This method will return `Nette\Di\Container` instance.
2. `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** -
* **baseSchema** - Database schema which includes `CREATE TABLE ...` sql statements.
3. `getBaseFixtureDir` - Return a base directory for searching base fixture file (see

```php
Expand Down Expand Up @@ -61,7 +61,7 @@ This is a primary fixtures that will be loaded for all test cases (think of it a
```txt
test
+- YourBaseDbTestCase.php
+- default-fixtures.{yaml, json, php}
+- <connection-name>-fixtures.{yaml, json, php} e.g. default-fixtures.json
```

##### 2. class fixtures
Expand All @@ -71,7 +71,7 @@ This will be loaded each time test case was invoked.
test
+- User
+- UserTest.php
+- default-fixtures.{yaml, json, php}
+- <connection-name>-fixtures.{yaml, json, php} e.g. default-fixtures.json
```

##### 3. instance fixtures
Expand All @@ -83,7 +83,7 @@ class MyTest extends MyBaseDbTestCase
public function getFixtures()
{
return [
'default' => [
'<connection-name>' => [
...
]
]
Expand Down Expand Up @@ -134,6 +134,8 @@ return [
]
]
```
## 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

Expand All @@ -155,4 +157,6 @@ user:
This will cause the above error. To fix this, just make sure you define the *same fields* on each fixtures.


* zlib.output_compression
* 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
18 changes: 18 additions & 0 deletions src/AbstractDbTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ abstract class AbstractDbTestCase extends AbstractTestCase
{
use PHPUnit_Extensions_Database_TestCase_Trait;

const ENV_CREATE_SCHEMA = 'UNITTEST_CREATE_SCHEMA';

protected $initialized;
protected static $started;

Expand All @@ -28,6 +30,13 @@ abstract class AbstractDbTestCase extends AbstractTestCase
*/
private $databaseTesters = [];

/**
* This will improve test speed when we execute it later
*
* @var bool
*/
protected $forceCreateSchema = true;

/**
* @return AbstractConnection[]
*/
Expand Down Expand Up @@ -80,6 +89,10 @@ protected function init()

protected function initDatabases()
{
if (!$this->shouldCreateSchema()) {
return;
}

foreach($this->getInitializedConnections() as $connection) {

// build schema
Expand Down Expand Up @@ -175,4 +188,9 @@ protected function getInitializedConnections()

return $this->connections = $this->getConnections();
}

private function shouldCreateSchema()
{
return filter_var(getenv(self::ENV_CREATE_SCHEMA) ?: $this->forceCreateSchema, FILTER_VALIDATE_BOOLEAN) === true;
}
}

0 comments on commit f616df8

Please sign in to comment.