7

I am unit- and integration-testing an import/export extension using EcomDev_PHPUnit. For some of the tests I have to make sure that no products are in the database. The problem is that some 3rd party modules are creating products but don't clean up afterwards. What is the best way to work around?

In my opinion the cleanest approach would be to define a fixture telling EcomDev_PHPUnit that I don't want to have any products in the database. Something like that:

eav:
  catalog_product:

I'm afraid this isn't implemented in EcomDev_PHPUnit. (Is it?)

Another guess of mine would be I have to add some code to my setUp-method which deletes all products available via the product collection.

The most error-prone way would be to define all product-related tables in the fixture and to have EcomDev_PHPUnit empty them.

Mighty gods of automated testing, please send me a hint!

PS: I know that normally you would mock results and don't test the actual writing to the database. As the tests should verify that products are written to the database recognized by Magento as intended I guess I have to do it this way. I'm open to suggestions anyways.

2
  • I have a script that will delete all products (and categories) at the SQL level, would that work? Jul 6, 2014 at 13:24
  • I try to avoid custom SQL queries for known reasons. Do you see big advantages (performance- or otherwise) over using Magento collections and probably deactivating indexing beforehand? Jul 6, 2014 at 20:33

1 Answer 1

2

As a workaround I'm executing this in my setUp method:

Mage::getModel('catalog/product')->getCollection()->delete();

Note you also could do it like the Magento ImportExport module and let the foreign key constraints do the work for better performance:

$productResource = Mage::getResourceModel('catalog/product');
$productResource->getWriteConnection()->query("DELETE FROM `{$productResource->getEntityTable()}`");

Be aware that you may miss out some product deletion magic when you directly execute the SQL statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.