问题描述
||
我正在使用Zend_Test_PHPUnit_DatabaseTestCase设置一些数据库集成测试。
我的测试运行了,但是db表没有被截断,所以添加测试无法通过断言-因为我作为数据集提供的xml与db不匹配,有人可以建议为什么
测试用例
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);
}
}
数据库测试设置代码(改编自Dragonbe和ibuildings教程)
abstract class DatabaseTestCase extends Zend_Test_PHPUnit_DatabaseTestCase
{
const DEFAULT_CONNECTION_SCHEMA = \'main\';
protected $_connectionMock;
private $__configuration = NULL;
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);
}
}
解决方法
答案是在测试类的
setUp()
方法中运行parent::setUp();