Zend_Test-PHPUnit-Db-Adapter.xml 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 17054 -->
  3. <!-- Reviewed: no -->
  4. <sect2 id="zend.test.phpunit.db.adapter">
  5. <title>Using the Database Testing Adapter</title>
  6. <para>
  7. There are times when you don't want to test parts of your application with a real database,
  8. but are forced to because of coupling. The <classname>Zend_Test_DbAdapter</classname> offers
  9. a convenient way to use a implementation of <classname>Zend_Db_Adapter_Abstract</classname>
  10. without having to open a database connection. Furthermore this Adapter is very easy to mock
  11. from within your PHPUnit testsuite, since it requires no constructor arguments.
  12. </para>
  13. <para>
  14. The Test Adapter acts as a stack for various database results. Its order of results have to
  15. be userland implemented, which might be a tedious task for tests that call many different
  16. database queries, but its just the right helper for tests where only a handful of queries
  17. are executed and you know the exact order of the results that have to be returned to your
  18. userland code.
  19. </para>
  20. <programlisting language="php"><![CDATA[
  21. $adapter = new Zend_Test_DbAdapter();
  22. $stmt1Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
  23. $stmt1 = Zend_Test_DbStatement::createSelectStatement($stmt1Rows);
  24. $adapter->appendStatementToStack($stmt1);
  25. $stmt2Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
  26. $stmt2 = Zend_Test_DbStatement::createSelectStatement($stmt2Rows);
  27. $adapter->appendStatementToStack($stmt2);
  28. $rs = $adapter->query('SELECT ...'); // Returns Statement 2
  29. while ($row = $rs->fetch()) {
  30. echo $rs['foo']; // Prints "Bar", "Baz"
  31. }
  32. $rs = $adapter->query('SELECT ...'); // Returns Statement 1
  33. ]]></programlisting>
  34. <para>
  35. Behaviour of any real database adapter is simulated as much as possible such that methods
  36. like <methodname>fetchAll()</methodname>, <methodname>fetchObject()</methodname>,
  37. <methodname>fetchColumn</methodname> and more are working for the test adapter.
  38. </para>
  39. <para>
  40. You can also put INSERT, UPDATE and DELETE statement onto the result stack, these however
  41. only return a statement which allows to specifiy the result of
  42. <methodname>$stmt->rowCount()</methodname>.
  43. </para>
  44. <programlisting language="php"><![CDATA[
  45. $adapter = new Zend_Test_DbAdapter();
  46. $adapter->appendStatementToStack(
  47. Zend_Test_DbStatement::createInsertStatement(1)
  48. );
  49. $adapter->appendStatementToStack(
  50. Zend_Test_DbStatement::createUpdateStatement(2)
  51. );
  52. $adapter->appendStatementToStack(
  53. Zend_Test_DbStatement::createDeleteStatement(10)
  54. );
  55. ]]></programlisting>
  56. <para>
  57. The test adapter never checks if the query specified is really of the type SELECT, DELETE,
  58. INSERT or UPDATE which is returned next from the stack. The correct order of returning the
  59. data has to be implemented by the user of the test adapter.
  60. </para>
  61. <para>
  62. The Test adapter also specifies methods to simulate the use of the methods
  63. <methodname>listTables()</methodname>, <methodname>describeTables()</methodname> and
  64. <methodname>lastInsertId()</methodname>.
  65. </para>
  66. </sect2>