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.
You can also reset data via fixture file. It actually generates a virtual fixture based on annotations for this functionality.
Thanks for your feedback, highly appreciate it! Right, I saw this at some point, will see if I can update the post with an example.
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.