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:
<phpunit bootstrap="bootstrap.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" verbose="true"> </phpunit>
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:
<?php error_reporting(E_ALL); date_default_timezone_set('UTC'); define('_JEXEC', 1); define('JOOMLA_PATH',realpath(dirname(__FILE__).'/../../../')); $_SERVER['HTTP_HOST'] = 'localhost'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = ''; if (file_exists(JOOMLA_PATH . '/defines.php')) { include_once JOOMLA_PATH . '/defines.php'; } if (!defined('_JDEFINES')) { define('JPATH_BASE', JOOMLA_PATH); require_once JPATH_BASE . '/includes/defines.php'; } require_once JPATH_BASE . '/includes/framework.php'; $app = JFactory::getApplication('site'); define('JPATH_COMPONENT',JOOMLA_PATH.'/components/com_content');
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:
<?php include JPATH_COMPONENT.'/content.php'; class ArticleTest extends \PHPUnit_Framework_TestCase { public function setUp() { } public function testSingleArticle(){ $input = JFactory::getApplication()->input; $input->set('id',5); $input->set('view','article'); $input->set('option','com_content'); $controller = JControllerLegacy::getInstance('Content'); $controller->execute(''); } }
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.