Unit Testing & Magento’s Autoloader

, ,

If code written for Magento is executed outside of Magento—as occurs when running unit tests—Magento’s normal startup process (which registers its autoload functionality) is not run. Without the autoloader in place, the first time PHP encounters a request to initialize a class which it doesn’t recognize, it will error out with a “class not found” error. Also, without Magento’s startup process running, PHP doesn’t know about the Mage family of singleton methods (e.g. Mage::getModel(), Mage::helper()) which are commonly used to create class instances.

Solution

Include/require (perhaps using require_once) app/Mage.php in the code you execute from outside of Magento. When Mage.php is included, the code in that file will register Magento’s autoloader with PHP. Also, Mage.php contains the method definitions for the Mage singleton methods.

Learn more about unit testing code written for Magento on Magento’s wiki.

Leave a Reply

Your email address will not be published. Required fields are marked *