Difference between revisions of "Unit Testing -- a Simple Example"

From Joomla! Documentation

Revision as of 06:53, 5 March 2008

Anatomy of a Basic Test

Here we're going to look at a very simple test for the JRequest class.

First there's a block of code that's derived from the sample templates:

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(
                $JUnit_posn + 1,
                strlen(__FILE__) - strlen(basename(__FILE__)) - $JUnit_posn - 2
        require_once $JUnit_root . DIRECTORY_SEPARATOR . 'setup.php';

The only part that's customized here is JRequestTest_GetMethod, which is the name of the test class. This code corresponds to some at the end of the file. Together, this allows you to run the test directly from the command line. Here's the corresponding code at the bottom:

// Call main() if this source file is executed directly.
if (JUNIT_MAIN_METHOD == 'JRequestTest_GetMethod::main') {

You can see the same transformation, the name of the test class is the only change from the template.

Back up at the top, once the main routine has been set up, we set up the test environment:

 * 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' );

This is all pretty straightforward. The steps are:

  • Define _JEXEC which lets us bring in the Joomla! framework.
  • Do nothing in the section for mock classes, since there are none for this test.
  • Include import.php, which sets the framework up.

Now we're ready to define the test class. There are three methods.

  • main() runs the tests if this file was invoked in stand-alone mode.
  • Any method starting with "test" is called by PHPUnit.
  • The setUp() method is called before each test is run (you can also have a tearDown method that is called once a test completes).

The main() method is straightforward and is copied from the test template with no change.

The setUp() method initializes the test. The JRequest class caches information in a global variable $_JREQUEST. Since caching can affect the logic flow, we use setUp to clear the cache out.

 * Clear the cache
function setUp() {
        // Make sure the request hash is clean.
        $GLOBALS['_JREQUEST'] = array();

Now onto the test. JRequest::getMethod() is supposed to return the HTTP request type. So our test is very simple. We modify the request type by changing the relevant server variable, then test to make sure getMethod() returns the expected result.

function testGetMethod()
        $_SERVER['REQUEST_METHOD'] = 'post';
        $this -> assertEquals('POST', JRequest::getMethod());
        $_SERVER['REQUEST_METHOD'] = 'get';
        $this -> assertEquals('GET', JRequest::getMethod());

That's it. We have our test case ready to run. Let's try it.

$ php JRequest-0000-getmethod-test.php
PHPUnit 3.2.11 by Sebastian Bergmann.


Time: 0 seconds

OK (1 test)

We pass the test. All is good!