Zend_Test-PHPUnit-Testing.xml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 14978 -->
  4. <sect2 id="zend.test.phpunit.testing">
  5. <title>コントローラおよび MVC アプリケーションのテスト</title>
  6. <para>
  7. 起動用の設定を済ませたら、テストの開始です。
  8. テストの方法は PHPUnit テストスイートによるものとほぼ同じですが、
  9. ちょっとした違いがいくつかあります。
  10. </para>
  11. <para>
  12. まず、テストケースの <code>dispatch()</code>
  13. メソッドを用いてテストの URL をディスパッチしなければなりません。
  14. </para>
  15. <programlisting role="php"><![CDATA[
  16. class IndexControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
  17. {
  18. // ...
  19. public function testHomePage()
  20. {
  21. $this->dispatch('/');
  22. // ...
  23. }
  24. }
  25. ]]>
  26. </programlisting>
  27. <para>
  28. しかし、時にはこれ以外の情報 (GET 変数や POST 変数、
  29. COOKIE 情報など) が必要になることもあります。
  30. これらの情報をリクエストに含めることもできます。
  31. </para>
  32. <programlisting role="php"><![CDATA[
  33. class FooControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
  34. {
  35. // ...
  36. public function testBarActionShouldReceiveAllParameters()
  37. {
  38. // GET 変数を設定します
  39. $this->request->setQuery(array(
  40. 'foo' => 'bar',
  41. 'bar' => 'baz',
  42. ));
  43. // POST 変数を設定します
  44. $this->request->setPost(array(
  45. 'baz' => 'bat',
  46. 'lame' => 'bogus',
  47. ));
  48. // クッキーの値を指定します
  49. $this->request->setCookie('user', 'matthew');
  50. // あるいは複数の値を指定します
  51. $this->request->setCookies(array(
  52. 'timestamp' => time(),
  53. 'host' => 'foobar',
  54. ));
  55. // ヘッダを設定することもできます
  56. $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
  57. // リクエストメソッドを設定します
  58. $this->request->setMethod('POST');
  59. // ディスパッチします
  60. $this->dispatch('/foo/bar');
  61. // ...
  62. }
  63. }
  64. ]]>
  65. </programlisting>
  66. <para>
  67. リクエストが準備できたので、次はアサーションを作成してみましょう。
  68. </para>
  69. </sect2>
  70. <!--
  71. vim:se ts=4 sw=4 et:
  72. -->