1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect2 id="zend.test.phpunit.db.adapter">
4 <title>Using the Database Testing Adapter</title>
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.
15 The Test Adapter acts as a stack for various database results. Its order of results have to
16 be userland implemented, which might be a tedious task for tests that call many different
17 database queries, but its just the right helper for tests where only a handful of queries
18 are executed and you know the exact order of the results that have to be returned to your
22 <programlisting language="php"><![CDATA[
23 $adapter = new Zend_Test_DbAdapter();
24 $stmt1Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
25 $stmt1 = Zend_Test_DbStatement::createSelectStatement($stmt1Rows);
26 $adapter->appendStatementToStack($stmt1);
28 $stmt2Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
29 $stmt2 = Zend_Test_DbStatement::createSelectStatement($stmt2Rows);
30 $adapter->appendStatementToStack($stmt2);
32 $rs = $adapter->query('SELECT ...'); // Returns Statement 2
33 while ($row = $rs->fetch()) {
34 echo $rs['foo']; // Prints "Bar", "Baz"
36 $rs = $adapter->query('SELECT ...'); // Returns Statement 1
40 Behaviour of any real database adapter is simulated as much as possible such that methods
41 like <methodname>fetchAll()</methodname>, <methodname>fetchObject()</methodname>,
42 <methodname>fetchColumn</methodname> and more are working for the test adapter.
46 You can also put INSERT, UPDATE and DELETE statement onto the result stack, these however
47 only return a statement which allows to specifiy the result of
48 <methodname>$stmt->rowCount()</methodname>.
51 <programlisting language="php"><![CDATA[
52 $adapter = new Zend_Test_DbAdapter();
53 $adapter->appendStatementToStack(
54 Zend_Test_DbStatement::createInsertStatement(1)
56 $adapter->appendStatementToStack(
57 Zend_Test_DbStatement::createUpdateStatement(2)
59 $adapter->appendStatementToStack(
60 Zend_Test_DbStatement::createDeleteStatement(10
65 By default the query profiler is enabled, so that you can retrieve the executed SQL
66 statements and their bound parameters to check for the correctness of the execution.
69 <programlisting language="php"><![CDATA[
70 $adapter = new Zend_Test_DbAdapter();
71 $stmt = $adapter->query("SELECT * FROM bugs");
73 $qp = $adapter->getProfiler()->getLastQueryProfile();
75 echo $qp->getQuerY(); // SELECT * FROM bugs
79 The test adapter never checks if the query specified is really of the type SELECT, DELETE,
80 INSERT or UPDATE which is returned next from the stack. The correct order of returning the
81 data has to be implemented by the user of the test adapter.
85 The Test adapter also specifies methods to simulate the use of the methods
86 <methodname>listTables()</methodname>, <methodname>describeTables()</methodname> and
87 <methodname>lastInsertId()</methodname>. Additionally using the
88 <methodname>setQuoteIdentifierSymbol()</methodname> you can specify which
89 symbol should be used for quoting, by default none is used.