Getting Started With PHPUnit for Unit Testing

by Shahroze Nawaz  October 26, 2016

Even a small software development project usually takes hours of hard work. During the course of development, a number of major and minor bugs and problems creep in the structure and code of the application. Developers usually try to work out the bugs during the process of development.

The problem is that there is no surefire way of ensuring that all the bugs have been worked out of the final code. Another important aspect is the possibility of the introduction of further bugs when existing bugs are fixed. To address these problems, testing and QA are integral components of software development process.

Careful software development ensures that some test and QA methodology is built in the development process from the very beginning. The result of such a development process is a robust code that is easy to maintain and extend further. This article is the first one in the series of articles that focus on Unit Testing, a well known test methodology. I will use PHPUnit as the main test tool to show how to write unit tests in PHP. Once you get hold of the basics, getting started with PHPUnit is not that difficult.

Understanding Unit Testing

The word Unit refers to a block of code, method or an individual or independent class. Unit testing is a software testing process in which code blocks are checked to see whether the produced result matches the expectations.  The units are tested by writing a unique test case. Unit test are generally automatic but could be implemented manually. I have designed a short algorithm to define it visually how unit test work.

php unit testing

Benefits Of Unit Testing

  • Testing can be automated for speed.
  • Higher probability of bug free code
  • Easy to understand inherited code
  • Refactor code to the point of testability and understandability

Installing PHPUnit

To explain unit testing, I will use PHPUnit library, developed by Sebastian Burgmen.

Before using the library, you need to set up a proper environment. If you are doing unit testing in local environment, you must install XAMPP, Composer, Enable Xdebug, and finally install PHPUnit. However, thanks to Cloudways, you do not need to install anything except PHPUnit. Xdebug is easily enabled at a single click in the Advance tab in Server Management.

Related: How To Host PHP On DigitalOcean

Login to SSH terminal by using Master Credentials and go to the application folder by using the command

To install PHPUnit from Composer, run the following command in SSH terminal.

For installing from composer.json, add the following lines of code.

You can also install PHPUnit manually by downloading the latest version of PHPUnit from the official Website.

Directory Structure

Before starting the test process using PHPUnit, a proper directory structure for the files to be tested is required. At present, the directory structure will looks like this.

I will create a new folder for the files to be tested. Create a folder UnitTestFiles. In this folder, create a subfolder  Test. Create a new file phpunit.xml in this subfolder and add the following code to it.

The colors="true" will show the results in highlighted colors and <directory>./UnitTestFiles/Test/</directory> will ask PHPUnit for the location of the files to be tested.

Now the directory structure will look like:

All the project files will be located in the root, while the files to be tested will be located in the  Test folder

Basic Conventions to Write Test Case

Following are some basic conventions and steps for writing tests with PHPUnit:

  1. Test File names should have a suffix Test. For example, if First.php needs to be tested,the name of the test file name will be FirstTest.php
  2. Similarly, If the class name is MyFirstClass than the test class name will be MyFirstClassTest.
  3. Add test as the prefix for method names. For example,if the method name is getuser(), then in test class, it will become testgetuser(). You can also use @test annotation in document block to declare it as a testing method.
  4. All testing methods are public
  5. MyFirstClassTest should be inherited from PHPUnit\Framework\TestCase

These are the major ground rules for the testing framework. The essential configurations and settings are all setup. It is now time to write the first test case.

Writing The First Test Case

I will now start with a very simple test case.

Create a file FirstTest.php in UnitTestFiles/Test. Add the following code to it.

This is very basic test.  In the first line, I mentioned the namesapace where the test files are located. In the second line, I initialized the PHPUnit framework. FirstTest is the test class that extends Testcase. I have declared a method testTrueCheck() with prefix test. In the method, I simply declares a variable $condition = true; I will next validate (true or false) this variable with assertTrue. Save the file and run it through the SSH terminal by using the following commandunit testing result

Here is the result of our first unit test. To do another test, pass false to the variable $condition. The present result $condition = true is changed to  $condition = false; Run the test command in SSH terminal.

unit testing failure

This red line indicates the failure of the test since assertTrue reports an error and identified $condition as false.

Conclusion

This article explains a basic setup that help you in getting started with PHPUnit testing. I would like to  mention several changes in the latest version of PHPUnit. In the previous versions, the class extend with PHPUnit_Framework_TestCase. In the latest version, it extends with TestCase only. In the second article of this series, I will explain Assertions, data providers and other features in some details. If you have a question or would like to contribute to the discussion, please leave a comment.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...