Zend_Test-PHPUnit-Testing.xml 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. <important>
  6. <para>
  7. The redirect action helper issues an <code>exit()</code> statement in
  8. some situtions which obviously also stops a test running for this method.
  9. </para>
  10. </important>
  11. <para>
  12. Once you have your bootstrap in place, you can begin testing. Testing
  13. is basically as you would expect in an PHPUnit test suite, with a few
  14. minor differences.
  15. </para>
  16. <para>
  17. First, you will need to dispatch a <acronym>URL</acronym> to test, using the
  18. <methodname>dispatch()</methodname> method of the TestCase:
  19. </para>
  20. <programlisting language="php"><![CDATA[
  21. class IndexControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
  22. {
  23. // ...
  24. public function testHomePage()
  25. {
  26. $this->dispatch('/');
  27. // ...
  28. }
  29. }
  30. ]]></programlisting>
  31. <para>
  32. There will be times, however, that you need to provide extra
  33. information -- GET and POST variables, COOKIE information, etc. You can
  34. populate the request with that information:
  35. </para>
  36. <programlisting language="php"><![CDATA[
  37. class FooControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
  38. {
  39. // ...
  40. public function testBarActionShouldReceiveAllParameters()
  41. {
  42. // Set GET variables:
  43. $this->request->setQuery(array(
  44. 'foo' => 'bar',
  45. 'bar' => 'baz',
  46. ));
  47. // Set POST variables:
  48. $this->request->setPost(array(
  49. 'baz' => 'bat',
  50. 'lame' => 'bogus',
  51. ));
  52. // Set a cookie value:
  53. $this->request->setCookie('user', 'matthew');
  54. // or many:
  55. $this->request->setCookies(array(
  56. 'timestamp' => time(),
  57. 'host' => 'foobar',
  58. ));
  59. // Set headers, even:
  60. $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
  61. // Set the request method:
  62. $this->request->setMethod('POST');
  63. // Dispatch:
  64. $this->dispatch('/foo/bar');
  65. // ...
  66. }
  67. }
  68. ]]></programlisting>
  69. <para>
  70. Now that the request is made, it's time to start making assertions
  71. against it.
  72. </para>
  73. </sect2>
  74. <!--
  75. vim:se ts=4 sw=4 et:
  76. -->