作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我正在使用 Zend_Test_PHPUnit_DatabaseTestCase 设置一些数据库集成测试。 我的测试运行但数据库表没有被截断,所以添加测试断言失败 - 因为我作为数据集提供的 xml
我已经实现了 Database testing with Zend Framework and PHPUnit 的抽象类它实现了 Zend_Test_PHPUnit_DatabaseTestCase
当我运行我的 PHP 单元测试时,我得到: 1) Test_Model_Mapper_TestTest::testTest Argument 1 passed to PHPUnit_Extension
我是一名优秀的程序员,十分优秀!