Zend_Test-PHPUnit-Testing.xml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 24249 -->
  3. <!-- Reviewed: no -->
  4. <sect2 id="zend.test.phpunit.testing">
  5. <title>Tester vos contrôleurs et vos applications MVC</title>
  6. <para>
  7. Une fois , votre fichier d'amorçage en place, vous pouvez commencer à tester. Tester
  8. est typiquement ce que vous auriez pu faire avec une suite de test PHPUnit ("test suite"),
  9. avec quelques petites différences mineures.
  10. </para>
  11. <para>
  12. Premièrement, vous devez distribuer l'URL à tester en utilisant la méthode
  13. <methodname>dispatch()</methodname> de TestCase :
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  17. {
  18. // ...
  19. public function testPageAccueil()
  20. {
  21. $this->dispatch('/');
  22. // ...
  23. }
  24. }
  25. ]]></programlisting>
  26. <para>
  27. Il y a des moments, cependant, où vous devez fournir des informations supplémentaires
  28. - des variables GET et POST, des informations de COOKIE, etc. Vous pouvez peupler la requête
  29. avec ces informations :
  30. </para>
  31. <programlisting language="php"><![CDATA[
  32. class FooControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  33. {
  34. // ...
  35. public function testBarActionShouldReceiveAllParameters()
  36. {
  37. // Passer les variables GET :
  38. $this->request->setQuery(array(
  39. 'foo' => 'bar',
  40. 'bar' => 'baz',
  41. ));
  42. // Passer les variables POST :
  43. $this->request->setPost(array(
  44. 'baz' => 'bat',
  45. 'lame' => 'bogus',
  46. ));
  47. // Paramètrer une valeur de cookie :
  48. $this->request->setCookie('user', 'matthew');
  49. // ou plusieurs :
  50. $this->request->setCookies(array(
  51. 'timestamp' => time(),
  52. 'host' => 'foobar',
  53. ));
  54. // Ajouter des en-têtes :
  55. $this->request->setHeader('X-Requested-With', 'XmlHttpRequest');
  56. // Définir le type de requête :
  57. $this->request->setMethod('POST');
  58. // Distribuer :
  59. $this->dispatch('/foo/bar');
  60. // ...
  61. }
  62. }
  63. ]]></programlisting>
  64. <para>Maintenant que la requête est construite, il est temps de créer des assertions.</para>
  65. <sect3 id="zend.test.phpunit.testing.redirector">
  66. <title>Controller Tests and the Redirector Action Helper</title>
  67. <important>
  68. <para>
  69. The redirect action helper issues an <methodname>exit()</methodname> statement
  70. when using the method <methodname>gotoAndExit()</methodname>
  71. and will then obviously also stops a test running for this method.
  72. For testability of your application dont use that method on the
  73. redirector!
  74. </para>
  75. </important>
  76. <para>
  77. Due to its nature the redirector action helper plugin issues a redirect
  78. and exists after this. Because you cannot test parts of an application
  79. that issue exit calls <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>
  80. automatically disables the exit part of the redirector which can cause
  81. different behaviours in tests and the real application. To make sure
  82. redirect work correctly you should it them in the following way:
  83. </para>
  84. <programlisting language="php"><![CDATA[
  85. class MyController extends Zend_Controller_Action
  86. {
  87. public function indexAction()
  88. {
  89. if($someCondition == true) {
  90. return $this->_redirect(...);
  91. } else if($anotherCondition == true) {
  92. $this->_redirector->gotoSimple("foo");
  93. return;
  94. }
  95. // do some stuff here
  96. }
  97. }
  98. ]]></programlisting>
  99. <important>
  100. <para>
  101. Depending on your application this is not enough as additional action, <methodname>preDispatch()</methodname> or
  102. <methodname>postDispatch()</methodname> logic might be executed. This cannot be handled in a good way with
  103. Zend Test currently.
  104. </para>
  105. </important>
  106. </sect3>
  107. </sect2>