| Prev | Next |
While creating tests for your software you may come across database code that needs to be unit tested. The database extension has been created to provide an easy way to place your database in a known state, execute your database-effecting code, and ensure that the expected data is found in the database.
The quickest way to create a new Database Unit Test is to extend the PHPUnit_Extensions_Database_TestCase class. This class provides the functionality to create a database connection, seed your database with data, and after executing a test comparing the contents of your database with a dataset that can be built in a variety of formats. In Example 9.1 you can see examples of getConnection() and getDataSet() implementations.
Example 9.1: Setting up a database test case
<?php
require_once 'PHPUnit/Extensions/Database/TestCase.php';
class DatabaseTest extends PHPUnit_Extensions_Database_TestCase
{
protected function getConnection()
{
$pdo = new PDO('mysql:host=localhost;dbname=testdb', 'root', '');
return $this->createDefaultDBConnection($pdo, 'testdb');
}
protected function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__).'/_files/bank-account-seed.xml');
}
}
?>
The getConnection() method must return an implementation of the PHPUnit_Extensions_Database_DB_IDatabaseConnection interface. The createDefaultDBConnection() method can be used to return a database connection. It accepts a PDO object as the first parameter and the name of the schema you are testing against as the second parameter.
The getDataSet() method must return an implementation of the PHPUnit_Extensions_Database_DataSet_IDataSet interface. There are currently three different data sets available in PHPUnit. These datasets are discussed in the section called “Datasets”
Table 9.1. Database Test Case Methods
| Method | Meaning |
|---|---|
PHPUnit_Extensions_Database_DB_IDatabaseConnection getConnection()
|
Implement to return the database connection that will be checked for expected data sets and tables. |
PHPUnit_Extensions_Database_DataSet_IDataSet getDataSet()
|
Implement to return the data set that will be used in in database set up and tear down operations. |
PHPUnit_Extensions_Database_Operation_DatabaseOperation getSetUpOperation()
|
Override to return a specific operation that should be performed on the test database at the beginning of each test. The various operations are detailed in the section called “Operations”. |
PHPUnit_Extensions_Database_Operation_DatabaseOperation getTearDownOperation()
|
Override to return a specific operation that should be performed on the test database at the end of each test. The various operations are detailed in the section called “Operations”. |
PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection createDefaultDBConnection(PDO $connection, string $schema)
|
Return a database connection wrapper around the $connection PDO object. The database schema being tested against should be specified by $schema. The result of this method can be returned from getConnection().
|
PHPUnit_Extensions_Database_DataSet_FlatXmlDataSet createFlatXMLDataSet(string $xmlFile)
|
Returns a flat xml dataset that is created from the xml file located at the absolute path specified in $xmlFile. More details about flat xml files can be found in the section called “Flat XML Data Set”. The result of this method can be returned from getDataSet().
|
PHPUnit_Extensions_Database_DataSet_XmlDataSet createXMLDataSet(string $xmlFile)
|
Returns a xml dataset that is created from the xml file located at the absolute path specified in $xmlFile. More details about xml files can be found in the section called “XML Data Set”. The result of this method can be returned from getDataSet().
|
void assertTablesEqual(PHPUnit_Extensions_Database_DataSet_ITable $expected, PHPUnit_Extensions_Database_DataSet_ITable $actual)
|
Reports an error if the contents of the $expected table do not match the contents in the $actual table.
|
void assertDataSetsEqual(PHPUnit_Extensions_Database_DataSet_IDataSet $expected, PHPUnit_Extensions_Database_DataSet_IDataSet $actual)
|
Reports an error if the contents of the $expected data set do not match the contents in the $actual data set.
|
| Prev | Next |
Copyright © 2005-2011 Sebastian Bergmann.