Zend_Test-PHPUnit-Bootstrapping.xml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.test.phpunit.bootstrapping">
  4. <title>Bootstrapping your TestCase</title>
  5. <para>
  6. As noted in the <link linkend="zend.test.phpunit.loginexample">Login
  7. example</link>, all MVC test cases should extend
  8. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>. This class in turn
  9. extends <code>PHPUnit_Framework_TestCase</code>, and gives you all the
  10. structure and assertions you'd expect from PHPUnit -- as well as some
  11. scaffolding and assertions specific to Zend Framework's MVC
  12. implementation.
  13. </para>
  14. <para>
  15. In order to test your MVC application, you will need to bootstrap it.
  16. There are several ways to do this, all of which hinge on the public
  17. <code>$bootstrap</code> property.
  18. </para>
  19. <para>
  20. First, you can set this property to point to a file. If you do
  21. this, the file should <emphasis>not</emphasis> dispatch the front
  22. controller, but merely setup the front controller and any application
  23. specific needs.
  24. </para>
  25. <programlisting language="php"><![CDATA[
  26. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  27. {
  28. public $bootstrap = '/path/to/bootstrap/file.php'
  29. // ...
  30. }
  31. ]]></programlisting>
  32. <para>
  33. Second, you can provide a PHP callback to execute in order to bootstrap
  34. your application. This method is seen in the <link
  35. linkend="zend.test.phpunit.loginexample">Login example</link>. If
  36. the callback is a function or static method, this could be set at the
  37. class level:
  38. </para>
  39. <programlisting language="php"><![CDATA[
  40. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  41. {
  42. public $bootstrap = array('App', 'bootstrap');
  43. // ...
  44. }
  45. ]]></programlisting>
  46. <para>
  47. In cases where an object instance is necessary, we recommend performing
  48. this in your <code>setUp()</code> method:
  49. </para>
  50. <programlisting language="php"><![CDATA[
  51. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  52. {
  53. public function setUp()
  54. {
  55. // Use the 'start' method of a Bootstrap object instance:
  56. $bootstrap = new Bootstrap('test');
  57. $this->bootstrap = array($bootstrap, 'start');
  58. parent::setUp();
  59. }
  60. }
  61. ]]></programlisting>
  62. <para>
  63. Note the call to <code>parent::setUp()</code>; this is necessary, as
  64. the <code>setUp()</code> method of
  65. <classname>Zend_Test_PHPUnit_Controller_TestCase</classname> will perform the
  66. remainder of the bootstrapping process (which includes calling the
  67. callback).
  68. </para>
  69. <para>
  70. During normal operation, the <code>setUp()</code> method will bootstrap
  71. the application. This process first will include cleaning up the
  72. environment to a clean request state, resetting any plugins and
  73. helpers, resetting the front controller instance, and creating new
  74. request and response objects. Once this is done, it will then either
  75. <code>include</code> the file specified in <code>$bootstrap</code>, or
  76. call the callback specified.
  77. </para>
  78. <para>
  79. Bootstrapping should be as close as possible to how the application
  80. will be bootstrapped. However, there are several caveats:
  81. </para>
  82. <itemizedlist>
  83. <listitem><para>
  84. Do not provide alternate implementations of the Request and
  85. Response objects; they will not be used.
  86. <classname>Zend_Test_PHPUnit_Controller_TestCase</classname> uses custom
  87. request and response objects,
  88. <classname>Zend_Controller_Request_HttpTestCase</classname> and
  89. <classname>Zend_Controller_Response_HttpTestCase</classname>, respectively.
  90. These objects provide methods for setting up the request
  91. environment in targeted ways, and pulling response artifacts in
  92. specific ways.
  93. </para></listitem>
  94. <listitem><para>
  95. Do not expect to test server specifics. In other words, the tests
  96. are not a guarantee that the code will run on a specific server
  97. configuration, but merely that the application should run as
  98. expected should the router be able to route the given request. To
  99. this end, do not set server-specific headers in the request object.
  100. </para></listitem>
  101. </itemizedlist>
  102. <para>
  103. Once the application is bootstrapped, you can then start creating
  104. your tests.
  105. </para>
  106. </sect2>
  107. <!--
  108. vim:se ts=4 sw=4 et:
  109. -->