Zend_Test-PHPUnit-Testing.xml 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 URL to test, using the
  12. <code>dispatch()</code> method of the TestCase:
  13. </para>
  14. <programlisting language="php"><![CDATA[
  15. class IndexControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
  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_Controller_TestCase
  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. </sect2>
  68. <!--
  69. vim:se ts=4 sw=4 et:
  70. -->