Actions

Difference between revisions of "Unit Testing -- Data Driven Example"

From Joomla! Documentation

m
m
Line 141: Line 141:
  
 
We check that we get the expected result, and we check that the cache works.
 
We check that we get the expected result, and we check that the cache works.
 +
 +
==== Defining the Test Environment ====
 +
JRequest gets data from the PHP super-globals ($_GET, $_POST, etc.). We're going to define a helper class that both initializes these variables and holds our data set of tests (see /unittest/tests/libraries/joomla/environment/JRequest-helper-dataset.php). The initSuperGlobals() method will set up the environment. One of the things we do is define an entry called 'tag' in each of the super-globals, each with a value that identifies the source variable.
 +
 +
In our unit test class, we'll define a setUp() method that calls the helper and clears the cache:
 +
<source lang="php">
 +
function setUp() {
 +
JRequestTest_DataSet::initSuperGlobals();
 +
// Make sure the request hash is clean.
 +
$GLOBALS['_JREQUEST'] = array();
 +
}
 +
</source>
 +
This code is called by PHPUnit at the beginning of every test.
  
 
==== Defining a Data Set ====
 
==== Defining a Data Set ====
Line 148: Line 161:
 
* For some of these combinations, JRequest will use JInputFilter. In these cases we need to define the expected calls to our mock class, and the result.
 
* For some of these combinations, JRequest will use JInputFilter. In these cases we need to define the expected calls to our mock class, and the result.
  
Sample data set entry:
+
We define each of these tests in an array in our helper class, $getVarTests. Here is a sample entry:
 
<source lang="php">
 
<source lang="php">
 
array(
 
array(
Line 157: Line 170:
 
),
 
),
 
</source>
 
</source>
 +
In this entry, we're passing $name = 'tag', $default = null, $hash = 'request', $type = 'none', and $mask = 0.
 +
 +
We expect the return value for this combination of parameters to be the string 'from _REQUEST'.
 +
 +
We also expect that this test will result in a single call to JInputFilter::clean() with $source = 'from _REQUEST' and $type = 'NONE'. Our mock of this method will return the string 'from _REQUEST', and we expect this method to be called with these parameters precisely once.
 +
 +
You can find the whole data set in /unittest/tests/libraries/joomla/environment/JRequest-helper-dataset.php.
 +
 +
Some readers may be surprised when they don't find tests to verify that strings like '123abc' are not numeric. There are no tests like this because although this is the most common use of getVar(), the actual filtering is done by the JFilterInput class, so test cases like that belong with the JFilterInput tests. Our tests merely need to verify that the data is coming from the right place.

Revision as of 16:02, 12 May 2008

Contents

Anatomy of a Data-Driven Test

This expands on the basics given in (add link). In this example, we're interested in running a number of tests on the JRequest::getVar() method, each with different parameters. While it's possible to just write one test and then copy it once for each test, this would create a lot of duplicate code, add a lot of needless lines to the test file, and make it harder to organize test cases.

We also need to introduce a mock class with this example. Let's dig into the test to see why.

Defining the Test Criteria for JRequest::getVar()

We'll start by getting an understanding of what actually needs to be tested when we say "test getVar". Looking at the code, we can learn a few things:

  • The getVar method uses the $hash parameter to determine the source of the variable. This means we should test all expected values for $hash and a few invalid ones as well.
  • The class maintains a cache in $GLOBALS['_JREQUEST']. This means we should check to ensure that the hash works as expected, and that we need to pay some attention to ensuring that the cache is in a known state when we run a test.
  • getVar() calls the protected method _cleanVar() to do the real work. Depending on the value of the $mask parameter, this might result in a call to JFilterInput::clean(). We learn two things. First, we need to test various values for $mask (now in combination with $hash). Second, since testing JFilterInput is the responsibility of a unit test for that class, we will need to create a mock JFilterInput class, so we can isolate JRequest.

Building a Mock Class

(ed note: this should be its own page)

Defining a Mock Class for JFilterInput

There are two ways to look at mock classes. If a class is pretty simple, it's possible to write a common class that can be used by a number of unit tests. However if the class is more complex, it's better to write a test-specific mock that only implements the subset of the class that's needed.

The JFilterInput class falls into the second category. Writing a good general purpose mock for it is too complicated and time consuming, so we will create a subset that only does what we need for our JRequest tests.

JRequest uses two methods in JFilterInput: getInstance() and clean().

The getInstance method is straightforward. It returns an instance a JFilterInput object. Since we aren't interested in the internals of JFilterInput, we have no need to pay attention to the parameters passed to getInstance() in our mock.

For clean(), we're interested in making sure that JRequest calls clean() with the expected number parameter values, and that clean is called the appropriate number of times (this will verify the cache mechanism). Our mock also needs to return the clean version of the parameters.

Implementing the JFilterInput Mock Class

This class follows a common pattern for mock objects. For each test, we define expectations. An expectation is a combination of the expected parameters and the number of calls to a mock method.

This is a simple case because we have only one method to set expectations for: clean(). The below is the complete code for our mock, which can be found in /unittest/tests/libraries/joomla/environment/JFilterInput-mock-general.php


/**
 * Mock of JFilterInput for JRequest testing
 */
class JFilterInput
{
        /**
         * Information on the calls expected to the mock object.
         *
         * This array is indexed by a hash of the source and type; each element is
         * an array containing the source, type, expected response and number of
         * expected calls.
         */
        static private $_expectations = array();
 
