| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <!-- EN-Revision: 24662 -->
- <sect2 id="zend.test.phpunit.testing">
- <title>コントローラおよび MVC アプリケーションのテスト</title>
- <para>
- 起動用の設定を済ませたら、テストの開始です。
- テストの方法は PHPUnit テストスイートによるものとほぼ同じですが、
- ちょっとした違いがいくつかあります。
- </para>
- <para>
- まず、テストケースの <methodname>dispatch()</methodname>
- メソッドを用いてテストの <acronym>URL</acronym> をディスパッチしなければなりません。
- </para>
- <programlisting language="php"><![CDATA[
- class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
- {
- // ...
- public function testHomePage()
- {
- $this->dispatch('/');
- // ...
- }
- }
- ]]></programlisting>
- <para>
- しかし、時にはこれ以外の情報 (<constant>GET</constant> 変数や POST 変数、
- COOKIE 情報など) が必要になることもあります。
- これらの情報をリクエストに含めることもできます。
- </para>
- <programlisting language="php"><![CDATA[
- class FooControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
- {
- // ...
- public function testBarActionShouldReceiveAllParameters()
- {
- // GET 変数を設定します
- $this->request->setQuery(array(
- 'foo' => 'bar',
- 'bar' => 'baz',
- ));
- // POST 変数を設定します
- $this->request->setPost(array(
- 'baz' => 'bat',
- 'lame' => 'bogus',
- ));
- // クッキーの値を指定します
- $this->request->setCookie('user', 'matthew');
- // あるいは複数の値を指定します
- $this->request->setCookies(array(
- 'timestamp' => time(),
- 'host' => 'foobar',
- ));
- // ヘッダを設定することもできます
- $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
- // リクエストメソッドを設定します
- $this->request->setMethod('POST');
- // ディスパッチします
- $this->dispatch('/foo/bar');
- // ...
- }
- }
- ]]></programlisting>
- <para>
- リクエストが準備できたので、次はアサーションを作成してみましょう。
- </para>
- <sect3 id="zend.test.phpunit.testing.redirector">
- <title>コントローラのテストと Redirector アクションヘルパー</title>
- <important>
- <para>
- Redirect アクションヘルパーは、<methodname>gotoAndExit()</methodname>
- メソッドを使うときに <methodname>exit()</methodname>
- ステートメントを発行し、このメソッドを使用するコントローラーに対するテストを全て停止させます。
- アプリケーションのテスト容易性を考慮して、
- リダイレクタではこのメソッドを使わないようにしましょう。
- </para>
- </important>
- <para>
- その性質上、リダイレクタアクションヘルパープラグインは
- リダイレクトしたあと処理を終了します。
- <!-- to be translated -->
- Because you cannot test parts of an application
- that issue exit calls <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>
- automatically disables the exit part of the redirector as it can cause
- test behavior to differ from the real application. To ensure your controllers can
- be properly tested, please make use of the redirector when you need to redirect
- the user to a different page:
- </para>
- <programlisting language="php"><![CDATA[
- class MyController extends Zend_Controller_Action
- {
- public function indexAction()
- {
- if($someCondition == true) {
- return $this->_redirect(...);
- } else if($anotherCondition == true) {
- $this->_redirector->gotoSimple("foo");
- return;
- }
- // do some stuff here
- }
- }
- ]]></programlisting>
- <important>
- <para>
- アプリケーションによっては、これだけでは不十分かもしれません。さらに
- <methodname>preDispatch()</methodname> あるいは
- <methodname>postDispatch()</methodname> といったロジックを実行するかもしれないからです。
- 現状の Zend Test では、これらをうまく処理することはできません。
- </para>
- </important>
- </sect3>
- </sect2>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|