Zend_Test_PHPUnit

Zend_Test_PHPUnit_Db

Coupling of data-access and the domain model often requires the use of a database for testing purposes. But the database is persistent across different tests which leads to test results that can affect each other. Furthermore setting up the database to be able to run a test is quite some work. PHPUnit's Database extension simplifies testing with a database by offering a very simple mechanism to set up and teardown the database between different tests. This component extends the PHPUnit Database extension with Zend Framework specific code, such that writing database tests against a Zend Framework application is simplified.

Database Testing can be explained with two conceptual entities, DataSets and DataTables. Internally the PHPUnit Database extension can build up an object structure of a database, its tables and containing rows from configuration files or the real database content. This abstract object graph can then be compared using assertions. A common use-case in database testing is setting up some tables with seed data, then performing some operations, and finally asserting that the operated on database-state is equal to some predefined expected state. Zend_Test_PHPUnit_Db simplifies this task by allowing to generate DataSets and DataTables from existing Zend_Db_Table_Abstract or Zend_Db_Table_Rowset_Abstract instances.

Furthermore this component allows to integrate any Zend_Db_Adapter_Abstract for testing whereas the original extension only works with PDO. A Test Adapter implementation for Zend_Db_Adapter_Abstract is also included in this component. It allows to instantiate a Db Adapter that requires no database at all and acts as an SQL and result stack which is used by the API methods.

Quickstart

Setup a Database TestCase

We are now writting some database tests for the Bug Database example in the Zend_Db_Table documentation. First we begin to test that inserting a new bug is actually saved in the database correctly. First we have to setup a test-class that extends Zend_Test_PHPUnit_DatabaseTestCase. This class extends the PHPUnit Database Extension, which in turn extends the basic PHPUnit_Framework_TestCase. A database testcase contains two abstract methods that have to be implemented, one for the database connection and one for the initial dataset that should be used as seed or fixture.

Note: You should be familiar with the PHPUnit Database extension to follow this quickstart easily. Although all the concepts are explained in this documentation it may be helpful to read the PHPUnit documentation first.

  1. class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase
  2. {
  3.     private $_connectionMock;
  4.  
  5.     /**
  6.      * Returns the test database connection.
  7.      *
  8.      * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
  9.      */
  10.     protected function getConnection()
  11.     {
  12.         if($this->_connectionMock == null) {
  13.             $connection = Zend_Db::factory(...);
  14.             $this->_connectionMock = $this->createZendDbConnection(
  15.                 $connection, 'zfunittests'
  16.             );
  17.             Zend_Db_Table_Abstract::setDefaultAdapter($connection);
  18.         }
  19.         return $this->_connectionMock;
  20.     }
  21.  
  22.     /**
  23.      * @return PHPUnit_Extensions_Database_DataSet_IDataSet
  24.      */
  25.     protected function getDataSet()
  26.     {
  27.         return $this->createFlatXmlDataSet(
  28.             dirname(__FILE__) . '/_files/bugsSeed.xml'
  29.         );
  30.     }
  31. }

Here we create the database connection and seed some data into the database. Some important details should be noted on this code:

  • You cannot directly return a Zend_Db_Adapter_Abstract from the getConnection() method, but a PHPUnit specific wrapper which is generated with the createZendDbConnection() method.

  • The database schema (tables and database) is not re-created on every testrun. The database and tables have to be created manually before running the tests.

  • Database tests by default truncate the data during setUp() and then insert the seed data which is returned from the getDataSet() method.

  • DataSets have to implement the interface PHPUnit_Extensions_Database_DataSet_IDataSet. There is a wide range of XML and YAML configuration file types included in PHPUnit which allows to specifiy how the tables and datasets should look like and you should look into the PHPUnit documentation to get the latest information on these dataset specifications.

Specify a seed dataset

In the previous setup for the database testcase we have specified a seed file for the database fixture. We now create this file specified in the Flat XML format:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <dataset>
  3.     <zfbugs bug_id="1" bug_description="system needs electricity to run"
  4.         bug_status="NEW" created_on="2007-04-01 00:00:00"
  5.         updated_on="2007-04-01 00:00:00" reported_by="goofy"
  6.         assigned_to="mmouse" verified_by="dduck" />
  7.     <zfbugs bug_id="2" bug_description="Implement Do What I Mean function"
  8.         bug_status="VERIFIED" created_on="2007-04-02 00:00:00"
  9.         updated_on="2007-04-02 00:00:00" reported_by="goofy"
  10.         assigned_to="mmouse" verified_by="dduck" />
  11.     <zfbugs bug_id="3" bug_description="Where are my keys?" bug_status="FIXED"
  12.         created_on="2007-04-03 00:00:00" updated_on="2007-04-03 00:00:00"
  13.         reported_by="dduck" assigned_to="mmouse" verified_by="dduck" />
  14.     <zfbugs bug_id="4" bug_description="Bug no product" bug_status="INCOMPLETE"
  15.         created_on="2007-04-04 00:00:00" updated_on="2007-04-04 00:00:00"
  16.         reported_by="mmouse" assigned_to="goofy" verified_by="dduck" />
  17. </dataset>

