Auto Complete with PHPSpec

Unit testing with PHPSpec is GREAT! Unfortunately it is missing some nice to haves

What is PHPSpec, from their documentation:

phpspec is a tool which can help you write clean and working PHP code using behaviour driven development or BDD. BDD is a technique derived from test-first development.

BDD is a technique used at story level and spec level. phpspec is a tool for use at the spec level or SpecBDD. The technique is to first use a tool like phpspec to describe the behaviour of an object you are about to write. Next you write just enough code to meet that specification and finally you refactor this code.

This is not an introduction to PHPSpec, this assumes you already know and use PHPSpec. When using PHPSpec you will notice that because of the magic the Class Under Test (CUT) does not auto complete. This is easily fixed with using PHPStorm's Mixin Annotation (@mixin \Class\Name).

Doing this will turn this screenshot:

Into this:

Thus giving the usual IDE intellisense / code completion on the CUT with PHPSpec.

Note: using PHPStorm v8, PHPSpec v2

Reference: Information discovered by @schrotty at @PHPUCEU


You will notice that the PHPSpec methods are not auto completing.

There is yet to be a clean solution to this.

The best solution to date for this is to put a mixin on the CUT (class under test) to a template class which contains the PHPSpec methods. Hopefully a better solution will present itself in the near future.

Simple example (namespaces removed for simplicity):

/**
 * Class Dashboard
 *
 * @mixin Template
 */
class Dashboard
{
    /**
     * @var int
     */
    protected $id;
    // ...
}
// PHPSpec template
class Template
{
    /**
     * @param string $type
     */
    public function shouldHaveType($type) {}
    // ...
}

This is still not ideal, because now your Source Code Classes have auto complete to the PHPSpec template - so now you have it in your Specs but also in your Source Code...the world is not perfect.


Share this on →