PHPUnit Integration with Magento

Last modified by bcelenza on Fri, July 2, 2010 08:55
Source|Old Revisions  

This is an old revision of the document!

TDD (Test Driven Development) is a widely accepted practice for programming today. The PHP community, however, has been slow to adopt this method of programming.

This article briefly explains how you can integrate PHPUnit‘s popular testing suite into your Magento source code. It does not provide assistance in writing test cases.



Although the directory structure you wish to use for your testing is entirely up to you, in this document we will assume you have added a directory named ‘tests’ under the Magento root.

Create a new directory under your document root (or wherever Magento has been installed) named ‘tests’. All of your unit tests will go within this directory.

Writing Your First Test Case

Setting up a test case for Magento is surprisingly simple, and involves just a small addition to the setUp() method of your test case. See the example below

  1. require_once 'PHPUnit/Framework.php';
  2. require_once '../app/Mage.php';
  4. class MyModelTest extends PHPUnit_Framework_TestCase {
  6.   public function setUp() {
  7.     Mage::app('default');
  8.     $this->myModel = Mage::getModel("mymodule/mymodel");
  9.   }
  11.   public function testGetWidgets() {
  12.     $widgets = $this->myModel->getWidgets();
  13.     $this->assertEquals(25, sizeOf($widgets));
  14.   }
  16. }

As you can see above, we’ve included two external files:

  1. PHPUnit/Framework.php: No explanations should be necessary, as this is required under the standard method of writing PHPUnit test cases.
  2. ../app/Mage.php: Again, we’ve made the assumption your test case is located in a sub-directory under the Magento document root. This will allow us access to the Mage final class and provide us the means to initiate the Magento session for this test.

In the setUp() method for any test, it is required to add the following line:

  1. Mage::app('default');

That’s it! You should now be able to load models and test configuration just like you would in any Magento controller. Whether or not you chose to load your model in the setUp() method remains up to you. You should have access to the Mage model from within any test method you write.

Drawbacks, known Issues

  • Currently there is no known method for testing a controller.
  • Although preliminary tests have proved very successful, PHPUnit integration with Magento should still be considered EXPERIMENTAL at this point. Tests currently rely on the idea that the setUp() code used to init Magento is all that is necessary.

Common Problems

This section to be written.

Using PHPUnit+Magento with Phing

If you are using Phing to create builds and do unit testing, you may be required to modify the include for Mage.php. For instance, if your build.xml is in the root level of the Magento directory, remove the leading ‘../’ from the require_once function in each of your tasks files.


Brian Celenza, Sr. PHP Developer,, bcelenza at gmail dot com