| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- EN-Revision: 24249 -->
- <!-- Reviewed: no -->
- <sect2 id="zend.test.phpunit.testing">
- <title>Tester vos contrôleurs et vos applications MVC</title>
- <para>
- Une fois , votre fichier d'amorçage en place, vous pouvez commencer à tester. Tester
- est typiquement ce que vous auriez pu faire avec une suite de test PHPUnit ("test suite"),
- avec quelques petites différences mineures.
- </para>
- <para>
- Premièrement, vous devez distribuer l'URL à tester en utilisant la méthode
- <methodname>dispatch()</methodname> de TestCase :
- </para>
- <programlisting language="php"><![CDATA[
- class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
- {
- // ...
- public function testPageAccueil()
- {
- $this->dispatch('/');
- // ...
- }
- }
- ]]></programlisting>
- <para>
- Il y a des moments, cependant, où vous devez fournir des informations supplémentaires
- - des variables GET et POST, des informations de COOKIE, etc. Vous pouvez peupler la requête
- avec ces informations :
- </para>
- <programlisting language="php"><![CDATA[
- class FooControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
- {
- // ...
- public function testBarActionShouldReceiveAllParameters()
- {
- // Passer les variables GET :
- $this->request->setQuery(array(
- 'foo' => 'bar',
- 'bar' => 'baz',
- ));
- // Passer les variables POST :
- $this->request->setPost(array(
- 'baz' => 'bat',
- 'lame' => 'bogus',
- ));
- // Paramètrer une valeur de cookie :
- $this->request->setCookie('user', 'matthew');
- // ou plusieurs :
- $this->request->setCookies(array(
- 'timestamp' => time(),
- 'host' => 'foobar',
- ));
- // Ajouter des en-têtes :
- $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
- // Définir le type de requête :
- $this->request->setMethod('POST');
- // Distribuer :
- $this->dispatch('/foo/bar');
- // ...
- }
- }
- ]]></programlisting>
- <para>Maintenant que la requête est construite, il est temps de créer des assertions.</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 stops a test running for this method.
- For testability of your application dont use that method on the
- redirector!
- </para>
- </important>
- <para>
- Due to its nature the redirector action helper plugin issues a redirect
- and exists after this. 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 which can cause
- different behaviours in tests and the real application. To make sure
- redirect work correctly you should it them in the following way:
- </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>
|