Zend_Test-PHPUnit-Db-Testing.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.test.phpunit.db.testing">
  4. <title>Usage, API and Extensions Points</title>
  5. <para>
  6. The Quickstart already gave a good introduction on how database testing can be done using
  7. PHPUnit and the Zend Framework. This section gives an overview over the
  8. <acronym>API</acronym> that the <classname>Zend_Test_PHPUnit_Db</classname> component comes
  9. with and how it works internally.
  10. </para>
  11. <note>
  12. <title>Some Remarks on Database Testing</title>
  13. <para>
  14. Just as the Controller TestCase is testing an application at an integration level, the
  15. Database TestCase is an integration testing method. Its using several different
  16. application layers for testing purposes and therefore should be consumed with caution.
  17. </para>
  18. <para>
  19. It should be noted that testing domain and business logic with integration tests such
  20. as Zend Framework's Controller and Database TestCases is a bad practice. The purpose of
  21. an Integration test is to check that several parts of an application work smoothly when
  22. wired together. These integration tests do not replace the need for a set of unit tests
  23. that test the domain and business logic at a much smaller level, the isolated class.
  24. </para>
  25. </note>
  26. <sect3 id="zend.test.phpunit.db.testing.testcase">
  27. <title>The Zend_Test_PHPUnit_DatabaseTestCase class</title>
  28. <para>
  29. The <classname>Zend_Test_PHPUnit_DatabaseTestCase</classname> class derives from the
  30. <classname>PHPUnit_Extensions_Database_TestCase</classname> which allows to setup tests
  31. with a fresh database fixture on each run easily. The Zend implementation offers some
  32. additional convenience features over the PHPUnit Database extension when it comes to
  33. using <classname>Zend_Db</classname> resources inside your tests. The workflow of a
  34. database test-case can be described as follows.
  35. </para>
  36. <orderedlist>
  37. <listitem>
  38. <para>
  39. For each test PHPUnit creates a new instance of the TestCase and calls the
  40. <methodname>setUp()</methodname> method.
  41. </para>
  42. </listitem>
  43. <listitem>
  44. <para>
  45. The Database TestCase creates an instance of a Database Tester which handles the
  46. setting up and tearing down of the database.
  47. </para>
  48. </listitem>
  49. <listitem>
  50. <para>
  51. The database tester collects the information on the database connection and
  52. initial dataset from <methodname>getConnection()</methodname> and
  53. <methodname>getDataSet()</methodname> which are both abstract methods and have
  54. to be implemented by any Database Testcase.
  55. </para>
  56. </listitem>
  57. <listitem>
  58. <para>
  59. By default the database tester truncates the tables specified in the given
  60. dataset, and then inserts the data given as initial fixture.
  61. </para>
  62. </listitem>
  63. <listitem>
  64. <para>
  65. When the database tester has finished setting up the database, PHPUnit runs the
  66. test.
  67. </para>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. After running the test, <methodname>tearDown()</methodname> is called. Because
  72. the database is wiped in <methodname>setUp()</methodname> before inserting the
  73. required initial fixture, no actions are executed by the database tester at this
  74. stage.
  75. </para>
  76. </listitem>
  77. </orderedlist>
  78. <note>
  79. <para>
  80. The Database TestCase expects the database schema and tables to be setup correctly
  81. to run the tests. There is no mechanism to create and tear down database tables.
  82. </para>
  83. </note>
  84. <para>
  85. The <classname>Zend_Test_PHPUnit_DatabaseTestCase</classname> class has some convenience
  86. functions that can help writing tests that interact with the database and the database
  87. testing extension.
  88. </para>
  89. <para>
  90. The next table lists only the new methods compared to the
  91. <classname>PHPUnit_Extensions_Database_TestCase</classname>, whose <ulink
  92. url="http://www.phpunit.de/manual/current/en/database.html">API is documented in
  93. the PHPUnit Documentation</ulink>.
  94. </para>
  95. <table id="zend.test.phpunit.db.testing.testcase.api-methods">
  96. <title>Zend_Test_PHPUnit_DatabaseTestCase API Methods</title>
  97. <tgroup cols="2">
  98. <thead>
  99. <row>
  100. <entry>Method</entry>
  101. <entry>Description</entry>
  102. </row>
  103. </thead>
  104. <tbody>
  105. <row>
  106. <entry>
  107. <methodname>createZendDbConnection(Zend_Db_Adapter_Abstract $connection,
  108. $schema)</methodname>
  109. </entry>
  110. <entry>
  111. Create a PHPUnit Database Extension compatible Connection instance from
  112. a <classname>Zend_Db_Adapter_Abstract</classname> instance. This method
  113. should be used in for testcase setup when implementing the abstract
  114. <methodname>getConnection()</methodname> method of the database
  115. testcase.
  116. </entry>
  117. </row>
  118. <row>
  119. <entry><methodname>getAdapter()</methodname></entry>
  120. <entry>
  121. Convenience method to access the underlying
  122. <classname>Zend_Db_Adapter_Abstract</classname> instance which is nested
  123. inside the PHPUnit database connection created with
  124. <methodname>getConnection()</methodname>.
  125. </entry>
  126. </row>
  127. <row>
  128. <entry>
  129. <methodname>createDbRowset(Zend_Db_Table_Rowset_Abstract $rowset,
  130. $tableName = null)</methodname>
  131. </entry>
  132. <entry>
  133. Create a DataTable Object that is filled with the data from a given
  134. <classname>Zend_Db_Table_Rowset_Abstract</classname> instance. The table
  135. the rowset is connected to is chosen when <varname>$tableName</varname>
  136. is <constant>NULL</constant>.
  137. </entry>
  138. </row>
  139. <row>
  140. <entry>
  141. <methodname>createDbTable(Zend_Db_Table_Abstract $table, $where = null,
  142. $order = null, $count = null, $offset = null)</methodname>
  143. </entry>
  144. <entry>
  145. Create a DataTable object that represents the data contained in a
  146. <classname>Zend_Db_Table_Abstract</classname> instance. For retrieving
  147. the data <methodname>fetchAll()</methodname> is used, where the optional
  148. parameters can be used to restrict the data table to a certain subset.
  149. </entry>
  150. </row>
  151. <row>
  152. <entry>
  153. <methodname>createDbTableDataSet(array $tables=array())</methodname>
  154. </entry>
  155. <entry>
  156. Create a DataSet containing the given <varname>$tables</varname>, an
  157. array of <classname>Zend_Db_Table_Abstract</classname> instances.
  158. </entry>
  159. </row>
  160. </tbody>
  161. </tgroup>
  162. </table>
  163. </sect3>
  164. <sect3 id="zend.test.phpunit.db.testing.controllerintegration">
  165. <title>Integrating Database Testing with the ControllerTestCase</title>
  166. <para>
  167. Because <acronym>PHP</acronym> does not support multiple inheritance it is not possible
  168. to use the Controller and Database testcases in conjunction. However you can use the
  169. <classname>Zend_Test_PHPUnit_Db_SimpleTester</classname> database tester in your
  170. controller test-case to setup a database enviroment fixture for each new controller
  171. test. The Database TestCase in general is only a set of convenience functions which can
  172. also be accessed and used without the test case.
  173. </para>
  174. <example id="zend.test.phpunit.db.testing.controllerintegration.example">
  175. <title>Database integration example</title>
  176. <para>
  177. This example extends the User Controller Test from the
  178. <classname>Zend_Test_PHPUnit_ControllerTestCase</classname> documentation to include
  179. a database setup.
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  183. {
  184. public function setUp()
  185. {
  186. $this->setupDatabase();
  187. $this->bootstrap = array($this, 'appBootstrap');
  188. parent::setUp();
  189. }
  190. public function setupDatabase()
  191. {
  192. $db = Zend_Db::factory(...);
  193. $connection = new Zend_Test_PHPUnit_Db_Connection($db,
  194. 'database_schema_name');
  195. $databaseTester = new Zend_Test_PHPUnit_Db_SimpleTester($connection);
  196. $databaseFixture =
  197. new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
  198. dirname(__FILE__) . '/_files/initialUserFixture.xml'
  199. );
  200. $databaseTester->setupDatabase($databaseFixture);
  201. }
  202. }
  203. ]]></programlisting>
  204. <para>
  205. Now the Flat <acronym>XML</acronym> dataset "initialUserFixture.xml" is used to set
  206. the database into an initial state before each test, exactly as the DatabaseTestCase
  207. works internally.
  208. </para>
  209. </example>
  210. </sect3>
  211. </sect2>