Although there is a good tutorial about PHPunit and Magento integration and I’d rather using Lime ( the symfony-integrated test engine ) here I’ll show you how to set up PHPUnit tests in order to improve the QA of your Magento customizations.
I’m working on Ubuntu Jaunty, so a few steps might be different based on your OS: nevermind, with a bit of patience you’ll be able to set up your environment correctly.
First of all let’s install PHPUnit from the repo:
1
| |
You can decide to download PHPUnit from its official website and run a single installation under a magento directory: I prefer having PHPUnit running in my whole environment because I want to test different applications, so I don’t have to re-install PHPunit for all of them.
So, now let’s set up our environment:
1 2 3 4 5 | |
and here’s how you need to populate the confg file phpunit.xml:
1 2 3 4 5 | |
so now we can create directories and testfiles under /var/www/magento/test/unit and run them with a single command:
1
| |
But we have no tests now! Let’s create, at least, one.
Let’s assume we want to test an helper file of a custom module we created, for example a “faq” module.
1 2 3 4 | |
So we have to fill that file with the standard PHPUnit structure:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Now we need to populate the setup function in order to have he ability to use all Magento functions:
1 2 3 4 5 6 7 8 9 | |
After this we can test the methods of our helper: for example the getUrl() method we’ve created in order to retrieve the URL of a FAQ from its id:
1 2 3 4 5 6 | |
So now we have a test covering our fictional getUrl() method of the class Projectname_Faq_Helper_Data we’ve created in Magento.
To run the test use the code I wrote some lines above:
1
| |
If you create other directories and tesfiles under test/unit directory of Magento, using this command all of those will be run by PHPUnit.
That’s it!