EcomDev_PHPUnit Tip #1

For years, the test framework EcomDev_PHPUnit is quasi-standard for Magento unit tests. The current version is 0.3.7 and last state of official documentation is version 0.2.0 – since then, much has changed which you have to search yourself in code and GitHub issues. This series shall collect practical usage tips.

Tip #1: Reset Global State

Something that makes testing with Magento quite difficult, is the liberal use of global state, in form of singletons and registry. They are not reset between tests, but EcomDev_PHPUnit allows explicit resetting with annotations.

 * @singleton checkout/session
 * @helper tax
 * @registry current_product
public function testSomething()

The parameters are the same as for Mage::getSingleton(), Mage::helper() and Mage::registry().

It is recommended to reset all singletons and registry values that are used during the test, not only after you get conflicts. Especially resetting used session singletons is important, regardless if they are mocked in the current test. Only for stateless helpers, i.e. those that don’t have own attributes, resetting is not necessary.

4 Replies to “EcomDev_PHPUnit Tip #1”

  1. You can also reset data via fixture file. It actually generates a virtual fixture based on annotations for this functionality.

  2. Ich habe das Problem inzwischen so gelöst, dass ich vor jedem Test mittels Reflection den aktuellen Zustand der Registry speichere und ihn danach wiederherstelle. Zu oft musste ich mich mit vielen vielen Singletons herumschlagen, die Magento tief verborgen einsetzt.

Comments are closed.