We will work with this four entries in the database table "zfbugs" in the next examples. The required MySQL schema for this example is:

  1. CREATE TABLE IF NOT EXISTS `zfbugs` (
  2.     `bug_id` int(11) NOT NULL AUTO_INCREMENT,
  3.     `bug_description` varchar(100) DEFAULT NULL,
  4.     `bug_status` varchar(20) DEFAULT NULL,
  5.     `created_on` datetime DEFAULT NULL,
  6.     `updated_on` datetime DEFAULT NULL,
  7.     `reported_by` varchar(100) DEFAULT NULL,
  8.     `assigned_to` varchar(100) DEFAULT NULL,
  9.     `verified_by` varchar(100) DEFAULT NULL,
  10. PRIMARY KEY  (`bug_id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=1 ;

A few initial database tests

Now that we have implemented the two required abstract methods of the Zend_Test_PHPUnit_DatabaseTestCase and specified the seed database content, which will be re-created for each new test, we can go about to make our first assertion. This will be a test to insert a new bug.

  1. class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase
  2. {
  3.     public function testBugInsertedIntoDatabase()
  4.     {
  5.         $bugsTable = new Bugs();
  6.  
  7.         $data = array(
  8.             'created_on'      => '2007-03-22 00:00:00',
  9.             'updated_on'      => '2007-03-22 00:00:00',
  10.             'bug_description' => 'Something wrong',
  11.             'bug_status'      => 'NEW',
  12.             'reported_by'     => 'garfield',
  13.             'verified_by'     => 'garfield',
  14.             'assigned_to'     => 'mmouse',
  15.         );
  16.  
  17.         $bugsTable->insert($data);
  18.  
  19.         $ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
  20.             $this->getConnection()
  21.         );
  22.         $ds->addTable('zfbugs', 'SELECT * FROM zfbugs');
  23.  
  24.         $this->assertDataSetsEqual(
  25.             $this->createFlatXmlDataSet(dirname(__FILE__)
  26.                                       . "/_files/bugsInsertIntoAssertion.xml"),
  27.             $ds
  28.         );
  29.     }
  30. }

Now up to the $bugsTable->insert($data); everything looks familiar. The lines after that contain the assertion methodname. We want to verify that after inserting the new bug the database has been updated correctly with the given data. For this we create a Zend_Test_PHPUnit_Db_DataSet_QueryDataSet instance and give it a database connection. We will then tell this dataset that it contains a table "zfbugs" which is given by an SQL statement. This current/actual state of the database is compared to the expected database state which is contained in another XML file "bugsInsertIntoAssertions.xml". This XML file is a slight deviation from the one given above and contains another row with the expected data:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <dataset>
  3.     <!-- previous 4 rows -->
  4.     <zfbugs bug_id="5" bug_description="Something wrong" bug_status="NEW"
  5.         created_on="2007-03-22 00:00:00" updated_on="2007-03-22 00:00:00"
  6.         reported_by="garfield" assigned_to="mmouse" verified_by="garfield" />
  7. </dataset>

There are other ways to assert that the current database state equals an expected state. The "Bugs" table in the example already knows a lot about its inner state, so why not use this to our advantage? The next example will assert that deleting from the database is possible:

  1. class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase
  2. {
  3.     public function testBugDelete()
  4.     {
  5.         $bugsTable = new Bugs();
  6.  
  7.         $bugsTable->delete(
  8.             $bugsTable->getAdapter()->quoteInto("bug_id = ?", 4)
  9.         );
  10.  
  11.         $ds = new Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet();
  12.         $ds->addTable($bugsTable);
  13.  
  14.         $this->assertDataSetsEqual(
  15.             $this->createFlatXmlDataSet(dirname(__FILE__)
  16.                                       . "/_files/bugsDeleteAssertion.xml"),
  17.             $ds
  18.         );
  19.     }
  20. }

We have created a Zend_Test_PHPUnit_Db_DataSet_DbTableDataSet dataset here, which takes any Zend_Db_Table_Abstract instance and adds it to the dataset with its table name, in this example "zfbugs". You could add several tables more if you wanted using the method addTable() if you want to check for expected database state in more than one table.

Here we only have one table and check against an expected database state in "bugsDeleteAssertion.xml" which is the original seed dataset without the row with id 4.

Since we have only checked that two specific tables (not datasets) are equal in the previous examples we should also look at how to assert that two tables are equal. Therefore we will add another test to our TestCase which verifies updating behaviour of a dataset.

  1. class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase
  2. {
  3.     public function testBugUpdate()
  4.     {
  5.         $bugsTable = new Bugs();
  6.  
  7.         $data = array(
  8.             'updated_on'      => '2007-05-23',
  9.             'bug_status'      => 'FIXED'
  10.         );
  11.  
  12.         $where = $bugsTable->getAdapter()->quoteInto('bug_id = ?', 1);
  13.  
  14.         $bugsTable->update($data, $where);
  15.  
  16.         $rowset = $bugsTable->fetchAll();
  17.  
  18.         $ds        = new Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset);
  19.         $assertion = $this->createFlatXmlDataSet(
  20.             dirname(__FILE__) . '/_files/bugsUpdateAssertion.xml'
  21.         );
  22.         $expectedRowsets = $assertion->getTable('zfbugs');
  23.  
  24.         $this->assertTablesEqual(
  25.             $expectedRowsets, $ds
  26.         );
  27.     }
  28. }

Here we create the current database state from a Zend_Db_Table_Rowset_Abstract instance in conjunction with the Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset) instance which creates an internal data-representation of the rowset. This can again be compared against another data-table by using the $this->assertTablesEqual() assertion.

Usage, API and Extensions Points

The Quickstart already gave a good introduction on how database testing can be done using PHPUnit and the Zend Framework. This section gives an overview over the API that the Zend_Test_PHPUnit_Db component comes with and how it works internally.

Note: Some Remarks on Database Testing
Just as the Controller TestCase is testing an application at an integration level, the Database TestCase is an integration testing method. Its using several different application layers for testing purposes and therefore should be consumed with caution.
It should be noted that testing domain and business logic with integration tests such as Zend Framework's Controller and Database TestCases is a bad practice. The purpose of an Integration test is to check that several parts of an application work smoothly when wired together. These integration tests do not replace the need for a set of unit tests that test the domain and business logic at a much smaller level, the isolated class.

The Zend_Test_PHPUnit_DatabaseTestCase class

The Zend_Test_PHPUnit_DatabaseTestCase class derives from the PHPUnit_Extensions_Database_TestCase which allows to setup tests with a fresh database fixture on each run easily. The Zend implementation offers some additional convenience features over the PHPUnit Database extension when it comes to using Zend_Db resources inside your tests. The workflow of a database test-case can be described as follows.

  1. For each test PHPUnit creates a new instance of the TestCase and calls the setUp() method.

  2. The Database TestCase creates an instance of a Database Tester which handles the setting up and tearing down of the database.

  3. The database tester collects the information on the database connection and initial dataset from getConnection() and getDataSet() which are both abstract methods and have to be implemented by any Database Testcase.

  4. By default the database tester truncates the tables specified in the given dataset, and then inserts the data given as initial fixture.

  5. When the database tester has finished setting up the database, PHPUnit runs the test.

  6. After running the test, tearDown() is called. Because the database is wiped in setUp() before inserting the required initial fixture, no actions are executed by the database tester at this stage.

Note: The Database TestCase expects the database schema and tables to be setup correctly to run the tests. There is no mechanism to create and tear down database tables.

The Zend_Test_PHPUnit_DatabaseTestCase class has some convenience functions that can help writing tests that interact with the database and the database testing extension.

The next table lists only the new methods compared to the PHPUnit_Extensions_Database_TestCase, whose » API is documented in the PHPUnit Documentation.

Zend_Test_PHPUnit_DatabaseTestCase API Methods
Method Description
createZendDbConnection(Zend_Db_Adapter_Abstract $connection, $schema) Create a PHPUnit Database Extension compatible Connection instance from a Zend_Db_Adapter_Abstract instance. This method should be used in for testcase setup when implementing the abstract getConnection() method of the database testcase.
getAdapter() Convenience method to access the underlying Zend_Db_Adapter_Abstract instance which is nested inside the PHPUnit database connection created with getConnection().
createDbRowset(Zend_Db_Table_Rowset_Abstract $rowset, $tableName = null) Create a DataTable Object that is filled with the data from a given Zend_Db_Table_Rowset_Abstract instance. The table the rowset is connected to is chosen when $tableName is NULL.
createDbTable(Zend_Db_Table_Abstract $table, $where = null, $order = null, $count = null, $offset = null) Create a DataTable object that represents the data contained in a Zend_Db_Table_Abstract instance. For retrieving the data fetchAll() is used, where the optional parameters can be used to restrict the data table to a certain subset.
createDbTableDataSet(array $tables=array()) Create a DataSet containing the given $tables, an array of Zend_Db_Table_Abstract instances.

Integrating Database Testing with the ControllerTestCase

Because PHP does not support multiple inheritance it is not possible to use the Controller and Database testcases in conjunction. However you can use the Zend_Test_PHPUnit_Db_SimpleTester database tester in your controller test-case to setup a database enviroment fixture for each new controller test. The Database TestCase in general is only a set of convenience functions which can also be accessed and used without the test case.

Example #1 Database integration example

This example extends the User Controller Test from the Zend_Test_PHPUnit_ControllerTestCase documentation to include a database setup.

  1. class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
  2. {
  3.     public function setUp()
  4.     {
  5.         $this->setupDatabase();
  6.         $this->bootstrap = array($this, 'appBootstrap');
  7.         parent::setUp();
  8.     }
  9.  
  10.     public function setupDatabase()
  11.     {
  12.         $db = Zend_Db::factory(...);
  13.         $connection = new Zend_Test_PHPUnit_Db_Connection($db,
  14.                                                       'database_schema_name');
  15.         $databaseTester = new Zend_Test_PHPUnit_Db_SimpleTester($connection);
  16.  
  17.         $databaseFixture =
  18.                     new PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet(
  19.                         dirname(__FILE__) . '/_files/initialUserFixture.xml'
  20.                     );
  21.  
  22.         $databaseTester->setupDatabase($databaseFixture);
  23.     }
  24. }

Now the Flat XML dataset "initialUserFixture.xml" is used to set the database into an initial state before each test, exactly as the DatabaseTestCase works internally.

Using the Database Testing Adapter

There are times when you don't want to test parts of your application with a real database, but are forced to because of coupling. The Zend_Test_DbAdapter offers a convenient way to use a implementation of Zend_Db_Adapter_Abstract without having to open a database connection. Furthermore this Adapter is very easy to mock from within your PHPUnit testsuite, since it requires no constructor arguments.

The Test Adapter acts as a stack for various database results. Its order of results have to be userland implemented, which might be a tedious task for tests that call many different database queries, but its just the right helper for tests where only a handful of queries are executed and you know the exact order of the results that have to be returned to your userland code.

  1. $adapter   = new Zend_Test_DbAdapter();
  2. $stmt1Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
  3. $stmt1     = Zend_Test_DbStatement::createSelectStatement($stmt1Rows);
  4. $adapter->appendStatementToStack($stmt1);
  5.  
  6. $stmt2Rows = array(array('foo' => 'bar'), array('foo' => 'baz'));
  7. $stmt2     = Zend_Test_DbStatement::createSelectStatement($stmt2Rows);
  8. $adapter->appendStatementToStack($stmt2);
  9.  
  10. $rs = $adapter->query('SELECT ...'); // Returns Statement 2
  11. while ($row = $rs->fetch()) {
  12.     echo $rs['foo']; // Prints "Bar", "Baz"
  13. }
  14. $rs = $adapter->query('SELECT ...'); // Returns Statement 1

Behaviour of any real database adapter is simulated as much as possible such that methods like fetchAll(), fetchObject(), fetchColumn and more are working for the test adapter.

You can also put INSERT, UPDATE and DELETE statement onto the result stack, these however only return a statement which allows to specifiy the result of $stmt->rowCount().

  1. $adapter = new Zend_Test_DbAdapter();
  2. $adapter->appendStatementToStack(
  3.     Zend_Test_DbStatement::createInsertStatement(1)
  4. );
  5. $adapter->appendStatementToStack(
  6.     Zend_Test_DbStatement::createUpdateStatement(2)
  7. );
  8. $adapter->appendStatementToStack(
  9.     Zend_Test_DbStatement::createDeleteStatement(10
  10. ));

By default the query profiler is enabled, so that you can retrieve the executed SQL statements and their bound parameters to check for the correctness of the execution.

  1. $adapter = new Zend_Test_DbAdapter();
  2. $stmt = $adapter->query("SELECT * FROM bugs");
  3.  
  4. $qp = $adapter->getProfiler()->getLastQueryProfile();
  5.  
  6. echo $qp->getQuerY(); // SELECT * FROM bugs

The test adapter never checks if the query specified is really of the type SELECT, DELETE, INSERT or UPDATE which is returned next from the stack. The correct order of returning the data has to be implemented by the user of the test adapter.

The Test adapter also specifies methods to simulate the use of the methods listTables(), describeTables() and lastInsertId(). Additionally using the setQuoteIdentifierSymbol() you can specify which symbol should be used for quoting, by default none is used.


Zend_Test_PHPUnit