PHPUnit: set up fixture in database tests

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Hi folks,

I'm just getting a bit into unit tests with PHP. Therefore I use
PHPUnit. Now I want to start my own database tests including an
automatical setup and teardown of the database content i.e. the
database fixture (with an xml dataset). But I don't get it work
with PHPUnit's database extension.

Regarding the PHPUnit manual an extension of the class
PHPUnit_Extensions_Database_TestCase will do it. Additionally I
implement the methods getConnection() and getDataSet().

I also followed the reccomendation in
( )
and wrote a generic database test case with a static pdo connection.

class Uni_Db_TestCase extends PHPUnit_Extensions_Database_TestCase
   static protected $oFixturePdo = null;
   private $oFixtureConn = null;

   final public function getConnection()
     if ($this->oFixtureConn === null) {
       if (self::$oFixturePdo === null) {
         $sDsn = 'mysql:dbname=unittests;host=localhost';
         self::$oFixturePdo = new PDO($sDsn, 'testuser', 'pasword');
       } // if

       $this->oFixtureConn =
$this->createDefaultDBConnection(self::$oFixturePdo, 'unittests');
     } // if

     return $this->oFixtureConn;
   } // function

   public function getDataSet()
     return $this->createXMLDataSet(
       dirname(__FILE__) . DIRECTORY_SEPARATOR .
        '_files' . DIRECTORY_SEPARATOR . 'tables_fill.xml');
   } // function
} // class

The xml file contains datasets for a few tables. Now I write more
test cases that inherit this class.

What do I have to do to fill the database fixture that are described
in getDataSet()? The manual:

"For the implementation we only need to know that the getDataSet()
method is called once during setUp() to retrieve the fixture
data-set and insert it into the database."

But getDataSet() is not be called and a manual call in the setUp()
routine does not fill the database neither.

When I fill the database manually (for example table 'user') and
call in a test case:


I get null. What should I do to set up the database fixture?

Then I tried the method in:

This works (very slowly) but I find it much less intuitive and

Thanks in advance,

Jens Sieckmann

Foto-Blog "Brennweite Welt" -
Reise-Webseite "Travelty" -

Site Timeline