Zend_Test-PHPUnit.xml 4.5 KB

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