1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
<?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>
|