Unit Testing in PHP – The easy way

In one sentence, Unit testing is the process of testing small chunks of your code units as you write them. This is quite useful to be confident in your own unit of code before you integrate them into the whole system. With each part of your code being tested, you are sure each part can stand on their own and serve its purpose. To demonstrate unit testing in PHP, we would be using the latest version of the unit testing framework PHPUnit which was developed by Sebastian Bergmann

I would recommend you use the latest version of PHP. PHPUnit requires the dom, json, pcre, reflection, and spl extensions. You don’t need to worry, these extensions are always enabled by default.

As pointed earlier, we would be using PHPUnit to perform unit testing on our code in this tutorial. First things first, download PHPUnit here. What you will get is a single file with a .phar extension. And in case you are wondering what a .phar file is, a phar extension file provides a way to put entire PHP applications into a single file called a “phar” (PHP Archive) for easy distribution and installation, think of it as what .jar is for java. No unzipping is required, just copy the file into your project folder. I would suggest the root folder of your project. If you are using WAMP, I am guessing the location of your folder would look like something like this: C:wamp\www\[project-folder-name]. Keep the location in mind for later.

To run a unit test successfully, you need your actual code and a test suite or class. The actual code I would be testing is an arithmetic class that performs various arithmetic operations on two given numbers (type of number doesn’t matter, it’s PHP). Below is the arithmetic class:

value_one + $this->value_two;
    function substraction(){
        return $this->value_one - $this->value_two;
    function multiplication(){
        return $this->value_one * $this->value_two;

As you can see, it’s just a simple class to perform addition, subtraction and multiplication on two given numbers initialized to 0. Now we create our test class, which I should point out would extend PHPUnit_Framework_TestCase as shown below:

Let’s initialize a variable that would later hold our arithmetic object.

private $arithmetic = null;

We setup our test suite and set the values we want to use in the test (9 and 3), as in:

function setUp()
       $this->arithmetic = new arithmetic();
       $this->arithmetic->value_one = 9;
       $this->arithmetic->value_two = 3; 

And our tear down function to unset our arithmetic object when we are done testing is:

function tearDown()
        unset($this->arithmetic);  //or $this->arithmetic = null;

We proceed to build our actual test case. This is the function that would actually be tested for success or failure. I would be testing only the addition function here, the rest of the operations are tested in the code file attached to this tutorial:

function testAddition(){        
        $correctAnswer = 12;
        $this->assertEquals($correctAnswer, $this->arithmetic->addition());       

You would see on the last line of our code that we called an assertEquals method. This method does a comparison of the first parameter which is actually the correct value (9+3 =12) and the value returned by the addition() method. If they are equal, our code has no problem, we get a success message, if not, we get a failure message. There are other assertions you might want to make in your unit test case. You can check the full list of assertions and how to use them in the official documentation of PHPUnit. So we have our test suite setup with only one test case available in it (which is the addition test case). To carry out the unit test, we launch command prompt. We change the current directory to the directory where our project files and the PHPUnit.phar file is located using the command

cd C:\wamp\www\

Kindly note that I would have to include my arithmetic class in my test case, but I didn’t use the PHP include method, I would use a bootstrap method that PHPUnit has. Here’s the command to bootstrap my needed file and also run my test case in my test suite.

C:\php5\php.exe phpunit.phar --bootstrap src\arithmetic.php tst\test-arithmetic.php

Where src\arithmetic.php is the file to include in my test suite and tst\test-arithmetic.php is the test suite that contains the test case. When you run this, you should have an OK message with details of the test and assertions as shown below:
PHPUnit OK Message

Now, let’s try changing the value of the $correctAnswer variable to 11, which is wrong, remember 9+3 = 12, save and run the earlier command in command prompt. You should see a message that indicates the test failed and also giving full details of the test.
PHPUnit FAIL Message

You can have more than one test case in a test suite as shown in the test suite I attached to this post.

Source Code: Download Here