11

Does anyone have a link to a good, working tutorial or book on how to get started with adding the DBUnit layer to my PHPUNit tests?

I've tried following the code in

protected function getDatabaseTester()
{
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
    $connection = new PHPUnit_Extensions_Database_DB_DefaultDatabaseConnection($pdo);
    $tester = new PHPUnit_Extensions_Database_DefaultTester($connection);
    $tester->setSetUpOperation(PHPUnit_Extensions_Database_Operation_Factory::CLEAN_INSERT());
    $tester->setTearDownOperation(PHPUnit_Extensions_Database_Operation_Factory::NONE());
    /*
    * the next line fails with the error

    PHP Fatal error:  __autoload(): Failed opening required 'PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet.php' (include_path= *** 

    */
    $tester->setDataSet(new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__).'/../../../files/xml_database_export.xml'));
    return $tester;
}

The XML is created via mysqldump command. I'd happily use CSV, or even an array in memory (whatever works)

Unfortunately I just can't seem to get this system started.

edorian
  • 38,542
  • 15
  • 125
  • 143
Alex C
  • 16,624
  • 18
  • 66
  • 98

1 Answers1

9

There is a chapter to Database testing in the PHPUnit manual:

And B. Eberlei's Ultimate Guide to DB Testing with PHPUnit

There is also a Blogpost by PHPUnit's author Sebastian Bergmann on the topic (2008 though):

Some even older blog posts by Mike Lively, the author the DbUnit extension can be found at

A more recent tutorial (2010) would be in Matthew Turland's Blog:

You can also visit #phpunit on Freenode IRC to get official support.

Marwelln
  • 28,492
  • 21
  • 93
  • 117
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 2
    After a good amount of time learning DbUnit, I wish someone had told me that it basically just `TRUNCATE`s (erases) a table and populates it with custom data you provide from an XML. Apparently, that's it. No magical persistent connection, no Avatar-like world of growth and dreams and frolicking through temporary datasets. At the end of the day I can pretty much do the same thing with `CREATE TEMPORARY TABLE` which is even better because it maintains relations. So I'd like to say something like 'don't waste your time with this crappy extension' but due to my limited experience, I won't. – Ben Sep 18 '13 at 07:24
  • 1
    While I'm here I'll report a couple of things that took me a while to figure out: You need to call `parent::setUp()` if you've got a `setUp()` method, otherwise the database won't populate. And in that method, DbUnit will "helpfully" raise your `PDO::ATTR_ERRMODE` to "exception" level when it's `setUp()`, so even if you had `ERRMODE_SILENT` you'll get exceptions where you're not expecting them. Also, I can't find an API, so you've got to examine the source code, or read the book, which is like documentation, but longer, with important gems hidden in pages of text. /rant – Ben Sep 18 '13 at 07:28
  • Sorry to mislead, above I said `CREATE TEMPORARY TABLE` maintains relations - actually, it won't maintain foreign keys. Not to worry though, just roll a `CREATE TABLE` variant to accomplish the same thing. – Ben Sep 24 '13 at 03:15
  • @Steve That sounds kind of dissapointing, I had higher hopes for DBUnit (seemed a great idea). Maybe it's a bad idea for some reason, but it would be 100 times more useful if each such test would execute some queries within a transaction that is rollbacked in the teardown stage. – Radu Murzea Dec 29 '14 at 17:51