Testing is as important as development in software life cycle. Without proper testing, we cannot be assured that software is bug free and is working fine.

Testing can be done at two phases in software life cycle, during development and after development.

Testing the application after full completion of software has its problems:

  1. If a major bug is found, software logic needs to be changed and that will consume lot of time
  2. Fixing bugs after full development may generate more bugs if components have dependencies

Testing during development have advantages:

  1. Bugs are found during developing a component and hence bug fixing becomes easy
  2. Testing can be done with automated test tools which reduces testing time
  3. We can test multiple components simultaneously which are independent of each other

Unit Testing

Unit testing is used to test smallest components of the system. The purpose of unit testing is to make sure every component is doing its work properly. It is the simplest of the testing and is done in-memory, means unit testing never involves database.

PHPUnit

PHPUnit is one of the most popular unit testing framework for PHP. It provides mocking objects, logging, analysis and many other features.

Let us create an example that demonstrates how PHPUnit can be used to test functions in a class.

Here are the steps:

  1. Create a new folder named unittest under your web server’s web folder. Since I’m using XAMPP on Windows, I will create this folder in c:\xampp\htdocs folder. On Ubuntu, you can create it in /var/www/html folder.
  2. Create composer.json file in this folder with following text:
    {
      "require": {
      },
      "require-dev": {
        "phpunit/phpunit": "3.7.14"
      },
      "autoload": {
        "psr-0": {
          "src": ""
        }
      }
    }

    Our source PHP classes will be placed in unittest/src folder.

  3. I assume we have composer installed on our system. Now, on a command prompt, go to the unittest folder and execute the following command:
    composer install
  4. Next we will create a new file named phpunit.xml in the unittest folder. This file will be used to define PHPUnit settings. Add following lines to this file:
    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit colors="true" bootstrap="./vendor/autoload.php">
        <testsuites>
            <testsuite name="FirstTest">
                <directory>./tests</directory>
            </testsuite>
        </testsuites>
    </phpunit>

    colors=”true” will display our test results in colors
    <directory> defines where our test cases are found

  5. Next, we will create a simple class named Math.php in unittest/src folder. This class will have simple functions that perform mathematical operations. Here is the class:
    <?php
    namespace src;
    
    class Math
    {
        function add($x, $y){
            return $x + $y;
        }
    
        function subtract($x, $y){
            return $x - $y;
        }
    }
  6. Now, we will create a test class named MathTest.php under unittest/tests folder.
    Note:
    To make testing simple, PHPUnit suggests that test classes have same name as actual class with Test appended. Similarly, test methods will have the keyword test prefixed.
    Here is our first test class:

    <?php
    namespace unittest\tests;
    
    use src\Math;
    
    class MathTest extends \PHPUnit_Framework_TestCase
    {
        function testAdd(){
    
            $obj = new Math();
    
            $this->assertEquals(10, $obj->add(4,6));
        }
    
        function testSubtract(){
    
            $obj = new Math();
    
            $this->assertEquals(-2, $obj->subtract(4,6));
        }
    }
  7. To run testing on the command prompt, execute following command from within unittest folder:
    phpunit

You should now see the test results.