PHPUnit Testing


How To Test PHP Applications With PHPUnit

PHPUnit is a testing framework that can be used to find coding errors and mistakes. On simpler level, you may have had some times when you have used echo, print and die() to track down errors and bugs in your code.

Although using these techniques can help you find problems, using PHPUnit can be applied to help find issues and problems with a much broader analysis. PHPUnit has many built-in methods that you can use to find problems. Then, you can run the command line to read its output. At this point, it will help pinpoint a target of concern.

If your company or testing platform can be accessed by various users, one coder could be writing the code while the lead developer could run tests with PHPUnit to find and fix the problems of the junior developer.

When you are testing with PHPUnit, your test file will need to make a class that extends the PHPUnit_Framework_TestCase class. An example is shown below.

 class MyTest extends PHPUnit_Framework_TestCase { } 

With that said, I will now show you how to install and use PHPUnit on Linux and windows. This tutorial is intended to help getting you setup and testing in no time at all. Since using and understanding PHPUnit can take some time to use effectively, you will probably want to move on to more documentation and perhaps further reading from books. 

Linux

The easiest method to use PHPUnit is to download the '.phar' file and run it on your test file.

To download the phpunit.phar file,

 wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar 

After the file is downloaded, you can run it from any folder and on any test file. A very easy way to use it is to move it to the folder with the test file(s). Then, open that folder with the Linux terminal and run the command.

 php phpunit.phar my_test.php or php phpunit.phar /var/www/test.php 

Using Pear

 sudo pear channel-discover pear.phpunit.de sudo pear install -a phpunit/PHPUnit 
 whereis phpunit /usr/bin/phpunit 
 username@username-VirtualBox:/usr/bin$ phpunit --version PHPUnit 4.0.17 by Sebastian Bergmann 

Windows 

To use PHPUnit on Windows, you only need to download the phpunit.phar file and move it into the PHP folder for your Wamp server. In this example, the file path is 'C:\wamp\bin\php\php5.3.13'.

Then, the command is the same as described above for using Linux.

 php phpunit.phar filname.php 

 

A Few Tips

Typically, PHPUnit uses 1 file that will run checks on another file. You can use the require_once() function to include the file for testing. When you are testing classes, the file being tested will have a unique class name while the test file will use a class with the same name that will include the word Test at the end of it.

In the example below, the file for our application uses the class name ‘MyClassName’ while the testing file uses the class name ‘MyClassnameTest’.

Example Test File

 <?php  require_once('FileToTest.php');  class MyClassnameTest extends PHPUnit_Framework_TestCase {   public function setUp(){ }   public function tearDown(){ }    public function testMyTestingIsValid()   {     // Make assertions here   } } ?> 

Example File(FileToTest.php) and Its Class 

 <?php class MyClassname { public $variable = "Test";        public function returnSampleObject()   {     return $this->variable;   } } ?> 

When you PHPUnit, you will use built-in methods like:

 public function setUp(){ } and  public function tearDown(){ } 

On top of that, you may use one or more test methods such as;

 public function testMyTestingIsValid(){} 

Under normal OOP programming, these methods would not be used unless the method is called. The examples below show how the typical method is used.

 $this->testMtTestingIsValid(); or $my_new_object = new ClassName(); echo $my_new_object->testMtTestingIsValid(); 

But, with PHPUnit, all functions in your test file will run automatically if they have the keyword test in front of the function. So, a public method like testMyTestingIsValid() just runs and does the adequate checks on the file you included.

When you make a test, you will create an object from the class you are testing. Then, you can use assertion functions to test your results. Many assertions can be found at http://phpunit.de/manual/4.1/en/appendixes.assertions.html.