        /**
         * Returns a reference to a mock input filter singleton
         */
        function & getInstance()
        {
                static $instance;
 
                if (! $instance) {
                        $instance = new JFilterInput();
                }
                return $instance;
        }
 
        /**
         * Stub for the clean method.
         *
         * @access  public
         * @param   mixed   Input string/array-of-string to be 'cleaned'
         * @param   string  Return type for the variable (INT, FLOAT, BOOLEAN, WORD,
         * ALNUM, CMD, BASE64, STRING, ARRAY, PATH, NONE)
         * @return  mixed   Canned response based on table lookup.
         * @static
         */
        function clean($source, $type='string')
        {
                $hash = md5($source . '|' . strtoupper($type));
                if (! isset($this -> _expectations[$hash])) {
                        $this -> _expectations[$hash] = array(
                                'source' => $source,
                                'type' => $type,
                                'result' => null,
                                'count' => 0,
                        );
                }
                --$this -> _expectations[$hash]['count'];
                return $this -> _expectations[$hash]['result'];
        }
 
        function mockReset() {
                $this -> _expectations = array();
        }
 
        function mockSetUp($source, $type, $result, $count = 1) {
                $hash = md5($source . '|' . strtoupper($type));
                $this -> _expectations[$hash] = array(
                        'source' => $source,
                        'type' => $type,
                        'result' => $result,
                        'count' => $count
                );
        }
 
        function mockTearDown() {
                foreach ($this -> _expectations as $hash => $info) {
                        if (! $info['count']) {
                                unset($this -> _expectations[$hash]);
                        }
                }
                if (count($this -> _expectations)) {
                        return $this -> _expectations;
                }
                return true;
        }
 
}

The mockSetup() method defines the expectations for the calls to clean() during a test. This is simple. For each combination of clean($source, $type), we specify the what clean should return ($result) and the number of times the method should be called with those parameters ($count).

The mockTearDown() method is called at the end of the test. If an expectation has been met, the value of count will be zero. If our test ran exactly as expected, then all expectations should have a zero count. Expectations with a zero count are removed from the array, and if the resulting array is empty then the test passed. If there are non-zero values then either the method wasn't called as many times as expected (a count greater than zero), or it was called with parameters we didn't anticipate (a negative count). mockTearDown() either returns true on success, or returns a list of mismatched expectations.

Of the two methods we need to mock, getInstance() simply returns a new instance of our mock. The clean() method matches the input parameters with expectations, either decrementing the count for parameters we expected, or creating a record of parameters we didn't expect.

That's all we need to support our test for JRequest. Let's put it to use.

Sample general-purpose mock: /unittest/tests/libraries/joomla/plugin/JPluginHelper-mock-general.php

A Unit Test for JRequest::getVar()

The core of our unit test is simple. For each combination of inputs we want to do this:

                /*
                 * Get the variable and check the value.
                 */
                $actual = JRequest::getVar($name, $default, $hash, $type, $mask);
                $this -> assertEquals($expect, $actual, 'Non-cached getVar');
                /*
                 * Repeat the process to check caching (JFilterInput should not
                 * get called unless the default is being used).
                 */
                $actual = JRequest::getVar($name, $default, $hash, $type, $mask);
                $this -> assertEquals($expect, $actual, 'Cached getVar');

We check that we get the expected result, and we check that the cache works.

Defining the Test Environment

JRequest gets data from the PHP super-globals ($_GET, $_POST, etc.). We're going to define a helper class that both initializes these variables and holds our data set of tests (see /unittest/tests/libraries/joomla/environment/JRequest-helper-dataset.php). The initSuperGlobals() method will set up the environment. One of the things we do is define an entry called 'tag' in each of the super-globals, each with a value that identifies the source variable.

In our unit test class, we'll define a setUp() method that calls the helper and clears the cache:

        function setUp() {
                JRequestTest_DataSet::initSuperGlobals();
                // Make sure the request hash is clean.
                $GLOBALS['_JREQUEST'] = array();
        }

This code is called by PHPUnit at the beginning of every test.

Defining a Data Set

What we know about our data set is this:

  • We need to test many combinations of the parameters $name, $default, $hash, $type, and $mask.
  • For each of these combinations, getVar() should return a single expected result.
  • For some of these combinations, JRequest will use JInputFilter. In these cases we need to define the expected calls to our mock class, and the result.

We define each of these tests in an array in our helper class, $getVarTests. Here is a sample entry:

                array(
                        'tag',  null,       'request',  'none', 0, 'from _REQUEST',
                        array(
                                array('from _REQUEST', 'NONE', 'from _REQUEST', 1),
                        ),
                ),

In this entry, we're passing $name = 'tag', $default = null, $hash = 'request', $type = 'none', and $mask = 0.

We expect the return value for this combination of parameters to be the string 'from _REQUEST'.

We also expect that this test will result in a single call to JInputFilter::clean() with $source = 'from _REQUEST' and $type = 'NONE'. Our mock of this method will return the string 'from _REQUEST', and we expect this method to be called with these parameters precisely once.

You can find the whole data set in /unittest/tests/libraries/joomla/environment/JRequest-helper-dataset.php.

Some readers may be surprised when they don't find tests to verify that strings like '123abc' are not numeric. There are no tests like this because although this is the most common use of getVar(), the actual filtering is done by the JFilterInput class, so test cases like that belong with the JFilterInput tests. Our tests merely need to verify that the data is coming from the right place.