PHPUnit

Posted by: John in Untagged  on

All developers at one point or another test their code. Although it generally takes the forum of an echo to make sure your code is being ran or the correct data is being processed – it is a test. The unfortunate side effect is, all those tests will be removed. Therefor, when a piece of functionality breaks within your code, you sit at your desk manually sifting through your files once again adding echo statements to deduce the location of the problem. As an alternative, you might choose to take advantage of unit tests.

What are unit tests?
“Unit tests” are decoupled tests that can be ran externally and do not have to be removed during production. Object oriented programming languages usually provide a framework to ease the unit testing process. Java provides JUnit and PHP provides PHPUnit. I'm sure there are other unit testing frameworks for other languages, but I will be discussing PHPUnit.

How do I use PHPUnit?
There are a few ways to use PHPUnit. You can either download the PEAR plugin, or you can download a stand alone version – which is not officially supported. Moreover, if you use Zend Studio for Eclipse you can take advantage of its built in PHPUnit features, which is what I will be doing.

Lets first take a look at how to test in_array() when not using unit tests. It might look like the following:

 
  1. $data = array(“foo”, “bar”);
  2. if(in_array(“foo”, $data) {
  3.     echo “The value is in the array.;
  4. } else {
  5.     echo “Sorry, this array does not contain that value.;
  6. }
  7.  
  8. ?>
This is a legitimate test, which will let you know what is happening. However, as mentioned before, you will probably remove this from your code. Now lets take a look at the unit test.

1. We need to create a class that extends PHPUnit_Framework_TestCase.
2. We need to create a method named test* where * is a wild card.


 
  1. class ArrayTest extends PHPUnit_Framework_TestCase
  2. {
  3.     public function testDoesNotContain()
  4.     {
  5.  
  6.        
  7.     }    
  8. }
  9.  
  10. ?>


One of the many methods we inherit from PHPUnit_Framework_TestCase is assertEquals() which will check if the second parameter will returns the same value as the first parameter. Here is how that will look:


 
  1. class ArrayTest extends PHPUnit_Framework_TestCase
  2. {
  3.     public function testDoesNotContain()
  4.     {
  5.         // Create the Array fixture.
  6.         $data = array();
  7.  
  8.         // Assert that array does not contain "foo"
  9.         $this->assertEquals(false, in_array("foo", $data));
  10.     }
  11.    
  12. }
  13.  
  14. ?>
When this test is ran, it will pass. Lets add a few more tests:

 
  1. class ArrayTest extends PHPUnit_Framework_TestCase
  2. {
  3.     public function testDoesNotContain()
  4.     {
  5.         $data = array();
  6.  
  7.         $this->assertEquals(false, in_array("foo", $data));
  8.     }
  9.  
  10.     public function testDoesContainSingle()
  11.     {
  12.         $data = array("foo");
  13.  
  14.         $this->assertEquals(true, in_array("foo", $data));
  15.     }
  16.    
  17.     public function testDoesContainMany()
  18.     {
  19.         $data = array("foo", "bar", "bat");
  20.  
  21.         $this->assertEquals(true, in_array("bar", $data));
  22.     }
  23.    
  24. }
  25. ?>


How do you run these tests?
You can run the tests from the command line by issuing the phpunit command: phpunit ArrayTest. However, since I am using Zend Studio, by simply right clicking on the file in the php explorer, you will see a run menu. In the run menu there should be an option to run as PHPUnit Test. By doing that, it will switch you to the PHP Unit perspective and show you a nice visual output of your tests.

There is an abundance of other methods PHPUnit_Framework_TestCase comes with and a lot more advanced features PHPUnit comes with. This is only a brief discussion of what you can do. To read more about PHPUnit check out the Trac page: http://www.phpunit.de/

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy