Zend_Test-PHPUnit-Testing.xml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.test.phpunit.testing">
  4. <title>Testing your Controllers and MVC Applications</title>
  5. <para>
  6. Once you have your bootstrap in place, you can begin testing. Testing
  7. is basically as you would expect in an PHPUnit test suite, with a few
  8. minor differences.
  9. </para>
  10. <para>
  11. First, you will need to dispatch a <acronym>URL</acronym> to test, using the
  12. <methodname>dispatch()</methodname> method of the TestCase:
  13. </para>
  14. <programlisting language="php"><![CDATA[
  15. class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  16. {
  17. // ...
  18. public function testHomePage()
  19. {
  20. $this->dispatch('/');
  21. // ...
  22. }
  23. }
  24. ]]></programlisting>
  25. <para>
  26. There will be times, however, that you need to provide extra
  27. information -- GET and POST variables, COOKIE information, etc. You can
  28. populate the request with that information:
  29. </para>
  30. <programlisting language="php"><![CDATA[
  31. class FooControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  32. {
  33. // ...
  34. public function testBarActionShouldReceiveAllParameters()
  35. {
  36. // Set GET variables:
  37. $this->request->setQuery(array(
  38. 'foo' => 'bar',
  39. 'bar' => 'baz',
  40. ));
  41. // Set POST variables:
  42. $this->request->setPost(array(
  43. 'baz' => 'bat',
  44. 'lame' => 'bogus',
  45. ));
  46. // Set a cookie value:
  47. $this->request->setCookie('user', 'matthew');
  48. // or many:
  49. $this->request->setCookies(array(
  50. 'timestamp' => time(),
  51. 'host' => 'foobar',
  52. ));
  53. // Set headers, even:
  54. $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
  55. // Set the request method:
  56. $this->request->setMethod('POST');
  57. // Dispatch:
  58. $this->dispatch('/foo/bar');
  59. // ...
  60. }
  61. }
  62. ]]></programlisting>
  63. <para>
  64. Now that the request is made, it's time to start making assertions
  65. against it.
  66. </para>
  67. <sect3 id="zend.test.phpunit.testing.redirector">
  68. <title>Controller Tests and the Redirector Action Helper</title>
  69. <important>
  70. <para>
  71. The redirect action helper issues an <methodname>exit()</methodname> statement
  72. when using the method <methodname>gotoAndExit()</methodname>
  73. and will then obviously also stops a test running for this method.
  74. For testability of your application dont use that method on the
  75. redirector!
  76. </para>
  77. </important>
  78. <para>
  79. Due to its nature the redirector action helper plugin issues a redirect
  80. and exists after this. Because you cannot test parts of an application
  81. that issue exit calls <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>
  82. automatically disables the exit part of the redirector which can cause
  83. different behaviours in tests and the real application. To make sure
  84. redirect work correctly you should it them in the following way:
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. class MyController extends Zend_Controller_Action
  88. {
  89. public function indexAction()
  90. {
  91. if($someCondition == true) {
  92. return $this->_redirect(...);
  93. } else if($anotherCondition == true) {
  94. $this->_redirector->gotoSimple("foo");
  95. return;
  96. }
  97. // do some stuff here
  98. }
  99. }
  100. ]]></programlisting>
  101. <important>
  102. <para>
  103. Depending on your application this is not enough as additional action, <methodname>preDispatch()</methodname> or
  104. <methodname>postDispatch()</methodname> logic might be executed. This cannot be handled in a good way with
  105. Zend Test currently.
  106. </para>
  107. </important>
  108. </sect3>
  109. </sect2>
  110. <!--
  111. vim:se ts=4 sw=4 et:
  112. -->