2
0

Zend_Test-PHPUnit.xml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.test.phpunit" xmlns:xi="http://www.w3.org/2001/XInclude">
  4. <title>Zend_Test_PHPUnit</title>
  5. <para>
  6. <classname>Zend_Test_PHPUnit</classname> provides a TestCase for <acronym>MVC</acronym>
  7. applications that contains assertions for testing against a variety of
  8. responsibilities. Probably the easiest way to understand what it can do
  9. is to see an example.
  10. </para>
  11. <example id="zend.test.phpunit.loginexample">
  12. <title>Application Login TestCase example</title>
  13. <para>
  14. The following is a simple test case for a
  15. <classname>UserController</classname> to verify several things:
  16. </para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>
  20. The login form should be displayed to non-authenticated users.
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. When a user logs in, they should be redirected to their profile
  26. page, and that profile page should show relevant information.
  27. </para>
  28. </listitem>
  29. </itemizedlist>
  30. <para>
  31. This particular example assumes a few things. First, we're moving
  32. most of our bootstrapping to a plugin. This simplifies setup of the
  33. test case as it allows us to specify our environment succinctly,
  34. and also allows us to bootstrap the application in a single line.
  35. Also, our particular example is assuming that autoloading is setup
  36. so we do not need to worry about requiring the appropriate classes
  37. (such as the correct controller, plugin, etc).
  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. This example could be written somewhat simpler -- not all the
  90. assertions shown are necessary, and are provided for illustration
  91. purposes only. Hopefully, it shows how simple it can be to test
  92. your applications.
  93. </para>
  94. </example>
  95. <xi:include href="Zend_Test-PHPUnit-Bootstrapping.xml" />
  96. <xi:include href="Zend_Test-PHPUnit-Testing.xml" />
  97. <xi:include href="Zend_Test-PHPUnit-Assertions.xml" />
  98. <xi:include href="Zend_Test-PHPUnit-Examples.xml" />
  99. </sect1>
  100. <!--
  101. vim:se ts=4 sw=4 et:
  102. -->