|
|
(Next) PEAR Installer: Core components (Installer API, REST, error handling) |
||||
| |
|||||
|
|||||
ユニットテストが外部のサービス (データベースや LDAP サーバ、あるいは企業のウェブサービスなど) に依存することもしばしばあります。 これらのサービスにアクセスするには認証情報が必要となります。 ユーザ名とパスワードの組み合わせや API キーなどです。
こういった機密情報は一般に公開するものではありませんし、 ユニットテスト内にそんな情報を含めてはいけません。 そのかわりに、設定ファイルのテンプレートである config.php.dist を同梱するようにしましょう。 テストを実行するユーザは、このファイルのコピーを作成して config.php という名前で保存し、内容を調整します。
ディレクトリ構成の例
tests/
config.php.dist
config.php
設定テンプレートの例
<?php
$GLOBALS['My_Package_UnittestConfig'] = array(
'host' => 'FIXME',
'username' => 'FIXME',
'password' => 'FIXME',
'host' => 'FIXME',
);
?>
設定ファイルが見つからない場合でも、die() するのではなくユニットテストを続行させなければなりません。 これは、複数のパッケージをまとめて一度にテストする際などに重要となります。
設定ファイルが存在するかどうかは、まず最初にチェックしなければなりません。
設定ファイルが存在するかどうかのチェック
<?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.');
}
//...
}
//...
}
?>
|
|
(Next) PEAR Installer: Core components (Installer API, REST, error handling) |
||||||||
| |
|||||||||
|
|||||||||