SnapShooter Backups Server, Database, Application and Laravel Backups - Get fully protected with SnapShooter

PHPUnit Beginner Part 3: Test Double

This is part 3 of PHPUnit Beginner. In this tutorial we will explain when and how to use test doubles in our test.

When to use test double

As mentioned in the first part of this series. One of PHPUnit's powerful features is test double. It is very common in our code, a function of one class is calling another class's function. In this case, we have a dependency in these two classes. In particular, the caller class has a dependency on calling class. But as we already know in part 1, unit test should test the smallest unit of functionality, in this case, it should test only the caller function. To solve this problem, We can use test double to replace the calling class. Since a test double can be configured to return predefined results, we can focus on testing the caller function.

Types of test doubles

Test double is a generic term for objects we use, to replace real production ready objects. In our opinion, it is very useful to categorize test doubles by their purpose. It does not only make it easy for us to understand the test case, but also make our code friendly to other parties.

Accordingly to Martin Fowler's post, There are five types of test double:

  1. Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  2. Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production.
  3. Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.
  4. Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
  5. Mocks are pre-programmed with expectations which form a specification of the calls they are expected to receive. They can throw an exception if they receive a call they don't expect and are checked during verification to ensure they got all the calls they were expecting.

How to create test double

PHPUnit's method getMockBuilder can be used to create any similar user defined objects. Combining with its configurable interface. We can use it to create basically all five types of test doubles.

Add test double to our first unit test

It is meaningless to use test double to our calculator test case, since currently the Calculator class has no dependency on other classes. However to demonstrate how to use test double in PHPUnit, we will create a stub Calculator class and test it.

Let's add a test case called testWithStub to our existing class:

public function testWithStub()
    // Create a stub for the Calculator class.
    $calculator = $this->getMockBuilder('Calculator')
    // Configure the stub.
    $this->assertEquals(6, $calculator->add(100,100));
  1. getMockBuilder() method creates a stub similar to our Calculator object.
  2. getMock() method returns the object.
  3. expects() method tells the stub to be called any number of times.
  4. method() method specifies which method it will be called.
  5. will() method configures the return value of the stub.

The end

We have completed the PHPUnit Beginner series. You should have gained enough knowledge on PHPUnit to start implementing unit tests on your code. If you are looking for more, checkout the The Grumpy Programmer's PHPUnit Cookbook.

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.