Agile Zone is brought to you in partnership with:

I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 637 posts at DZone. You can read more from them at their website. View Full User Profile

Practical PHP Testing Patterns: Fresh Fixture

11.09.2010
| 4954 views |
  • submit to reddit

We are exploring the realm of fixtures, the dependencies needed for a test to run. Every test needs a fixture, even if it is only an object, the System Under Test, that must be created.

When implementing the Fresh Fixture pattern, each test builds its own fixture, without relying on any other fixtures to exist.

Of course, test should also tear down the fixture once they have finished; but if the fixture consists of transient objects (without database or other persistence involvement), the PHP garbage collection mechanism usually takes care of it and you do not need to do anything to free memory and your member variables.

Moreover, PHPUnit creates a single TestCase object (instance of your *Test class) before running each test method. Therefore, each private member of the class is not shared between different tests and starts from null every time. Even setUp() methods act only on the current TestCase object, and cannot influence the subsequent tests.

Pros & cons

When using Fresh Fixtures, tests have no shared state at all: the whole object graph and the potential external resources are recreated each time. Test isolation is almost guaranteed, at least if the code subjected to test is not using singletons or static calls.

Isolation is a very important property: imagine that the last test of your suite (which takes 10 minutes ro tun) fails when running in the suite but pass when running by itself. Debugging this would take much time.

The only great disadvantage of Fresh Fixture is that they are on average slower than their Shared counterpart (that we have not yet seen in this series). Particularly in the case of databases and out-of-memory resources, the need to recreate everything from scratch may render the use of Fresh Fixtures problematic. For testing in-memory objects instead, Fresh Fixtures are usually fine and you can avoid premature optimization.

Implementation

Fresh fixtures can be transient (objects, either under test or representing input data) or persistent (database rows and similar external data.) The need for persistent Fresh Fixture is clear for example when testing the interaction with a database (DAOs or Repositories).

No static variables should be used for fresh fixtures; as long as you use local variables (in the test methods) or object variables (also in setUp() and derivatives) you're fine as they will be destructed after each test run by PHPUnit.

This pattern is orthogonal to the Standard and Minimal Fixture ones: they apply to the design of the code that creates the fixture, which can be shared (Standard) or tailored to the single tests (Minimal). Thus, a Fresh Fixture is called so because it is recreated for each test: it is not important if all the tests create the same identical data structure or each one defines a single one.

Examples

The code sample reworks the code of the Standard Fixture article, which represents a Standard Fixture, and improves it by introducing a setUp() method and adding an additional test to show how PHPUnit resets the state of $this.

<?php
class MultidimensionalArrayTest extends PHPUnit_Framework_TestCase
{
/**
* @var array
*/
private $array;

/**
* @var boolean
*/
private $tryToInterfere;

/**
* This private method creates the Fresh Fixture, which is
* incidentally a Standard Fixture. In fact, the code is almost
* identical to the one from the relative article, with the difference
* of setUp() encapsulation.
*/
public function setUp()
{
$this->array = array(
'key' => array(4, 8, 15),
'otherKey' => array(16, 23, 42)
);
}

public function testArrayWalkRecursive()
{
array_walk_recursive($this->array, function(&$value) {
$value += 10;
});
$this->assertEquals(array(
'key' => array(14, 18, 25),
'otherKey' => array(26, 33, 52)
), $this->array);
}

/**
* Even if the first test modifies $this->array, it will
* be recreated by setUp() at the start of each test.
*/
public function testArrayMergeRecursive()
{
$result = array_merge_recursive($this->array, array(
'key' => array(100),
'otherKey' => array(1000, 10000)
));
$this->assertEquals(array(
'key' => array(4, 8, 15, 100),
'otherKey' => array(16, 23, 42, 1000, 10000)
), $result);
$this->tryToInterfere = true;
}

/**
* It's not that the next call to setUp()
* overwrites the previous value: $this is a third, new
* instance of the class MultidimensionalArrayTest.
*/
public function testTheWholeTestCaseIsRecreated()
{
$this->assertNull($this->tryToInterfere);
}
}
Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)