Home »
Imported messages »
comp.lang.php »
PHPUnit: set up fixture in database tests
PHPUnit: set up fixture in database tests [message #175222] |
Sat, 27 August 2011 14:23 |
Jens Sieckmann
Messages: 3 Registered: August 2011
Karma:
|
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
|
|
|
Goto Forum:
Current Time: Tue Nov 26 09:22:28 GMT 2024
Total time taken to generate the page: 0.04652 seconds