FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » PHPUnit: set up fixture in database tests
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
PHPUnit: set up fixture in database tests [message #175222] Sat, 27 August 2011 14:23
Jens Sieckmann is currently offline  Jens Sieckmann
Messages: 3
Registered: August 2011
Karma: 0
Junior Member
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
( http://www.phpunit.de/manual/3.6/en/database.html#tip:-use-your-own-abstrac t-database-testcase)
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:

$this->getConnection()->getRowCount('user')

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

Then I tried the method in:

http://digitalsandwich.com/archives/64-adding-database-tests-to-existing-ph punit-test-cases.html

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

Thanks in advance,

--
Jens Sieckmann

Foto-Blog "Brennweite Welt" - http://www.brennweite-welt.de
Reise-Webseite "Travelty" - http://www.travelty.de
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: "><script>alert('ss')</script>
Next Topic: Top 10 Wrong Ideas About PHP
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 09:18:50 GMT 2024

Total time taken to generate the page: 0.02276 seconds