Unit test configuration
Every now and then your unit tests depend on external services - be it a database or LDAP server or a company's web service. Access to those services and servers require creditentials, be it username and password combinations or API keys.
Such confidential information may often not be distributed, and
your unit tests should not have them coded into.
Instead a separate config file template,
config.php.dist
, should be shipped.
To run the tests, the user creates a copy of that configuration
template, saves it as config.php
and adjusts it.
Exemplary directory layout
tests/
config.php.dist
config.php
Exemplary configuration template
<?php
$GLOBALS['My_Package_UnittestConfig'] = array(
'host' => 'FIXME',
'username' => 'FIXME',
'password' => 'FIXME',
'host' => 'FIXME',
);
?>
You should not die() if no config file is found but let the unit test continue gracefully - this is important in combined suites, when several packages are unit tested in a row.
Instead, you should check if it exists first:
Checking if the configuration file exists
<?php
//...
class My_Package_ClassTest extends PHPUnit_Framework_TestCase
{
protected $configExists = null;
//...
public function __construct($name = null)
{
parent::__construct($name);
$configFile = dirname(__FILE__) . '/config.php';
$this->configExists = file_exists($configFile);
if ($this->configExists) {
include_once $configFile;
}
}
//...
public function setUp()
{
if (!$this->configExists) {
$this->markTestSkipped('Unit test configuration is missing.');
}
//...
}
//...
}
?>