PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Testing PHP Code with PHPUnit
Pages: 1, 2

Sharing Setup Code

One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is the fixture of the test.

In Example 1, the fixture was simply the array stored in the $fixture variable. Most of the time, though, the fixture will be more complex than a simple array, and the amount of code needed to set it up will grow accordingly. The actual content of the test gets lost in the noise of setting up the fixture. This problem gets even worse when you write several tests with similar fixtures. Without some help from the testing framework, you would have to duplicate the code that sets up the fixture for each test you write.

PHPUnit supports sharing the setup code. Before running a test method, PHPUnit invokes a template method called setUp(). With setUp() you create the objects against which you will test. Once the test method has finished running, whether it succeeded or failed, PHPUnit invokes another template method called tearDown(). In tearDown() you clean up the objects against which you tested.

You can now refactor Example 1 and use setUp() to eliminate the code duplication. First declare the instance variable, $fixture, to use instead of a method-local variable. Then put the creation of the Array fixture into the setUp() method. Finally, remove the redundant code from the test methods and use the newly introduced instance variable, $this->fixture, instead of the method-local variable $fixture with the assertEquals() assertion method.

Example 2. Using setUp() to create the Array fixture

require_once 'PHPUnit2/Framework/TestCase.php';

class ArrayTest extends PHPUnit2_Framework_TestCase {
    protected $fixture;

    protected function setUp() {
        // Create the Array fixture.
        $this->fixture = Array();

    public function testNewArrayIsEmpty() {
        // Assert that the size of the Array fixture is 0.
        $this->assertEquals(0, sizeof($this->fixture));

    public function testArrayContainsAnElement() {
        // Add an element to the Array fixture.
        $this->fixture[] = 'Element';

        // Assert that the size of the Array fixture is 1.
        $this->assertEquals(1, sizeof($this->fixture));

setUp() and tearDown() will run once for each test method run. While it might seem frugal to run these methods only once for all the test methods in a test-case class, doing so would make it hard to write tests that are completely independent of each other.

What PHPUnit Has to Offer

Automated tests give you justified confidence in your code. You can use this confidence to take more daring leaps in improving your design (Refactoring), get along with your teammates better (Cross-Team Tests), improve relations with your customers, and go home every night with proof that because of your efforts the system is better now than it was this morning. Once you get used to writing automated tests, you will likely discover more uses for tests.

Extreme Programming, for example, demands collective code ownership, so all developers need to know how the entire system works. If you are disciplined enough to consequently use "speaking names" for your tests that describe what a class should do, you can use PHPUnit's TestDox feature to generate automated documentation for your project based on its tests. This documentation gives developers an overview of what each class of the project is supposed to do.

Code-Coverage Analysis gives you an insight into what parts of the production code execute when the tests are run and helps to measure testing completeness.

PHPUnit for PHP 5 is actively developed and maintained by Sebastian Bergmann. He is currently working on support for Mock Objects in PHPUnit 2.4, which he hopes to release later this year.

Sebastian Bergmann is the author of a variety of PHP software projects such as PHPUnit and phpOpenTracker.

Return to the PHP DevCenter.

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: