-
Notifications
You must be signed in to change notification settings - Fork 27
Multiple Database
This is a tutorial for using https://github.com/etsy/phpunit-extensions/tree/master/PHPUnit/Extensions/MultipleDatabase.
DBUnit was originally written in Java to work with JUnit. It was then ported to PHP as an extension of PHPUnit (as explained in the PHPUnit documentation).
DBUnit operates on the premise that the object under test accesses a single database, but many projects are moving to Sharded Database Architectures which results in a unit possibly having to talk to multiple database shards. Simply
-
extend PHPUnit_Extensions_MultipleDatabase_TestCase
-
override
protected function getDatabaseConfigs()
-
use the
PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder
to create aPHPUnit_Extensions_MultipleDatabase_DatabaseConfig
object -
return an array of the
PHPUnit_Extensions_MultipleDatabase_DatabaseConfig
objects<?php /** * @covers MyClass */ class MyClassTest extends PHPUnit_Extensions_MultipleDatabase_TestCase { ... test functions here ... protected function getDatabaseConfigs() { $configs = array(); $builder = new PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder(); $configs[] = $builder ->connection( new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection( new PDO($dsn_a, $username, $port), $handle_a ) ) ->dataSet(new PHPUnit_Extensions_Database_DataSet_YamlDataSet('_files/shard_a.yml')) ->build(); $builder = new PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder(); $configs[] = $builder ->connection( new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection( new PDO($dsn_b, $username, $port), $handle_b ) ) ->dataSet(new PHPUnit_Extensions_Database_DataSet_YamlDataSet('_files/shard_b.yml')) ->build(); return $configs; } } ?>
The PHPUnit_Extensions_MultipleDatabase_DatabaseConfig
encapsulates all of the information that the PHPUnit_Extensions_Database_DefaultTester
needs. The PHPUnit_Extensions_MultipleDatabase_TestCase
creates these testers
for the DBUnit framework to be able to invoke setUp
and tearDown
PHPUnit_Extensions_Database_Operation
on each database connection. To help build these configs, there is a builder. The default setUp
operation is a CLEAN_INSERT
and the default tearDown
operation is TRUNCATE
.
/**
* A connection must be provided.
*
* @param PHPUnit_Extensions_Database_DB_IDatabaseConnection connection
* @return PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder
*/
public function connection($connection);
/**
* A data set must be provided.
*
* @param PHPUnit_Extensions_Database_DataSet_IDataSet dataSet
* @return PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder
*/
public function dataSet($dataSet);
/**
* Default set up operation is CLEAN_INSERT with cascading deletes.
*
* @param PHPUnit_Extensions_Database_Operation_IDatabaseOperation setUpOperation
* @return PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder
*/
public function setUpOperation($setUpOperation);
/**
* Default tear down operation is NONE.
*
* @param PHPUnit_Extensions_Database_Operation_IDatabaseOperation tearDownOperation
* @return PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder
*/
public function tearDownOperation($tearDownOperation);
/**
* @return PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder
*/
public function build();
how to port all tests to PHPHUnit 6.3? PHP Fatal error: Interface 'PHPUnit_Extensions_Database_Operation_IDatabaseOperation' not found