Search code examples
phpunit-testingphpunitdbunit

dbunit in phpunit is not truncating the tables


I am currently using PHPUnit and DBUnit for my project. I have a problem in DBUnit because DBUnit PHPUnit_Extensions_Database_TestCase­Src class does not seem to be truncating the existing data on the test db. So this makes my insertion tests fail after only working for one time.

I am using mysql and here is my code :

abstract class Generic_Tests_DatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
{
    // only instantiate pdo once for test clean-up/fixture load
    static private $pdo = null;

    // only instantiate PHPUnit_Extensions_Database_DB_IDatabaseConnection once per test
    private $conn = null;

    final public function getConnection()
    {
        if ($this->conn === null) {
            if (self::$pdo == null) {
                self::$pdo = new PDO( "mysql:dbname=db;host=localhost", "root", "pass" );
            }
            $this->conn = $this->createDefaultDBConnection(self::$pdo, "db");
        }

        return $this->conn;
    }
}

class DbopTest extends Generic_Tests_DatabaseTestCase
{       
    private $db;

    protected function setup(){
        $this->db = null;
    }

    public function getDataSet(){
        return $this->createMySQLXMLDataSet(dirname(__FILE__) . '/../rows.xml');
    }       
    ...
}

So how can I fix this problem? What is it that I do wrong here?


Solution

  • If you override the setUp method, PHPUnit won't automatically call your getDataSet method. You need to take care that you call the parent::setUp method as well, otherwise PHPUnit does not know what to do ;).