PHPUnit: Multiple datasets on database testing
Asked Answered
S

2

7

Is it possible to load multiple flat xml datasets on PHPUnit to load lots of fixtures?

We are writing a rather complex application and the xml dataset is getting pretty big, so I would like to slip it into 2-3 xml.

Here is the current code for a test case:

<?php

class My_TestBase extends Zend_Test_PHPUnit_DatabaseTestCase{ 

/**
 * Zend_Application
 * @var Zend_Application 
 */
protected $_application;

/**
 * Connection
 * 
 * @var Zend_Test_PHPUnit_Db_Connection
 */
private $_connection;

/**
 * Returns the test database connection.
 *
 * @link http://framework.zend.com/wiki/display/ZFPROP/Zend_Test_PHPUnit_Database+-+Benjamin+Eberlei
 * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
 */
protected function getConnection(){

    if($this->_connection === null){

        $Resources = $this->_application->getOption("resources");

        $conn = Zend_Db::factory($Resources["db"]["adapter"], $Resources["db"]["params"]);          
        $this->_connection = $this->createZendDbConnection($conn, $Resources["db"]["params"]["dbname"]);
    }

    return $this->_connection;
}


/**
 * Returns the test dataset.
 * 
 * @link http://framework.zend.com/wiki/display/ZFPROP/Zend_Test_PHPUnit_Database+-+Benjamin+Eberlei
 * @return PHPUnit_Extensions_Database_DataSet_IDataSet
 */
protected function getDataSet(){

    return $this->createFlatXMLDataSet(__DIR__."/seed_data.xml");
}

/**
 * Setup
 */
protected function setUp(){

    $this->_application = new Zend_Application(
        APPLICATION_ENV,
        APPLICATION_PATH . '/configs/application.ini'
    );
}   

}

Slipover answered 27/5, 2012 at 13:54 Comment(2)
Wow, just ran into this same issue, but rather than dealing with complex datasets, I'd prefer to keep them atomic so they can be used in other tests. Doesn't make much sense to tightly couple datasets to a specific test. Were you able to figure it out?Figurehead
Sadly, no! We have a lot of fixtures required on every test (we have quite a few tables linked together), replicate the fixtures in many files for each test case can be a pain to mantain for us. The only way to go with splitted XML fixtures is build some wrapper class of Zend_Test_PHPUnit_DatabaseTestCase and code a "addXmlFile" method, but i had no time to do so. Next time i would definitively use separate yaml fixtures loaded when needed.Slipover
F
2

Dislaimer: The following will only work for yaml fixtures, for some reason the xml fixtures API DOES NOT afford the same functionality (checked source code), don't ask me why, seems like we should be able to add multiple tables regardless of the fixture file format type.

The API is a bit clumsy, and exactly why I don't like passing args to constructors, especially in this case, but try the following (this was tested and worked):

class MyTest extends PHPUnit_Extensions_Database_TestCase
{
    protected function getDataset()
    {
        $primary = new PHPUnit_Extensions_Database_DataSet_YamlDataSet('etc/fixture/dbname/table1.yml');

        $primary->addYamlFile('etc/fixture/dbname/table2.yml');
        $primary->addYamlFile('etc/fixture/dbname/table3.yml');

        return $primary;
    }
...
}
Figurehead answered 6/8, 2012 at 22:27 Comment(0)
C
8

You can use composite datasets.

From the manual:

The composite DataSet is very useful for aggregating several already existing datasets into a single dataset.

public function getDataSet()
{
    $ds1 = $this->createFlatXmlDataSet('fixture1.xml');
    $ds2 = $this->createFlatXmlDataSet('fixture2.xml');

    $compositeDs = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
    $compositeDs->addDataSet($ds1);
    $compositeDs->addDataSet($ds2);

    return $compositeDs;
}

(Above code example is straight from the docs but appears to be missing the constructor parameter. The docs are also incorrect about allowing a table to be defined in more than one data set when compositing.)

Contraception answered 16/4, 2013 at 1:50 Comment(2)
That is a neat solution, will try next time i need. thank youSlipover
I fixed the example code, according to github.com/sebastianbergmann/dbunit/blob/master/PHPUnit/… the constructor will automatically call addDataSet methodBulganin
F
2

Dislaimer: The following will only work for yaml fixtures, for some reason the xml fixtures API DOES NOT afford the same functionality (checked source code), don't ask me why, seems like we should be able to add multiple tables regardless of the fixture file format type.

The API is a bit clumsy, and exactly why I don't like passing args to constructors, especially in this case, but try the following (this was tested and worked):

class MyTest extends PHPUnit_Extensions_Database_TestCase
{
    protected function getDataset()
    {
        $primary = new PHPUnit_Extensions_Database_DataSet_YamlDataSet('etc/fixture/dbname/table1.yml');

        $primary->addYamlFile('etc/fixture/dbname/table2.yml');
        $primary->addYamlFile('etc/fixture/dbname/table3.yml');

        return $primary;
    }
...
}
Figurehead answered 6/8, 2012 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.