如何开始和回滚数据库事务以包装 Magento 的 PHPUnit 套件

发布于 2024-10-07 16:05:16 字数 858 浏览 3 评论 0原文

我想使用事务回滚方法来隔离我的数据库以进行单元测试。理想情况下,我会使用类似这样的结构:

public static function setUpBeforeClass(){
    Mage_Core_Model_Resource_Transaction::beginTransaction();
}

public function testOne(){...}
public function testTwo(){...}

public static function tearDownAfterClass(){
    Mage_Core_Model_Resource_Transaction::rollBack();
}

不幸的是,Mage_Core_Model_Resource_Transaction 类不公开公共 begin/rollbackTransaction 函数。我找不到任何满足要求的公共 Magento 功能。 Zend 是否有类似的功能?

我想我可以重写 Mage_Core_Model_Resource_Transaction 并为受保护的方法添加公共包装器,但我对重写这样的核心类犹豫不决。

我也尝试过

$this->model = Mage::getModel('model_being_tested');
$this->model->getResource()->beginTransaction(); 
...
$this->model->getResource()->rollBack();

在测试中使用然后使用 $this->model 但不能在静态函数中使用。

有什么建议或替代架构吗?提前致谢。

I'd like to use a transaction rollback method to isolate my database for unit testing purposes. Ideally, I would use a structure something like this:

public static function setUpBeforeClass(){
    Mage_Core_Model_Resource_Transaction::beginTransaction();
}

public function testOne(){...}
public function testTwo(){...}

public static function tearDownAfterClass(){
    Mage_Core_Model_Resource_Transaction::rollBack();
}

Unfortunately the Mage_Core_Model_Resource_Transaction class doesn't expose public begin/rollbackTransaction functions. I can't find any public Magento functions to meet the requirement. Is there a Zend equivalent that will work?

I guess I could rewrite Mage_Core_Model_Resource_Transaction and add public wrappers for the protected methods, but I'm hesitant to override such a core class.

I have also tried using

$this->model = Mage::getModel('model_being_tested');
$this->model->getResource()->beginTransaction(); 
...
$this->model->getResource()->rollBack();

and then use the $this->model in the tests but that can't be used in static functions.

Any suggestions or alternative architectures? Thanks in advance.

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

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

发布评论

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

评论(1

那请放手 2024-10-14 16:05:16

坦率地说,我将为 Magento 创建某种测试套件,以便可以在模块中编写测试用例,而无需处理初始化等问题。当然,由于项目的原因,我没有足够的时间,但我想分享我将在考虑数据库固定装置时使用的想法。我想出了为单元测试创​​建一个单独的数据库(通过测试用例从当前复制),因为每个测试用例将通过固定装置为其加载初始数据。数据库连接凭据将在 Mage::app()->run() 之前设置,因此可以保护您的开发副本免受单元测试可能发生的更改。

您不能依赖事务,尤其是在保存产品的情况下...设置了一个提交回调来启动重新索引过程,如果您尚未将数据提交到产品表,则可能会导致不可预测的结果。在这种情况下,mysql 服务器也可能会消失,特别是如果您有一个大型数据库。

更新:

它的扩展名:
http://www.ecomdev.org /2011/02/01/phpunit-and-magento-yes-you-can.html

Frankly speaking, I am going to create some kind of test suite for Magento to make possible writing test cases in your module without taking care of the initialization and so on. Of course I don't have enough time because of the projects, but I want to share the idea wich I am going to use considering database fixtures. I came up with creating a separate database for unit tests (copied from current by test case), because each test case will load initial data for it via fixture. The database connection credentials will be set up prior to Mage::app()->run(), so it will be possible to protect your development copy from possible changes by unit tests.

You cannot rely on transactions, especially in case of saving product... There is set a commit callback for starting re-indexing process and it may cause unpredictable results if you haven't committed data to product tables. As well mysql server may gone away in this case, especially if you have a large database.

UPDATE:

The extension for it:
http://www.ecomdev.org/2011/02/01/phpunit-and-magento-yes-you-can.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文