PHPUnit 数据库测试

发布于 2024-10-02 09:12:47 字数 1108 浏览 6 评论 0原文

我正在使用 PHPUnit 通过我的存储对象测试对象的插入。每个域对象都有一个添加的和最后修改的时间戳,由存储对象自动处理。我可以使用 PHPUnits DB 扩展方法assertDataSetsEqual 并作为 XML 数据集传递,如下所示。问题已添加,lastmodified 无法硬编码到 XML 数据集中,因为这会一直自动更改,我可以告诉 PHPUnit 忽略这些列吗?或者比较表输出的另一种方式(不是 XML),我可以忽略这些列?

测试

$user = new Social_User();
$user->setFk_mswuserId(10);
$user->setFirstName('Gavin');

$store = new Storage();
$store->save($user);

$xml_dataset = $this->createFlatXMLDataSet('after-new.xml');
$this->assertDataSetsEqual($xml_dataset, $this->getConnection()->createDataSet());

XML 数据集

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
            <user id="1" password="NULL" ip="0" added="0" authenticated="0" lat="0" lon="0" avatar="0" fk_mswuserId="1" timezoneoffset="0" firstName="Ben" lastName="Freeston" deleted="0" lastModified="0" />
            <user id="2" password="NULL" ip="0" added="0" authenticated="0" lat="0" lon="0" avatar="0" fk_mswuserId="10" timezoneoffset="0" firstName="Gavin" lastName="Cooper" deleted="0" lastModified="0"/>
</dataset>

I am using PHPUnit to test insertion of objects via my storage object. Each domain object has a added and lastmodified timestamp, that is handled by the storage object automatically. I can using PHPUnits DB extensions method assertDataSetsEqual and passing as XML data set as below shows. The problem is added and lastmodified cannot be hardcoded into the XML dataset as this will change all the time automatically, can I tell PHPUnit to ignore these cols? or compare the tables output another way (not XML) where I can ignore these columns?

Test

$user = new Social_User();
$user->setFk_mswuserId(10);
$user->setFirstName('Gavin');

$store = new Storage();
$store->save($user);

$xml_dataset = $this->createFlatXMLDataSet('after-new.xml');
$this->assertDataSetsEqual($xml_dataset, $this->getConnection()->createDataSet());

XML Dataset

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
            <user id="1" password="NULL" ip="0" added="0" authenticated="0" lat="0" lon="0" avatar="0" fk_mswuserId="1" timezoneoffset="0" firstName="Ben" lastName="Freeston" deleted="0" lastModified="0" />
            <user id="2" password="NULL" ip="0" added="0" authenticated="0" lat="0" lon="0" avatar="0" fk_mswuserId="10" timezoneoffset="0" firstName="Gavin" lastName="Cooper" deleted="0" lastModified="0"/>
</dataset>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-10-09 09:12:47

根据

这已经是内置的。

另请参阅 M.Lively(DBUnit 主要作者)的这些幻灯片

和 B. Eberlei 的 PHPUnit 数据库测试终极指南

所以它应该按照以下方式工作

$database_dataset = new PHPUnit_Extensions_Database_DataSet_DataSetFilter ( 
    $this->getConnection()->createDataSet(array('bank_account')), 
    array('bank_account' => array ('date_created')) // excluded
); 

According to

this is already built-in.

Also see these slides by M.Lively (the main DBUnit author)

and B. Eberlei's Ultimate Guide to DB Testing with PHPUnit

So it should work along the lines of

$database_dataset = new PHPUnit_Extensions_Database_DataSet_DataSetFilter ( 
    $this->getConnection()->createDataSet(array('bank_account')), 
    array('bank_account' => array ('date_created')) // excluded
); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文