Zend_Test-PHPUnit-Bootstrapping.xml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <acronym>MVC</acronym> test cases should extend
  8. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname>. This class in turn
  9. extends <classname>PHPUnit_Framework_TestCase</classname>, 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 <acronym>MVC</acronym>
  12. implementation.
  13. </para>
  14. <para>
  15. In order to test your <acronym>MVC</acronym> application, you will need to bootstrap it.
  16. There are several ways to do this, all of which hinge on the public
  17. <varname>$bootstrap</varname> property.
  18. </para>
  19. <para>
  20. First, and probably most straight-forward, simply create a
  21. <classname>Zend_Application</classname> instance as you would in your
  22. <filename>index.php</filename>, and assign it to the <varname>$bootstrap</varname> property.
  23. Typically, you will do this in your <methodname>setUp()</methodname> method; you will need
  24. to call <methodname>parent::setUp()</methodname> when done:
  25. </para>
  26. <programlisting language="php"><![CDATA[
  27. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  28. {
  29. public function setUp()
  30. {
  31. // Assign and instantiate in one step:
  32. $this->bootstrap = new Zend_Application(
  33. 'testing',
  34. APPLICATION_PATH . '/configs/application.ini'
  35. );
  36. parent::setUp();
  37. }
  38. }
  39. ]]></programlisting>
  40. <para>
  41. Second, you can set this property to point to a file. If you do
  42. this, the file should <emphasis>not</emphasis> dispatch the front
  43. controller, but merely setup the front controller and any application
  44. specific needs.
  45. </para>
  46. <programlisting language="php"><![CDATA[
  47. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  48. {
  49. public $bootstrap = '/path/to/bootstrap/file.php'
  50. // ...
  51. }
  52. ]]></programlisting>
  53. <para>
  54. Third, you can provide a <acronym>PHP</acronym> callback to execute in order to bootstrap
  55. your application. This method is seen in the <link
  56. linkend="zend.test.phpunit.loginexample">Login example</link>. If
  57. the callback is a function or static method, this could be set at the
  58. class level:
  59. </para>
  60. <programlisting language="php"><![CDATA[
  61. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  62. {
  63. public $bootstrap = array('App', 'bootstrap');
  64. // ...
  65. }
  66. ]]></programlisting>
  67. <para>
  68. In cases where an object instance is necessary, we recommend performing
  69. this in your <methodname>setUp()</methodname> method:
  70. </para>
  71. <programlisting language="php"><![CDATA[
  72. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  73. {
  74. public function setUp()
  75. {
  76. // Use the 'start' method of a Bootstrap object instance:
  77. $bootstrap = new Bootstrap('test');
  78. $this->bootstrap = array($bootstrap, 'start');
  79. parent::setUp();
  80. }
  81. }
  82. ]]></programlisting>
  83. <para>
  84. Note the call to <methodname>parent::setUp()</methodname>; this is necessary, as
  85. the <methodname>setUp()</methodname> method of
  86. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> will perform the
  87. remainder of the bootstrapping process (which includes calling the
  88. callback).
  89. </para>
  90. <para>
  91. During normal operation, the <methodname>setUp()</methodname> method will bootstrap
  92. the application. This process first will include cleaning up the
  93. environment to a clean request state, resetting any plugins and
  94. helpers, resetting the front controller instance, and creating new
  95. request and response objects. Once this is done, it will then either
  96. <methodname>include()</methodname> the file specified in <varname>$bootstrap</varname>, or
  97. call the callback specified.
  98. </para>
  99. <para>
  100. Bootstrapping should be as close as possible to how the application
  101. will be bootstrapped. However, there are several caveats:
  102. </para>
  103. <itemizedlist>
  104. <listitem>
  105. <para>
  106. Do not provide alternate implementations of the Request and
  107. Response objects; they will not be used.
  108. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> uses custom
  109. request and response objects,
  110. <classname>Zend_Controller_Request_HttpTestCase</classname> and
  111. <classname>Zend_Controller_Response_HttpTestCase</classname>, respectively.
  112. These objects provide methods for setting up the request
  113. environment in targeted ways, and pulling response artifacts in
  114. specific ways.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <para>
  119. Do not expect to test server specifics. In other words, the tests
  120. are not a guarantee that the code will run on a specific server
  121. configuration, but merely that the application should run as
  122. expected should the router be able to route the given request. To
  123. this end, do not set server-specific headers in the request object.
  124. </para>
  125. </listitem>
  126. </itemizedlist>
  127. <para>
  128. Once the application is bootstrapped, you can then start creating
  129. your tests.
  130. </para>
  131. </sect2>
  132. <!--
  133. vim:se ts=4 sw=4 et:
  134. -->