Unit Testing -- a Simple Example
From Joomla! Documentation
im just starting to look at this but the code given in this page didn't work for me. Instead I had to wrap the setup and test functions inside a class, e.g.
<?php
if (! defined('JUNIT_MAIN_METHOD')) {
define('JUNIT_MAIN_METHOD', 'JRequestTest_GetMethod::main');
$JUnit_home = DIRECTORY_SEPARATOR . 'unittest' . DIRECTORY_SEPARATOR;
if (($JUnit_posn = strpos(__FILE__, $JUnit_home)) === false) {
die('Unable to find ' . $JUnit_home . ' in path.');
}
$JUnit_posn += strlen($JUnit_home) - 1;
$JUnit_root = substr(__FILE__, 0, $JUnit_posn);
$JUnit_start = substr(
__FILE__,
$JUnit_posn + 1,
strlen(__FILE__) - strlen(basename(__FILE__)) - $JUnit_posn - 2
);
require_once $JUnit_root . DIRECTORY_SEPARATOR . 'setup.php';
}
/*
* Now load the Joomla environment
*/
if (! defined('_JEXEC')) {
define('_JEXEC', 1);
}
require_once JPATH_BASE . '/includes/defines.php';
/*
* Mock classes
*/
// (no mocks for this test)
/*
* We now return to our regularly scheduled environment.
*/
require_once JPATH_LIBRARIES . '/joomla/import.php';
jimport( 'joomla.environment.request' );
class addElementTest_Mode1 extends PHPUnit_Framework_TestCase
{
/**
* Clear the cache
*/
function setUp() {
// Make sure the request hash is clean.
$GLOBALS['_JREQUEST'] = array();
}
function testGetMethod()
{
$this->assertEquals('1', '1');
}
}
// Call main() if this source file is executed directly.
if (JUNIT_MAIN_METHOD == 'JRequestTest_GetMethod::main') {
JRequestTest_GetMethod::main();
}
?>
Pollen8 10:06, 29 May 2008 (EDT) pollen8