<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Weblog van Paul de Raaij &#187; TDD</title>
	<atom:link href="http://www.paulderaaij.nl/tag/tdd/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paulderaaij.nl</link>
	<description>Algemeen weblog van Paul de Raaij</description>
	<lastBuildDate>Fri, 03 Feb 2012 11:19:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>A way to implement unit testing in Joomla!</title>
		<link>http://www.paulderaaij.nl/2010/07/31/a-way-to-implement-unit-testing-in-joomla/</link>
		<comments>http://www.paulderaaij.nl/2010/07/31/a-way-to-implement-unit-testing-in-joomla/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 12:21:28 +0000</pubDate>
		<dc:creator>Paul de Raaij</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Webdevelopment]]></category>
		<category><![CDATA[joomla]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.paulderaaij.nl/?p=203</guid>
		<description><![CDATA[Since a couple of months I&#8217;m not anymore self-employed and am I working for a company who develops for ActiveCollab, Magento and Joomla!. While developing I&#8217;m trying to work according the TDD paradigm. So you write your tests first and then you develop the business logic to succeed the failing test. I&#8217;d have to admit that Joomla! [...]]]></description>
			<content:encoded><![CDATA[<p>Since a couple of months I&#8217;m not anymore self-employed and am I working for a company who develops for ActiveCollab, Magento and Joomla!. While developing I&#8217;m trying to work according the TDD paradigm. So you write your tests first and then you develop the business logic to succeed the failing test. I&#8217;d have to admit that Joomla! never will be my first choice to develop on, but it isn&#8217;t as bad some are preaching.</p>
<p><span id="more-203"></span></p>
<p>While getting my way around the Joomla! framework I have a big problem getting the checked in unit tests to run. If you do a checkout of the unit tests from the svn you&#8217;ll see there are about 960 tests and about 600 fails straight out of the box. Pretty annoying and unusable to write your own tests against.</p>
<p>So I&#8217;ve been working on a test runner of my own. The test runner tries to use the most of the Joomla! framework, but some things it just have to do itself. It consists of 2 files. One file is the testloader which is setup as an autoloader using the JLoad class. The second class sets some configuration files and kickstarts the Joomla! framework.</p>
<p><strong>Here&#8217;s the code for the testloader</strong></p>
<pre class="brush: php; title: ;">

&lt;?php

/**

* Testloader class

*

* This class strips the requested class and passes it thru

* to the Joomla autoload function

*

* @author Paul de Raaij &lt;paul@paulderaaij.nl&gt;

*/

// Preload the model so we can add paths to the inlude path of JLoader

jimport( 'joomla.application.component.model' );

class TestLoader {

/**

* Strip the given class and pass it to the

* Joomla autoloader.

*

* If it's a component add the path to the

* include path of the loader

*

* @param String $classname

* @return bool Has the inclusion succeeded

*/

static public function load( $classname ) {

$explodedPath = self::explodeClassname( $classname );

if( $explodedPath == null ) {

return self::loadJoomlaClass( $classname );

}

// Add the folder to the loader include path

JModel::addIncludePath('../components/com_' . $explodedPath['component'] . '/' . $explodedPath['type'] . 's');

return self::loadJoomlaClass( $classname );

}

/**

* Explode the classname and strip component name,

* type and type name

*

* @param string $classname

* @return mixed Returns null when it's a core file. Returns an array with data if it's a component data

*/

static protected function explodeClassname( $classname ) {

$types = array('model', 'controller', 'helper', 'view');

$classname = strtolower($classname);

if( $classname == 'jmodel' ) {

return null;

}

$fileInfo = array();

foreach( $types as $type ) {

if( false !== strpos($classname, $type) ) {

$fileInfo['type']       = $type;

}

}

if( isset( $fileInfo['type'] ) ) {

$fileInfo['component']  = substr($classname, 0, strpos($classname, $fileInfo['type'] ) );

$fileInfo['name']       = substr($classname, ( strpos($classname, $fileInfo['type'] ) + strlen($fileInfo['type']) ) );

return $fileInfo;

}

return null;

}

/**

* Simple pass thru function to the Joomla autoloader

*

* @param string $class

* @return bool Has inclusion succeeded?

*/

static public function loadJoomlaClass( $class ) {

return JLoader::load( $class );

}

}

?&gt;
</pre>
<p>And this is the TestCase class which is required by each unit test file:</p>
<pre class="brush: php; title: ;">

&lt;?php

require_once 'PHPUnit/Framework.php';

// We are a valid Joomla entry point.

define('_JEXEC', 1);

// Setup the path related constants.

define('DS', DIRECTORY_SEPARATOR);

define('JPATH_BASE', dirname( __FILE__ ) . '\..');

define('JPATH_ROOT', JPATH_BASE);

define('JPATH_CONFIGURATION', JPATH_BASE);

define('JPATH_LIBRARIES', JPATH_BASE.DS.'libraries');

define('JPATH_METHODS', JPATH_ROOT.DS.'methods');

// Load the library importer, datbase class and configuration

require_once (JPATH_LIBRARIES.'/joomla/import.php');

require_once (JPATH_LIBRARIES.'/joomla/database/database.php');

// Load the TestLoader

require_once dirname(__FILE__) . '\lib\TestLoader.php';

// register specific test autloader

if( !defined('LOADED_AUTOLOADER') ) {

spl_autoload_register( array('TestLoader', 'load') );

define('LOADED_AUTOLOADER', true);

}

// Define configuration

jimport( 'joomla.registry.registry' );

require_once( JPATH_CONFIGURATION.'/configuration.php' );

// Create the JConfig object

$config = new JConfig();

// Get the global configuration object

$registry =&amp; JFactory::getConfig();

// Load the configuration values into the registry

$registry-&gt;loadObject($config);

$options = array ('driver' =&gt; $config-&gt;driver,

'host' =&gt; $config-&gt;host,

'user' =&gt; $config-&gt;user,

'password' =&gt; $config-&gt;password,

'database' =&gt; $config-&gt;database,

'prefix' =&gt; $config-&gt;prefix );

// Preload database with configuration

$db = JDatabase::getInstance( $options );

?&gt;
</pre>
<p>An example unit test looks like this:</p>
<pre class="brush: php; title: ;">

&lt;?php

require_once dirname(__FILE__) . '\..\..\..\TestCase.php';

class Model_Test_Base extends PHPUnit_Framework_TestCase {

protected $model = null;

public function setUp() {

$this-&gt;model = JModel::getInstance('&lt;modelname&gt;', '&lt;componentname&gt;Model');

}

public function testSimpleAssertion() {

$this-&gt;assertTrue( $this-&gt;model-&gt;test() );

}

}

?&gt;
</pre>
<p>Perhaps this isn&#8217;t the best way to use unit testing in Joomla! But at the moment this is the only thing I got working. If you&#8217;ve got some suggestions your glad to leave it behind as a reaction!</p>
<!-- PHP 5.x --><p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.paulderaaij.nl%2F2010%2F07%2F31%2Fa-way-to-implement-unit-testing-in-joomla%2F&amp;title=A%20way%20to%20implement%20unit%20testing%20in%20Joomla%21"><img src="http://www.paulderaaij.nl/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.paulderaaij.nl/2010/07/31/a-way-to-implement-unit-testing-in-joomla/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
