Prev Next

Chapter 9. Database Testing

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.

Datasets

...

Flat XML Data Set

...

XML Data Set

...

Operations

...

Database Testing Best Practices

...

Prev Next
1. Automating Tests
2. PHPUnit's Goals
3. Installing PHPUnit
4. Writing Tests for PHPUnit
Data Providers
Testing Exceptions
Testing PHP Errors
5. The Command-Line Test Runner
6. Fixtures
More setUp() than tearDown()
Variations
Sharing Fixture
7. Organizing Test Suites
Suite-Level Setup
8. TestCase Extensions
Testing Output
Testing Performance
9. Database Testing
Datasets
Flat XML Data Set
XML Data Set
Operations
Database Testing Best Practices
10. Incomplete and Skipped Tests
Incomplete Tests
Skipping Tests
11. Mock Objects
Self-Shunting
Stubs
12. Testing Practices
During Development
During Debugging
13. Test-First Programming
BankAccount Example
14. Code Coverage Analysis
Specifying Covered Methods
Ignoring Code Blocks
Including and Excluding Files
15. Other Uses for Tests
Agile Documentation
Cross-Team Tests
16. Logging
XML Format
Code Coverage (XML)
JavaScript Object Notation (JSON)
Test Anything Protocol (TAP)
GraphViz Markup
Test Database
17. Skeleton Generator
Annotations
18. PHPUnit and Selenium
Selenium RC
PHPUnit_Extensions_SeleniumTestCase
19. Continuous Integration
CruiseControl
phpUnderControl
Apache Maven
20. PHPUnit's Implementation
21. PHPUnit API
Overview
PHPUnit_Framework_Assert
PHPUnit_Framework_Test
PHPUnit_Framework_TestCase
PHPUnit_Framework_TestSuite
PHPUnit_Framework_TestResult
Package Structure
22. Extending PHPUnit
Subclass PHPUnit_Framework_TestCase
Assert Classes
Subclass PHPUnit_Extensions_TestDecorator
Implement PHPUnit_Framework_Test
Subclass PHPUnit_Framework_TestResult
Implement PHPUnit_Framework_TestListener
New Test Runner
A. Assertions
B. The XML Configuration File
Test Suite
Groups
Including and Excluding Files for Code Coverage
Logging
PMD Rules
Setting PHP INI settings and Global Variables
C. PHPUnit for PHP 4
D. Index
E. Bibliography
F. Copyright