简体   繁体   English

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

[英]How to begin and rollback a database transaction to wrap a PHPUnit suite for Magento

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. 不幸的是, Mage_Core_Model_Resource_Transaction类不公开public begin / rollbackTransaction函数。 I can't find any public Magento functions to meet the requirement. 我找不到任何公共Magento功能来满足要求。 Is there a Zend equivalent that will work? 是否有Zend等效工作?

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. 我想我可以重写Mage_Core_Model_Resource_Transaction并为受保护的方法添加公共包装器,但是我对于覆盖这样的核心类犹豫不决。

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. 然后在测试中使用$this->model ,但不能在静态函数中使用。

Any suggestions or alternative architectures? 任何建议或替代架构? Thanks in advance. 提前致谢。

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. 坦率地说,我将为Magento创建一些测试套件,以便在您的模块中编写测试用例,而无需进行初始化等操作。 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. 我想出了为单元测试创​​建一个单独的数据库(通过测试用例从当前复制),因为每个测试用例将通过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. 数据库连接凭证将在Mage::app()->run() ,因此可以通过单元测试保护开发副本免受可能的更改。

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. 在这种情况下,mysql服务器可能也会消失,特别是如果你有一个大型数据库。

UPDATE: 更新:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM