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.
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.
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
Zend_Db resources inside your tests. The workflow of a
database test-case can be described as follows.
For each test PHPUnit creates a new instance of the TestCase and calls the
The Database TestCase creates an instance of a Database Tester which handles the setting up and tearing down of the database.
The database tester collects the information on the database connection and initial dataset from
getDataSet()which are both abstract methods and have to be implemented by any Database Testcase.
By default the database tester truncates the tables specified in the given dataset, and then inserts the data given as initial fixture.
When the database tester has finished setting up the database, PHPUnit runs the test.
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.
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.
Zend_Test_PHPUnit_DatabaseTestCase class has some convenience
functions that can help writing tests that interact with the database and the database
The next table lists only the new methods compared to the
PHPUnit_Extensions_Database_TestCase, whose API is documented in
the PHPUnit Documentation.
Table 162. Zend_Test_PHPUnit_DatabaseTestCase API Methods
Create a PHPUnit Database Extension compatible Connection instance from
Convenience method to access the underlying
Create a DataTable Object that is filled with the data from a given
Create a DataTable object that represents the data contained in a
Create a DataSet containing the given
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 939. Database integration example
This example extends the User Controller Test from the
Zend_Test_PHPUnit_ControllerTestCase documentation to include
a database setup.
class UserControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
public function setUp()
$this->bootstrap = array($this, 'appBootstrap');
public function setupDatabase()
$db = Zend_Db::factory(...);
$connection = new Zend_Test_PHPUnit_Db_Connection($db,
$databaseTester = new Zend_Test_PHPUnit_Db_SimpleTester($connection);
dirname(__FILE__) . '/_files/initialUserFixture.xml'
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.