PHPUnit: multiple datasets when testing a database

Is it possible to upload multiple flat xml datasets to PHPUnit to load multiple fixtures?

We are writing a rather complicated application, and the xml dataset is getting quite large, so I would like to skip it at 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'
    );
}   

}

+5
source share
2 answers

Disclaimer: The following steps will work only for barley lamps, for some reason the xml API MUST NOT provide the same functionality (proven source code), do not ask me why, it seems, we should be able to add multiple tables regardless of the file type of the file format.

API , , , ( ):

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;
    }
...
}
+2

.

:

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;
}

( , , , . .)

+7

All Articles