2
0

Zend_Test-PHPUnit-Testing.xml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 -- <constant>GET</constant> 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 against it.
  65. </para>
  66. <sect3 id="zend.test.phpunit.testing.redirector">
  67. <title>Controller Tests and the Redirector Action Helper</title>
  68. <important>
  69. <para>
  70. The redirect action helper issues an <methodname>exit()</methodname> statement
  71. when using the method <methodname>gotoAndExit()</methodname>
  72. and will then obviously also stop any tests running against controllers
  73. using this method. For testability of your application dont use that
  74. method of the redirector!
  75. </para>
  76. </important>
  77. <para>
  78. Due to its nature the redirector action helper plugin issues a redirect
  79. and then exits. Because you cannot test parts of an application
  80. that issue exit calls <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>
  81. automatically disables the exit part of the redirector as it can cause
  82. test behavior to differ from the real application. To ensure your controllers can
  83. be properly tested, please make use of the redirector when you need to redirect
  84. the user to a different page:
  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,
  104. <methodname>preDispatch()</methodname> or <methodname>postDispatch()</methodname>
  105. logic might be executed. This cannot be handled in a good way with Zend Test
  106. currently.
  107. </para>
  108. </important>
  109. </sect3>
  110. </sect2>
  111. <!--
  112. vim:se ts=4 sw=4 et:
  113. -->