PHPUnit and DBUnit - getting started [closed]
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionDoes 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.
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.
精彩评论