Nick Jones

a magento developer (@nickj89) based in Bath

Magento How-to: Testing Magento Modules

I’m sure a lot of Magento developers would agree that testing your models when creating a Magento module can be a bit difficult.

During the development of Meanbee’s Royalmail and Shipwire modules I have found the following script handy:

<?php
require_once '../../../../Mage.php';
 
Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
 
umask(0);
Mage::app();
 
$obj = Mage::getModel('shipwire/api_inventorysynch');
$obj->submitRequest();
?>

I simply make a file, Test.php, in my module folder, app/code/community/Meanbee/Shipwire, for example, and then navigate to the file with my web browser, remembering to allow it through the .htaccess (and switch it back again). I then have access to the entire Magento class hierarchy along with any site specific settings to which I can perform arbitrary commands.

(EDIT: Remember that you could enable the profiler, set the development environment and enable the error reporting by placing those lines in your /index.php and feel the effects site wide.)

5 Responses to Magento How-to: Testing Magento Modules

  1. Pingback: Improving Magento’s Error Reporting | Nick Says

  2. Anthony says:

    You have mentioned, “allow it through the .htaccess (and switch it back again)” several times in your posts. How is this done?

    • Nick says:

      @Anthony: .htaccess permissions follow a hierarchy – the rules of the child are inherited from the parent. If we take this post as an example, we have two directories:

      app/
      app/code/community/Meanbee/Shipwire/

      In app/ there will be a file named .htaccess which says something along the lines of “DENY FROM ALL”. This will not allow the directory nor any files under it be displayed. If we have our Test.php in app/code/community/Meanbee/Shipwire/ then we would be unable to see it because of the inherited “DENY FROM ALL”.

      We can overcome this by placing another .htaccess file, this time in app/code/community/Meanbee/Shipwire/, that reads “ALLOW FROM ALL”. This modifies the viewing permissions on the folders to disable viewing of files and directories under app/ except for app/code/community/Meanbee/Shipwire/. We’ll then be able to see our Test.php! :)

  3. Anthony says:

    @Nick
    Many thanks for the .htaccess tip

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">