public
Last active

Using multiple databases in phpunit/dbunit with composer

  • Download Gist
DatabaseTest.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/**
* Specific example for Zend Framework 1.0 applications (update the database "fetching")
*/
class DatabaseTest extends PHPUnit_Extensions_MultipleDatabase_TestCase
{
protected function getDatabaseConfigs()
{
$configs = array();
$options = $this->_application->getOptions();
$databases = $options['resources']['dbs'];
 
foreach ($databases as $database) {
$fixture = __DIR__ . '/_files/fixture_' . $database . '.xml';
if (is_file($fixture)) {
$tables = simplexml_load_file($fixture);
if (count($tables) == 0) {
// don't register databases with empty fixtures
continue;
}
unset($tables);
$builder = new PHPUnit_Extensions_MultipleDatabase_DatabaseConfig_Builder();
$builder->connection(
new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection(
$this->_application->getBootstrap()
->getPluginResource('Dbs')
->getDbAdapter($database)
->getConnection(),
$database
)
);
$builder->dataSet(
new PHPUnit_Extensions_Database_DataSet_XmlDataSet($fixture)
);
$configs[$database] = $builder->build();
}
}
 
return $configs;
}
 
/**
* Fetch a specific connection to do testing (rowcount, etc...)
*
* @param string $database The name of the database
*
* @return PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection
*/
public function getConnection($database)
{
$configs = $this->getDatabaseConfigs();
return $configs[$database]->getConnection();
}
 
}
SomeTest.php
PHP
1 2 3 4
$this->assertEquals(0, $this->getConnection('dbname')->getRowCount('user'), "Pre-Condition");
$user = new User();
$user->save();
$this->assertEquals(1, $this->getConnection('dbname')->getRowCount('user'), "Inserting failed");
composer.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
{
"repositories": [
{
"type": "package",
"package": {
"name": "hamcrest/hamcrest",
"version": "1.1.0",
"dist": {
"type": "zip",
"url": "https://hamcrest.googlecode.com/files/hamcrest-php-1.1.0.zip"
},
"include-path": ["Hamcrest-1.1.0/"],
"autoload": {
"psr-0": { "Hamcrest_": "Hamcrest-1.1.0/" },
"files": ["Hamcrest-1.1.0/Hamcrest/Hamcrest.php"]
}
}
},
{
"type": "package",
"package": {
"name": "etsy/phpunit-extensions",
"version": "0.6.0",
"dist": {
"type": "zip",
"url": "https://github.com/etsy/phpunit-extensions/archive/v0.6.0.zip"
},
"autoload": {
"psr-0": { "PHPUnit_": "" }
}
}
}
],
"require": {
"php": ">=5.3.2"
},
"require-dev": {
"hamcrest/hamcrest": "1.1.0",
"mockery/mockery": ">=0.7.2",
"etsy/phpunit-extensions": "0.6.0",
"phpunit/phpunit": "3.7.*",
"phpunit/dbunit": "1.2.*"
},
"include-path": ["vendor/mockery/mockery/library"]
}
gistfile1.sh
Shell
1 2 3 4
cd $PROJECT_ROOT
curl -s https://getcomposer.org/installer | php
php composer.phar install --dev
vendor/bin/phpunit --version

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.