PHPUnit Zend_Test_PHPUnit_DatabaseTestCase fails to truncate table
I am setting up some db integration testing using Zend_Test_PHPUnit_DatabaseTestCase.
My tests run but the db tables do not get truncated so an add test fails assertion - as the xml I provide as a dataset does not match the db can anybody suggest why
TestCase
class ArtworkDBTest extends DatabaseTestCase
{
public function testAddArtwork()
{
$data=array("artwork_name"=>'test',"description"=>'test',"imgpath"=>'test',"size"=>'test',"price_information"=>'test',"category"=>1,"artwork_order"=>1);
$mockedLog=$this->getMock("Log",array("log"));
$artwork = new shop_Artwork($mockedLog,Zend_Db_Table_Abstract::getDefaultAdapter());
$artwork->addArtwork($data);
$this->assertDataSetsMatchXML('artwork-add.xml', $dataSet);
}
}
Database testing setup code (adapted from dragonbe and ibuildings tutorials)
abstract class DatabaseTestCase extends Zend_Test_PHPUnit_DatabaseTestCase
{
const DEFAULT_CONNECTION_SCHEMA = 'main';
protected $_connectionMock;
private $__configuration = N开发者_C百科ULL;
protected $_connectionSchema = self::DEFAULT_CONNECTION_SCHEMA;
protected $_seedFilesPath;
protected $dataSet;
public function __construct()
{
$this->dataSet = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet($this->getConnection());
$this->dataSet->addTable('artwork','Select* from artwork');
$this->dataSet->addTable('login','Select *from login');
$this->dataSet->addTable('category','Select *from category');
}
public function getConfiguration()
{
if ($this->__configuration == NULL) {
$this->__configuration = new Zend_Config_Ini(TEST_PATH . '/application/configs/tests.ini');
}
return $this->__configuration;
}
public function getSeedFilesPath()
{
if ($this->_seedFilesPath == NULL) {
$this->_seedFilesPath = $this->getConfiguration()->tests->seeds->folder;
}
return rtrim($this->_seedFilesPath, '/') . '/';
}
protected function getConnection()
{
if ($this->_connectionMock == NULL) {
$dbAdapterName = $this->getConfiguration()->tests->dbadapter;
$dbAdapterParams = $this->getConfiguration()->tests->dbparams->toArray();
$connection = Zend_Db::factory($dbAdapterName, $dbAdapterParams);
$this->_connectionMock = $this->createZendDbConnection(
$connection, $this->_connectionSchema
);
Zend_Db_Table_Abstract::setDefaultAdapter($connection);
}
return $this->_connectionMock;
}
protected function getDataSet()
{
return $this->createFlatXMLDataSet(TEST_PATH . '/fixtures/models/artwork-seed.xml');
}
/**
* Convert a Rowset to a Dataset
*
* @param Zend_Db_Table_Rowset_Abstract $rowset
* @param string $tableName
* @return PHPUnit_Extensions_Database_DataSet_DefaultDataSet
*/
public function convertRowsetToDataSet($rowset, $tableName = NULL)
{
$rowsetDataSet = new Zend_Test_PHPUnit_Db_DataSet_DbRowset($rowset, $tableName);
return new PHPUnit_Extensions_Database_DataSet_DefaultDataSet(array($rowsetDataSet));
}
/**
* Convert a Record to a Dataset
*
* @param array $data
* @param string $tableName
* @return PHPUnit_Extensions_Database_DataSet_DefaultDataSet
*/
public function convertRecordToDataSet(Array $data, $tableName)
{
$rowset = new Zend_Db_Table_Rowset(array('data' => array($data)));
return $this->convertRowsetToDataSet($rowset, $tableName);
}
/**
* Compare dataset with data stored in the file
*
* @param string $filename
* @param PHPUnit_Extensions_Database_DataSet_IDataSet $expected
* @return boolean
*/
public function assertDataSetsMatchXML($filename, PHPUnit_Extensions_Database_DataSet_IDataSet $actual)
{
if (empty($filename) || !is_string($filename))
throw new InvalidArgumentException(
'Second parameter "filename" is not a valid string.'
);
$expected = $this->createFlatXmlDataSet($this->getSeedFilesPath() . $filename);
return $this->assertDataSetsEqual($expected, $actual);
}
}
The answer was running parent::setUp();
inside the test class's setUp()
method
精彩评论