Zend_Test_PHPUnit(日本語)
Zend_Test_PHPUnit は
MVC アプリケーション向けのテストケースを用意します。
さまざまな責務に対応したテスト用のアサーションが含まれています。
実際に何ができるのかを知るには、
サンプルを見ていただくのが一番でしょう。
Application Login TestCase のサンプル
以下に示すのは
UserController
用のシンプルなテストケースで、以下のような内容を検証します。
ログインフォームは、未認証のユーザに対しても表示されること。
ユーザがログインしたら、自分のプロファイルページにリダイレクトされること。
そしてプロファイルページには、関連する情報が表示されること。
この例は、いくつかの前提条件のもとに作成されています。
まず、起動時の設定のほとんどをプラグインに追い出しました。
これにより、環境設定が簡潔になったのおで
テストケースの準備がしやすくなりました。
また、アプリケーションの起動処理が 1 行で書けるようになっています。
また、autoloading の設定を行うことで、
(コントローラやプラグインなどの)
適切なクラスをいちいち require することを考えなくてすむようにしています。
bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->frontController
->registerPlugin(new Bugapp_Plugin_Initialize('development'));
}
public function testCallWithoutActionShouldPullFromIndexAction()
{
$this->dispatch('/user');
$this->assertController('user');
$this->assertAction('index');
}
public function testIndexActionShouldContainLoginForm()
{
$this->dispatch('/user');
$this->assertAction('index');
$this->assertQueryCount('form#loginForm', 1);
}
public function testValidLoginShouldGoToProfilePage()
{
$this->request->setMethod('POST')
->setPost(array(
'username' => 'foobar',
'password' => 'foobar'
));
$this->dispatch('/user/login');
$this->assertRedirectTo('/user/view');
$this->resetRequest()
->resetResponse();
$this->request->setMethod('GET')
->setPost(array());
$this->dispatch('/user/view');
$this->assertRoute('default');
$this->assertModule('default');
$this->assertController('user');
$this->assertAction('view');
$this->assertNotRedirect();
$this->assertQuery('dl');
$this->assertQueryContentContains('h2', 'User: foobar');
}
}
]]>
この例は、もう少しシンプルに書くこともできます。
ここで示したアサーションのすべてが必須というわけではなく、
単に説明のためだけに用意しているものもあるからです。
アプリケーションのテストがいかにシンプルにできるのか、
この例でご理解いただけることでしょう。