| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect2 id="zend.test.phpunit.testing">
- <title>Testing your Controllers and MVC Applications</title>
- <para>
- Once you have your bootstrap in place, you can begin testing. Testing
- is basically as you would expect in an PHPUnit test suite, with a few
- minor differences.
- </para>
- <para>
- First, you will need to dispatch a URL to test, using the
- <code>dispatch()</code> method of the TestCase:
- </para>
- <programlisting language="php"><![CDATA[
- class IndexControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
- {
- // ...
- public function testHomePage()
- {
- $this->dispatch('/');
- // ...
- }
- }
- ]]></programlisting>
- <para>
- There will be times, however, that you need to provide extra
- information -- GET and POST variables, COOKIE information, etc. You can
- populate the request with that information:
- </para>
- <programlisting language="php"><![CDATA[
- class FooControllerTest extends Zend_Test_PHPUnit_Controller_TestCase
- {
- // ...
- public function testBarActionShouldReceiveAllParameters()
- {
- // Set GET variables:
- $this->request->setQuery(array(
- 'foo' => 'bar',
- 'bar' => 'baz',
- ));
- // Set POST variables:
- $this->request->setPost(array(
- 'baz' => 'bat',
- 'lame' => 'bogus',
- ));
- // Set a cookie value:
- $this->request->setCookie('user', 'matthew');
- // or many:
- $this->request->setCookies(array(
- 'timestamp' => time(),
- 'host' => 'foobar',
- ));
- // Set headers, even:
- $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
- // Set the request method:
- $this->request->setMethod('POST');
- // Dispatch:
- $this->dispatch('/foo/bar');
- // ...
- }
- }
- ]]></programlisting>
- <para>
- Now that the request is made, it's time to start making assertions
- against it.
- </para>
- </sect2>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|