Zend_Test-PHPUnit.xml 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.test.phpunit" xmlns:xi="http://www.w3.org/2001/XInclude">
  5. <title>Zend_Test_PHPUnit(日本語)</title>
  6. <para>
  7. <classname>Zend_Test_PHPUnit</classname> は
  8. <acronym>MVC</acronym> アプリケーション向けのテストケースを用意します。
  9. さまざまな責務に対応したテスト用のアサーションが含まれています。
  10. 実際に何ができるのかを知るには、
  11. サンプルを見ていただくのが一番でしょう。
  12. </para>
  13. <example id="zend.test.phpunit.loginexample">
  14. <title>Application Login TestCase のサンプル</title>
  15. <para>
  16. 以下に示すのは
  17. <classname>UserController</classname>
  18. 用のシンプルなテストケースで、以下のような内容を検証します。
  19. </para>
  20. <itemizedlist>
  21. <listitem>
  22. <para>
  23. ログインフォームは、未認証のユーザに対しても表示されること。
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. ユーザがログインしたら、自分のプロファイルページにリダイレクトされること。
  29. そしてプロファイルページには、関連する情報が表示されること。
  30. </para>
  31. </listitem>
  32. </itemizedlist>
  33. <para>
  34. この例は、いくつかの前提条件のもとに作成されています。
  35. まず、起動時の設定のほとんどをプラグインに追い出しました。
  36. これにより、環境設定が簡潔になったのおで
  37. テストケースの準備がしやすくなりました。
  38. また、アプリケーションの起動処理が 1 行で書けるようになっています。
  39. また、autoloading の設定を行うことで、
  40. (コントローラやプラグインなどの)
  41. 適切なクラスをいちいち require することを考えなくてすむようにしています。
  42. </para>
  43. <programlisting language="php"><![CDATA[
  44. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  45. {
  46. public function setUp()
  47. {
  48. $this->bootstrap = array($this, 'appBootstrap');
  49. parent::setUp();
  50. }
  51. public function appBootstrap()
  52. {
  53. $this->frontController
  54. ->registerPlugin(new Bugapp_Plugin_Initialize('development'));
  55. }
  56. public function testCallWithoutActionShouldPullFromIndexAction()
  57. {
  58. $this->dispatch('/user');
  59. $this->assertController('user');
  60. $this->assertAction('index');
  61. }
  62. public function testIndexActionShouldContainLoginForm()
  63. {
  64. $this->dispatch('/user');
  65. $this->assertAction('index');
  66. $this->assertQueryCount('form#loginForm', 1);
  67. }
  68. public function testValidLoginShouldGoToProfilePage()
  69. {
  70. $this->request->setMethod('POST')
  71. ->setPost(array(
  72. 'username' => 'foobar',
  73. 'password' => 'foobar'
  74. ));
  75. $this->dispatch('/user/login');
  76. $this->assertRedirectTo('/user/view');
  77. $this->resetRequest()
  78. ->resetResponse();
  79. $this->request->setMethod('GET')
  80. ->setPost(array());
  81. $this->dispatch('/user/view');
  82. $this->assertRoute('default');
  83. $this->assertModule('default');
  84. $this->assertController('user');
  85. $this->assertAction('view');
  86. $this->assertNotRedirect();
  87. $this->assertQuery('dl');
  88. $this->assertQueryContentContains('h2', 'User: foobar');
  89. }
  90. }
  91. ]]></programlisting>
  92. <para>
  93. この例は、もう少しシンプルに書くこともできます。
  94. ここで示したアサーションのすべてが必須というわけではなく、
  95. 単に説明のためだけに用意しているものもあるからです。
  96. アプリケーションのテストがいかにシンプルにできるのか、
  97. この例でご理解いただけることでしょう。
  98. </para>
  99. </example>
  100. <xi:include href="Zend_Test-PHPUnit-Bootstrapping.xml" />
  101. <xi:include href="Zend_Test-PHPUnit-Testing.xml" />
  102. <xi:include href="Zend_Test-PHPUnit-Assertions.xml" />
  103. <xi:include href="Zend_Test-PHPUnit-Examples.xml" />
  104. </sect1>
  105. <!--
  106. vim:se ts=4 sw=4 et:
  107. -->