PHPUnit and DBUnit - getting started [closed]
Asked Answered
A

1

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.

Allowable answered 25/8, 2010 at 18:34 Comment(0)
S
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.

Stitch answered 19/3, 2011 at 9:40 Comment(4)
After a good amount of time learning DbUnit, I wish someone had told me that it basically just TRUNCATEs (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.Microscopium
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. /rantMicroscopium
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.Microscopium
@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.Scraperboard

© 2022 - 2024 — McMap. All rights reserved.