Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit and DBUnit - getting started [closed]

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.

like image 536
Alex C Avatar asked Aug 25 '10 18:08

Alex C


1 Answers

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

  • http://phpunit.de/manual/current/en/database.html

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

  • http://www.phpunit.de/manual/dbunit.txt (gone)

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

  • Testing PHP/MySQL Applications with PHPUnit/DbUnit (removed)

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

  • http://digitalsandwich.com/?s=dbunit

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

  • Database Testing with PHPUnit and MySQL

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

like image 124
Gordon Avatar answered Nov 15 '22 15:11

Gordon