Codegento Who Let Mage Out Of The Cage?


Writing a PHPUnit Test Against Magento

We write PHPUnit tests for all the Models, Blocks and Helpers we write. The primary benefit of doing this is to prove a method works as expected.  This allows us to refactor code and prove the method still works as expected (i.e. regression test).  Another benefit is being able to run a full automated regression test after upgrading Magento.  In this post I will only be covering the writing a simple PHPUnit test for a model class. Note: We have not written unit tests for Magento controllers. But we are looking into using the Mage_Test extension to allow us to write unit tests for controllers.

In order to run a PHPUnit test against a Magento class we will need to create an extension of the PHPUnit_Framework_TestCase class.  This class will override the setup method to call Mage::app().

abstract class Magento_PHPUnit_Framework_TestCase extends PHPUnit_Framework_TestCase {
    public function setUp() {

We will be writing a unit test for the method getName() in the class Super_Awesome_Model_Customer.

class Super_Awesome_Model_Customer extends Mage_Customer_Model_Customer {

    public function getName() {
        $name = parent::getName();
        $name = $name . ' loves Magento';
        return $name;


We will create a unit test extending the Magento_PHPUnit_Framework_TestCase.. This unit test will assert the getName() method appends the string ' loves Magento' to the customer's name.

class Super_Awesome_Model_CustomerTest extends Magento_PHPUnit_Framework_TestCase
    public function setUp() {
        $this->myModel = new Super_Awesome_Model_Customer();

    public function testGetName() {
        $result = $this->myModel->getName();
        $this->assertStringEndsWith(' loves Magento', $result);


You can now run your unit test using PHPUnit. This post will not cover how to install and use PHPUnit. Click here for more information about using PHPUnit.. If you are using Eclipse PDT for your development environment. I highly recommend using the Eclipse plugin MakeGood. The MakeGood plugin allows you to easily debug unit tests and easily navigate to the failing test.

Once you start writing unit tests against Magento models you will need to write a test using the database. This will require you to either put the database in a predictable state between test runs or mock the database interaction. The techniques I have used in PHPUnit are the DbUnit extension and mock objects. Ideally, I would choose to mock the entire database layer. This would be the easiest to maintain and the tests would run faster compared to inserting data into the database. However this would require a significate coding effort. Instead we have taken advantage of DbUnit to setup the database. This has worked very well for us. In the future I may expand on how to use DbUnit for unit testing Magento classes.

Posted by Jason Evans

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment


Trackbacks are disabled.