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 Ma开发者_如何学Pythonge_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.
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
精彩评论