photo credit: NASA's Marshall Space Flight Center via photopin cc

Unit testing in Magento with PHPUnit

So maybe you’re familiar with unit testing in PHP, maybe not. But when it comes to Magento, there are some thinks to consider when you want to write unit testable code. Now I know, you could use some already existing framework for unit testing in Magento, like EcomDev PHPUnit for example, but if you’re like me and you’re not very fond on including a whole library right away but rather look for something lightweight: it’s actually not that hard to write unit testable code for Magento!

Keep it clean and simple

Our goal here is to make the most basic test suite ever. So let’s start by creating a simple phpunit.xml -file in the directory from which you want to run your tests:

This is as basic as it gets. No autoloader, nothing… We’re going plain vanilla on this one. The only thing worth mentioning here is the defining of the constant MAGENTO_ROOT . This is the only constant needed for our tests.

Create a simple test

Now let’s create a very simple test to start with. Create a file in your test-directory (where your phpunit.xml  is located) called MageTest.php :

Now there are 2 things to note here:

  1. We instantiate Magento so we can make use of Magentos’ functions.
  2. We are using Magentos’ internal functions here in our test.

This is awesome! This means we can also make use of Mage::helper() , Mage::getModel() , Mage::getLayout() , etc. to unit-test our code there! So let’s try it out!

Write unit testable code for Magento

So let’s create a simple module with a simple helper. I’m not going into details here how to create a module since that’s not in te scope of this article. But let’s say we have a helper with a function that
When it comes to writing unit testable code, the functions that are the most suitable for testing are the ones that require no external dependencies and are setup to have a basic input/output. Take the following code for example:

This code is a nightmare – almost impossible – to unit test. Unit testing this could would mean you would have to load a product (which requires the usage of a test-database), execute the changeProductAttribute() , and assert the values of the product afterward.
A more friendly approach for your tests would be something like this:

The unit test for this helper would look something like this:

This example not only shows that Magento is also suited for test-driven development, but it also shows that to truly write test-driven code you need to think about how you create your methods and where and how you calculate your logic.

In conclusion

This article showed you how to write the most basic setup for a unit test in Magento. It covers how to setup PHPUnit for this purpose and what to keep in mind when writing your modules.
Of course it needs without saying that you don’t write unit tests for the existing Magento codebase. I personally only try to write my own modules in a test-driven manner (for as far as possible), but unless your working with test-databases with testdata don’t feel ashamed if you can’t cover your code for 100%.

Visitors give this article an average rating of 3.4 out of 5.

How would you rate this article?

One thought on “Unit testing in Magento with PHPUnit”

  1. Nicely explained. Is it possible to follow this same process for testing custom built PHP websites? Or do I have to follow some other process (like this one: https://www.cloudways.com/blog/getting-started-with-unit-testing-php/ ) to test my codes?

Leave a Reply