How to do test driven development for Joomla Component: Part 2

In the last tutorial we have created a unit test for the back end or administrator part of Joomla component and in this tutorial we will create a unit test for the front end or public part of Joomla component. In this tutorial we will use Joomla 3.2.x and Joomla article component (com_content) for experiment. So let’s start

1. Create a Test folder

Create a new folder called Tests on /joomlapath/components/com_content folder. This folder will contain all test files for com_content component.

2. Create phpunit configuration file

Create a new file called phpunit.xml in Tests folder. We will use the following configurations for this experiment: 

3. Create a bootstrap file

Then create a bootstrap file called bootstrap.php  in Tests folder. This bootstrap file is based on index.php file on the root of Joomla site but with some updates, you can see the explanation of this code on the previous tutorial. Here is the code:

4. Test phpunit configuration file and bootstrap file from command line

Now let’s check the phpunit configuration and bootstrap file by running phpunit . on Tests folder and you should get this message:

it means the configuration file is working correctly and  we successfully load Joomla site using our bootstrap file. But if you get this error:  Call to a member function getTag() in libraries/cms/application/site.php, then we should modify our site.php file. This is a known bug on Joomla 3.2.x;

Edit /joomlapath/libraries/cms/application/site.php on line 305 and replace  $this->getLanguage()->getTag() to JFactory::getLanguage()->getTag() and run phpunit . again and you should not see that error again.

4. Create a test file

Then create a test file called ArticleTest.php in Tests folder. In this file we will try to display an existing article using article id for example the article id is 5, here is the code:

and then run phpunit . again and you should see all tests passed and the article content displayed on terminal. And thats it, Have fun with unit testing.

Leave a Comment

Your email address will not be published. Required fields are marked *