I started working on a console script to help aide in testing certain Magento functionality without having to go through a browser. This will also avoid adding debug code into places like action methods, templates or index.php. In all its alpha/first round glory I present my simple “interactive Magento console” script.
It's meant to be ran at the base directory of a Magento project. It will include app/Mage.php and run Mage::app(), similar to what's done in the cron.php. All standard methods are available but some configuration may not be setup. For instance, if you have custom events that only trigger when the “frontend” area is loaded, it must be initiated manually by calling:
I've started testing the script with a block class I'm working on and noticed the theme and package name were not set up. Knowing which areas need to be setup prior to executing code will depend on the type of object and one's familiarity with Magento's internals.
Testing Custom Layout Block (output prefixed with #):
Mage::app()->loadAreaPart('frontend', 'events'); Mage::getDesign()->setPackageName('mage_nation'); Mage::getDesign()->setTheme('mage_nation'); $l = Mage::app()->getLayout(); $b=$l->createBlock('magenation_catalog/product_view_spotlight', '', array('product_sku'=>'foo')); echo Mage::getDesign()->getTemplateFilename($b->getTemplate(), array('_area'=>'frontend')); #/mnt/hgfs/Projects/mn-sandbox/shop/app/design/frontend/mage_nation/default/template/catalog/product/view/spotlight.phtml echo $b->toHtml(); #testing!
One nice benefit when testing a block's output is that it is possible to modify the template file without having to restart the console session. Altering the Block class would require the console to be restarted so the latest version of the class can be included. Fortunately readline methods which the script uses allow for history to be saved.