MySQL Conf 2008 – PHP/DBUnit (Day 2)

Author Michael Tougeron on April 15, 2008

Posted under Events, mysql, PHP and tagged with , , , ,

I started off the day today listening to the keynotes at the MySQL Conference. Unfortunately I had to work on something so I wasn’t able to really pay attention. ๐Ÿ™ I hope I didn’t miss anything important.

After the keynotes, I decided to start off with the Testing PHP/MySQL Applications with PHPUnit/DbUnit session by Sebastian Bermann. I know I should already be using PHPUnit, but honestly I’ve never gotten around to it. I’m hoping that this will give me the kick in the ass that I need to start. ๐Ÿ˜›

The first thing that Sebastian wanted us to note is that the PHPUnit listed on is not the real PHPUnit. I kind of thought that weird and confusing. You’d think by now that he could get that old project de-listed.

Installing PHPUnit is fairly simple. It is not hosted off the standard PEAR channel, but by a quick extra command you can get access to it.
pear channel-discover
pear install phpunit/PHPUnit

By setting up a reusable testing environment with PHPUnit you’ll be able to do automatic execution of your test code. This would be good for things like over night tests of the latest code commits. If a test fails, the developer team can be immediately notified. Be sure to enforce strict separation between production and testing environments. The last thing you want to happen is to have your test cases somehow break production.

Setting up a test case can be as simple or complex as you need it to be. You setup a class for each element/object you need to test. Each test case is a member function of this class where you assert value comparisons. While the documentation examples are best, here is a very simple example:

class BankAccountTest extends PHPUnit_Framework_TestCase {

    public function testBalanceIsInitiallyZero() {
        $ba = new BankAccount;
        $this->assertEquals(0, $ba->getBalance());

Where things can now start to get interesting is when you use the database extension to PHPUnit. The DBUnit extension was ported from jUnit by Michael Lively Jr.

DBUnit uses PDO to talk to a default SQLite database. However, a database it not required as it can emulate a database in memory or via XML. It also works with MySQL, but if the tests are written in a way that is compatible with SQLite, then the tests can be faster by about 20%.

Once of the nice things about how DBUnit works is how it puts the db into a known state between each test run. This means it avoids problems with one test case corrupting the db for other tests. By using its ability to import/export data to/from XML datasets, your db tests will run reliably and accurately.

Like with PHPUnit, DBUnit has assert methods like “assertTableEquals(expected, actual)” and can use PHP arrays for comparison. This can be good for comparing records in the database. e.g., The db gets updated via a PHPunit test and you then do a DBUnit test to verify the action.

A quick example of a DBUnit test case:

class BankAccountTest extends PHPUnit_Extensions_Database_TestCase {
    protected $pdo;

    public function __construct() {
        $this->pdo = PHPUnit_Util_PDO::factory($dsn);

    protected function getConnection() {
        return $tihs->createDefaultDBConnection($this->pdo, 'mysql');

    protect function getDataSet() {
        return $this->createFlatXMLDataSet('/path/to/seed.xml');

Posted under Events, mysql, PHP

This post was written by Michael Tougeron on April 15, 2008

Tags: , , , ,

Leave a Comment

You must be logged in to post a comment.

More Blog Posts