gpt4 book ai didi

zend-framework - PHPUnit Zend_Test_PHPUnit_DatabaseTestCase 无法截断表

转载 作者:行者123 更新时间:2023-12-01 13:00:30 26 4
gpt4 key购买 nike

我正在使用 Zend_Test_PHPUnit_DatabaseTestCase 设置一些数据库集成测试。

我的测试运行但数据库表没有被截断,所以添加测试断言失败 - 因为我作为数据集提供的 xml 与数据库不匹配任何人都可以提出原因

测试用例

  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();

关于zend-framework - PHPUnit Zend_Test_PHPUnit_DatabaseTestCase 无法截断表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6315658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com