PHPSpec
Home | Manual | Download | Mailing List | Source
PHPSpec is a Behaviour-Driven Development (BDD) framework for PHP
PHPSpec allows you to write executable examples reflecting specifications of the desired behaviour of the source code being described.
PHPSpec is a simple framework. As the description suggests you can write code examples which are repeatable. This means you write an example, and may repeat it as often as you wish to ensure the implementation code it relates to continues to abide by the example. PHPSpec is related to Unit Testing, but our Behaviour-Driven Development (BDD) origins determine we use a clear plain English style API. This API, given it's fluent style and approximation of natural language is therefore referred to as a Domain Specific Language (DSL). At a deeper level, PHPSpec was designed entirely with BDD in mind. Though similar to Unit Testing, the framework is being designed to support BDD from the ground so that learning, understanding and practicing BDD is as easy as possible.
Spec Framework
|
Start with a very simple example expressing one desired behaviour. require_once 'Bowling.php';
class DescribeNewBowlingGame extends PHPSpec_Context
{
private $_bowling = null;
public function before()
{
$this->_bowling = new Bowling;
}
public function itShouldScore0ForGutterGame()
{
// allow 20 throws
for ($i=1; $i<=20; $i++) {
$this->_bowling->hit(0);
}
$this->spec($this->_bowling->score)->should->equal(0);
}
}
Run the example and watch it fail. $ phpspec BowlingSpec F Finished in 0.008634 seconds 1 example, 1 failure |
Write just enough code so that the example will pass. class Bowling
{
public $score = 0;
public function hit()
{
}
}
Rerun the example, and bask in the glow of success. $ phpspec -r --specdocs . Bowling - should score 0 for gutter game Finished in 0.007534 seconds 1 example, 0 failures |
Every passing example is another small step
Take small steps. We've implemented our first example so to write more code, we should first write more examples demonstrating how any new code should behave. This is encouraged by all spec methods, which must start with the term "itShould". Perhaps the next step is hitting one or more pins - that would allow us to predict a score for an example, and also lead us to implementing some scoring rules for our new Bowling::hit() method.
Get started now!
Download and install using the PEAR Installer:
$ pear channel-discover pear.phpspec.org $ pear install phpspec/PHPSpec
Or download manually from http://pear.phpspec.org/get.
Finding Help
Your first port of call should be the PHPSpec Manual which is available in English and Japanese. We also maintain a mailing list for general help and development chat which is managed by Google Groups at phpspec-dev