TDD of a Simple App in PHP

In this tutorial, I will present an end-to-end example of a simple application - made strictly with TDD in PHP. I will walk you through each step, one at a time, while explaining the decisions I made in order to get the task done. The example closely follows the rules of TDD: write tests, write code, refactor.
TDD is a "test-first" technique to develop and design software. It is almost always used in agile teams, being one of the core tools of agile software development. TDD was first defined and introduced to the professional community by Kent Beck in 2002. Since then, it has become an accepted - and recommended - technique in everyday programming.
TDD has three core rules:
  1. You are not allowed to write any production code, if there is not a failing test to warrant it.
  2. You are not allowed to write more of a unit test than is strictly necessary to make it fail. Not compiling / running is failing.
  3. You are not allowed to write more production code than is strictly necessary to make the failing test pass.
PHPUnit is the tool that allows PHP programmers to perform unit testing, and practice test-driven development. It is a complete unit testing framework with mocking support. Even though there are a few alternative choices, PHPUnit is the most used and most complete solution for PHP today.
To install PHPUnit, you can either follow along with the previous tutorial in our "TDD in PHP" session, or you can use PEAR, as explained in the official documentation:
  • become root or use sudo
  • make sure you have the latest PEAR: pear upgrade PEAR
  • enable auto discovery: pear config-set auto_discover 1
  • install PHPUnit: pear install
More information and instructions for installing extra PHPUnit modules can be found in the official documentation.
Some Linux distributions offer phpunit as a precompiled package, though I always recommend an installation, via PEAR, because it ensures that the most recent and up-to-date version is installed and used.
If you're a fan of NetBeans, you can configure it to work with PHPUnit by following these steps:
  • Go to NetBeans' configuration (Tools / Options)
  • Select PHP / Unit Testing
  • Check that the "PHPUnit Script" entry points to a valid PHPUnit executable. If it does not, NetBeans will tell you this, so if you don't see any red notices on the page, you are good to go. If not, look for the PHPUnit executable on your system and enter its path in the input field. For Linux systems, this path is typically /usr/bin/phpunit.
If you do not use an IDE with unit testing support, you can always run your test directly from the console:
Our team is tasked with the implementation of a "word wrap" feature.
Let's assume that we are part of a large corporation, which has a sophisticated application to develop and maintain. Our team is tasked with the implementation of a "word wrap" feature. Our clients don't wish to see horizontal scroll bars, and it's out job to comply.
In that case, we need to create a class that is capable of formatting an arbitrary bit of text provided as input. The result should be word wrapped at a specified number of characters. The rules of word wrapping should follow the behavior of other every-day applications, like text editors, web page text areas, etc. Our client does not understand all the rules of word wrapping, but they know they want it, and they know it should work in the same way that they've experienced in other apps.
TDD helps you achieve a better design, but it does not eliminate the need for up-front design and thinking.
One of the things that many programmers forget, after they start TDD, is to think and plan beforehand. TDD helps you achieve a better design most of the time, with less code and verified functionality, but it does not eliminate the need for up-front design and human thinking.
Every time you need to solve a problem, you should set aside time to think about it, to imagine a little design - nothing fancy - but enough to get you started. This part of the job also helps you to imagine and guess possible scenarios for the logic of the application.
Let's think about the basic rules for a word wrap feature. I suppose some un-wrapped text will be given to us. We will know the number of characters per line and we will want it to be wrapped. So, the first thing that comes to my mind is that, if the text has more characters than the number on one line, we should add a new line instead of the last space character that is still on the line.
Okay, that would sum up the behavior of the system, but it is much too complicated for any test. For example, what about when a single word is longer than the number of characters allowed on a line? Hmmm... this looks like an edge case; we can't replace a space with a new line since we have no spaces on that line. We should force wrap the word, effectively splitting it into two.
These ideas should be clear enough to the point that we can start programming. We'll need a project and a class. Let's call it Wrapper.


Popular posts from this blog

How to improve PHP web performance

Running PHP on Google App Engine

Importing data from CSV file into PHP applications