2017 © Pedro Peláez

magento-module testframework

A simple test framework module to develop integration and unit tests on Magento 1



A simple test framework module to develop integration and unit tests on Magento 1

  • Tuesday, March 13, 2018
  • by digitalpianism
  • Repository
  • 8 Watchers
  • 43 Stars
  • 986 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 9 Forks
  • 4 Open issues
  • 1 Versions
  • 40 % Grown


Digital Pianism Test Framework

A simple test framework module that can be used to create unit and integration tests on Magento 1., (*1)

Prepare your module for your tests

  • Create a Test folder under your module folder
  • Under this Test folder create the following files:

phpunit.xml with the following content:, (*2)

<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        <testsuite name="Magento Integration Tests">


Note that you can change most of the file here, the important part being the bootstrap.php file declaration., (*3)

bootstrap.php with the following content:, (*4)


require __DIR__ . '/../../../../../../lib/DigitalPianism/TestFramework/Helper/Magento.php';

Please note that you may have to adapt the link to lib/DigitalPianism/TestFramework/Helper/Magento.php depending on your Magento structure., (*5)

Controller test sample

Here is a sample of a controller test:, (*6)

Under your Test folder create a MyTest.php (your test files must be nammed accordingly to the declaration in your phpunit.xml.dist file, in the example above we declared a Test.php suffix):, (*7)


class Vendor_Module_Test_MyTest extends \PHPUnit_Framework_TestCase {

    public function setUp()
        // Stub response to avoid headers already sent problems
        $stubResponse = new \DigitalPianism_TestFramework_Controller_HttpResponse();

        // Possible parameter
        // Mage::app()->getRequest()->setParam('myparameter', 'myvalue');

        // Use the controller helper
        $controllerTestHelper = new \DigitalPianism_TestFramework_Helper_ControllerTestHelper($this);

        // Dispatch a GET request
        $controllerTestHelper->dispatchGetRequest('route', 'controller', 'action');
        // Dispatch a POST request
        //$controllerTestHelper->dispatchPostRequest('route', 'controller', 'action');

    public function testSomething()
        // Get the body
        $body = Mage::app()->getResponse()->getBody(true);

        // Get the headers
        $headers = Mage::app()->getResponse()->getHeaders();

        // Get a block
        $block = Mage::app()->getLayout()->getBlock('block_name');

        // Do your tests here

Using test doubles

As you may have noticed, the bootstrap.php injects a custom instance of the config class DigitalPianism_TestFramework_Model_Config, (*8)

This class declares several new methods:, (*9)

  • setModelTestDouble for model test doubles
  • setResourceModelTestDouble for resource model test doubles
  • setHelperTestDouble for helper test doubles

To use test doubles you can do the following in your setUp method., (*10)

Let's say you want to check what template ID is assigned to Mage_Core_Model_Email_Template_Mailer when the new customer account email is sent:, (*11)

$mailer = Mage::getModel('core/email_template_mailer');
Mage::getConfig()->setModelTestDouble('core/email_template_mailer', $mailer);


$this->assertSame($expectedEmailTemplateId, $mailer->getTemplateId());


You can create fixtures programmatically in your code., (*12)

A good recommendation to avoid having to manually delete your fixtures is to call Mage::getSingleton('core/resource')->getConnection('core_write')->beginTransaction(); in the setUp method and then call Mage::getSingleton('core/resource')->getConnection('core_write')->rollBack(); in the tearDown method, (*13)

The Versions

13/03 2018



A simple test framework module to develop integration and unit tests on Magento 1

  Sources   Download


The Requires