6.2. The HTTP Runner

The HTTP Runner is not an immediately available mode. Instead you need to do a small amount of setup within a PHP file which you intend calling from a web browser. This is actually a really simple task, and the available options for utilising a HTTP runner are identical to the options available for the Console Runner as outlined above.

Here's a simple example of a HTTP Runner stored to a file called AllSpecs.php.

<?php

require_once 'PHPSpec.php';

$options = new stdClass;
$options->recursive = true;
$options->specdocs = true;
$options->reporter = 'html';

PHPSpec_Runner::run($options);

The PHPSpec_Runner class is actually used internally by the default Console Runner so what we're doing here is pretty simple. All we're really doing is duplicating the internal work the Console Runner performs within a PHP file we can visit from a web browser.

First of all, we include the base PHPSpec.php file. Since PHPSpec takes advantage of PHP5 autoloading, there's no need to include any other PHPSpec files. Secondly, we setup the desired options in a stdClass (a PHP5 standard class) as object properties - optionally you can also use a plain array since it will just be converted to a standard object internally anyway.

The options are typical for a complete execution of all specs. PHPSpec will recursively search the current directory and all child directories for specs to execute, it will output specdoc formatted specifications in plain text along with the results, and it will use the HTML reporter to output HTML.

Finally we call PHPSpec_Runner's static run() method. And that's all there is to it! If you only want to execute a sub-directory of your specs, you can copy a similar file to that directory (recursive searching does not traverse parent directories).