Zend_Test-PHPUnit-Testing.xml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24662 -->
  4. <sect2 id="zend.test.phpunit.testing">
  5. <title>コントローラおよび MVC アプリケーションのテスト</title>
  6. <para>
  7. 起動用の設定を済ませたら、テストの開始です。
  8. テストの方法は PHPUnit テストスイートによるものとほぼ同じですが、
  9. ちょっとした違いがいくつかあります。
  10. </para>
  11. <para>
  12. まず、テストケースの <methodname>dispatch()</methodname>
  13. メソッドを用いてテストの <acronym>URL</acronym> をディスパッチしなければなりません。
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  17. {
  18. // ...
  19. public function testHomePage()
  20. {
  21. $this->dispatch('/');
  22. // ...
  23. }
  24. }
  25. ]]></programlisting>
  26. <para>
  27. しかし、時にはこれ以外の情報 (<constant>GET</constant> 変数や POST 変数、
  28. COOKIE 情報など) が必要になることもあります。
  29. これらの情報をリクエストに含めることもできます。
  30. </para>
  31. <programlisting language="php"><![CDATA[
  32. class FooControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  33. {
  34. // ...
  35. public function testBarActionShouldReceiveAllParameters()
  36. {
  37. // GET 変数を設定します
  38. $this->request->setQuery(array(
  39. 'foo' => 'bar',
  40. 'bar' => 'baz',
  41. ));
  42. // POST 変数を設定します
  43. $this->request->setPost(array(
  44. 'baz' => 'bat',
  45. 'lame' => 'bogus',
  46. ));
  47. // クッキーの値を指定します
  48. $this->request->setCookie('user', 'matthew');
  49. // あるいは複数の値を指定します
  50. $this->request->setCookies(array(
  51. 'timestamp' => time(),
  52. 'host' => 'foobar',
  53. ));
  54. // ヘッダを設定することもできます
  55. $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
  56. // リクエストメソッドを設定します
  57. $this->request->setMethod('POST');
  58. // ディスパッチします
  59. $this->dispatch('/foo/bar');
  60. // ...
  61. }
  62. }
  63. ]]></programlisting>
  64. <para>
  65. リクエストが準備できたので、次はアサーションを作成してみましょう。
  66. </para>
  67. <sect3 id="zend.test.phpunit.testing.redirector">
  68. <title>コントローラのテストと Redirector アクションヘルパー</title>
  69. <important>
  70. <para>
  71. Redirect アクションヘルパーは、<methodname>gotoAndExit()</methodname>
  72. メソッドを使うときに <methodname>exit()</methodname>
  73. ステートメントを発行し、このメソッドを使用するコントローラーに対するテストを全て停止させます。
  74. アプリケーションのテスト容易性を考慮して、
  75. リダイレクタではこのメソッドを使わないようにしましょう。
  76. </para>
  77. </important>
  78. <para>
  79. その性質上、リダイレクタアクションヘルパープラグインは
  80. リダイレクトしたあと処理を終了します。
  81. <!-- to be translated -->
  82. Because you cannot test parts of an application
  83. that issue exit calls <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>
  84. automatically disables the exit part of the redirector as it can cause
  85. test behavior to differ from the real application. To ensure your controllers can
  86. be properly tested, please make use of the redirector when you need to redirect
  87. the user to a different page:
  88. </para>
  89. <programlisting language="php"><![CDATA[
  90. class MyController extends Zend_Controller_Action
  91. {
  92. public function indexAction()
  93. {
  94. if($someCondition == true) {
  95. return $this->_redirect(...);
  96. } else if($anotherCondition == true) {
  97. $this->_redirector->gotoSimple("foo");
  98. return;
  99. }
  100. // do some stuff here
  101. }
  102. }
  103. ]]></programlisting>
  104. <important>
  105. <para>
  106. アプリケーションによっては、これだけでは不十分かもしれません。さらに
  107. <methodname>preDispatch()</methodname> あるいは
  108. <methodname>postDispatch()</methodname> といったロジックを実行するかもしれないからです。
  109. 現状の Zend Test では、これらをうまく処理することはできません。
  110. </para>
  111. </important>
  112. </sect3>
  113. </sect2>
  114. <!--
  115. vim:se ts=4 sw=4 et:
  116. -->