Jasa Pembuatan Aplikasi Murah Berkualitas
konsultasi gratis sekarang

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

Published on Sunday, Jan 19, 2014

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: 

<phpunit bootstrap="bootstrap.php"

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:

define('_JEXEC', 1);
$_SERVER['HTTP_HOST'] = 'localhost';

if (file_exists(JOOMLA_PATH . '/defines.php'))
	include_once JOOMLA_PATH . '/defines.php';

if (!defined('_JDEFINES'))
	require_once JPATH_BASE . '/includes/defines.php';

require_once JPATH_BASE . '/includes/framework.php';

$app = JFactory::getApplication('site');

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:

PHPUnit 3.7.28 by Sebastian Bergmann.

Configuration read from /joomlapath/components/com_content/Tests/phpunit.xml

Time: 0 ms, Memory: 4.50Mb

No tests executed!

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;

PHP Fatal error:  Call to a member function getTag() on a non-object in /joomlapath/libraries/cms/application/site.php on line 305

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.

// Get language			
//$lang_code = $this->getLanguage()->getTag();
$lang_code = JFactory::getLanguage()->getTag();
$languages = JLanguageHelper::getLanguages('lang_code'); 

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:

include JPATH_COMPONENT.'/content.php';

class ArticleTest extends \PHPUnit_Framework_TestCase {	
	public function setUp() {		
	public function testSingleArticle(){
		$input = JFactory::getApplication()->input; 
		$controller = JControllerLegacy::getInstance('Content');

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.