| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?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 <acronym>URL</acronym> to test, using the
- <methodname>dispatch()</methodname> method of the TestCase:
- </para>
- <programlisting language="php"><![CDATA[
- class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
- {
- // ...
- public function testHomePage()
- {
- $this->dispatch('/');
- // ...
- }
- }
- ]]></programlisting>
- <para>
- There will be times, however, that you need to provide extra
- information -- <constant>GET</constant> 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_ControllerTestCase
- {
- // ...
- 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>
- <sect3 id="zend.test.phpunit.testing.redirector">
- <title>Controller Tests and the Redirector Action Helper</title>
- <important>
- <para>
- The redirect action helper issues an <methodname>exit()</methodname> statement
- when using the method <methodname>gotoAndExit()</methodname>
- and will then obviously also stop any tests running against controllers
- using this method. For testability of your application dont use that
- method of the redirector!
- </para>
- </important>
- <para>
- Due to its nature the redirector action helper plugin issues a redirect
- and then exits. Because you cannot test parts of an application
- that issue exit calls <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>
- automatically disables the exit part of the redirector as it can cause
- test behavior to differ from the real application. To ensure your controllers can
- be properly tested, please make use of the redirector when you need to redirect
- the user to a different page:
- </para>
- <programlisting language="php"><![CDATA[
- class MyController extends Zend_Controller_Action
- {
- public function indexAction()
- {
- if($someCondition == true) {
- return $this->_redirect(...);
- } else if($anotherCondition == true) {
- $this->_redirector->gotoSimple("foo");
- return;
- }
- // do some stuff here
- }
- }
- ]]></programlisting>
- <important>
- <para>
- Depending on your application this is not enough as additional action,
- <methodname>preDispatch()</methodname> or <methodname>postDispatch()</methodname>
- logic might be executed. This cannot be handled in a good way with Zend Test
- currently.
- </para>
- </important>
- </sect3>
- </sect2>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|