PDA

View Full Version : Test driven programming in PHP



kgun
03-20-2010, 06:05 AM
1. Background.

Some are used to developing procedural PHP code. If you write procedural code, you miss the power of object oriented programming (http://www.computer-books.us/php_2.php). In addition, some web developers put this code on the internet without testing it on their own test server (http://www.webproworld.com/discussion-forum/49889-make-your-own-test-webserver-5-minutes.html#post254947). Formally that can be done by only allowing my own Ip address access through .htaccess. So we test our code to see how it functions on the internet. If it is broken, we look for bugs, fix them and load the code to our web server and refresh the page. But this is not a formal test environment.

2. Unit testing and extreme programming.

Extreme Programming: A Gentle Introduction. (http://www.extremeprogramming.org/) is the informal home page of extreme programming, Xp for short. The idea in Xp is that testing shall be formal and you write the tests before you write the code. Extreme programming is much more and there is written tons of books on the subject. Java programmers are used to unit testing (http://www.junit.org/). This concept is also developed in PHP and there are libraries that has classes for PHP unit testing that can be plugged into your projects. If you learn extreme programming and unit testing, your productivity may increase dramatically.

3. The first step, a simple example.

There are many books on this subject and one of my favourite PHP books http://www.webproworld.com/programming/95633-professional-php-6-a.html#post483321 has a short section (Chapter 13) on Unit testing. It is the very first step in testdriven application development with PHP. There is a more formal example in the important chapter 19 "A Real World Case Study". Before you try the code in the book, you have to download and install PHPUnit (http://www.phpunit.de/manual/current/en/installation.html). Personally I downloaded and installed version PHPUnit-1.3.3 (http://pear.phpunit.de/get/) as I think that version was used when the book was published. That means that I decompressed the code, installed PHPUnit.php and the PHPUnit folder with it's files in the ch13 code directory. That is done in a few minutes. Here is a short example. The methodology is the same on real world code.

testsuite.php


<?php
require_once 'testcase.phpm';
require_once 'PHPUnit.php';

$objSuite = new PHPUnit_TestSuite("MyTestCase");
$strResult = PHPUnit::run($objSuite);

print $strResult->toString(); // String output for commandline testing.
print $strResult->toHTML(); // Output to show in a web browser.
?>
testcase.phpm


<?
require_once("testclass.phpm");
require_once("PHPUnit.php");

class MyTestCase extends PHPUnit_TestCase
{
var $objMyTestClass;

function __construct($name) {
$this->PHPUnit_TestCase($name);
}

function setUp() {
$this->objMyTestClass = new TestClass();
}

function tearDown() {
unset($this->objMyTestClass);
}

function testMyMethod() {
$actualResult = $this->objMyTestClass->myMethod('parameter');
$expectedResult = 'expected result';
$this->assertTrue($actualResult == $expectedResult);
}
}


?>
testclass.phpm



<?php

class TestClass {
private $testVar;

function myMethod($strParam) {
$this->testVar = $strParam;
return('expected result');
}

};
If you have done everything correct and run the test, you should get the following output (in your web browser).

TestCase MyTestCase->testMyMethod() passed

TestCase MyTestCase->testMyMethod() passed

The phpm file extension may seem unusual to you. It is part of the authors native templating system that is explained in greater detail in chapter 13.

4. Additional resources.

Sebastian Bergmann (http://sebastian-bergmann.de/)

Article: Test Driven Development in PHP (http://www.phpbuilder.com/columns/baker20040202.php3)

Go to Amazon and search for books

Test Driven Development

Forthcoming book:

"Pro PHP Refactoring with Test-Driven Design" by Francesco Trucchia and Jacopo Romei

http://www.webproworld.com/web-programming-discussion-forum/44914-soft-introduction-object-oriented-programming.html#post226176