PHPSpec With Composer: The Short And Simple Story

We wanted to do something more with composer and so we dug further and found composer allows us to really simplify setup of our phpspec projects.
For our main project we create a composer.json on the root folder of our project as follows:

    "config": { "bin-dir": "." },
    "require": {
        "php": ">=5.3.2",
        "cordoval/phpspec": "master"

What this will do is to install all dependencies so we can work with phpspec and its dependencies and also have a standalone script with the phpspec version of our like. In this case we are using the only version existent at the time of writing which is `master`. The script linking magic is accomplished by the composer.json in phpspec as shown here:

    "name": "cordoval/phpspec",
    "description": "PHPSpec: The BDD Specification Library",
    "keywords": ["bdd", "spec", "test"],
    "homepage": "",
    "license": "MIT",
    "authors": [
            "name": "PHPSpec Team",
            "homepage": ""
    "bin": ["scripts/phpspec-composer.php"],
    "autoload": {
        "psr-0": { "PHPSpec\\": "src/" }
    "require": {
        "php": ">=5.3.2",
        "cordoval/mockery": "master-dev"

Of course you don’t need to write this anymore as it is taken care by us.

Instructions for installing the composer are easy. You can either create the composer.json on your personal project or clone this sample project.

git clone git://
cd phpspec-composer

The only thing besides writing the short composer.json above is to install composer. It is simple:

php composer.phar install

If you have composer installed as in a development setup like in a previous post you just do `composer install`.
The last command will install phpspec and all its dependencies included in the phpspec composer.json package specification, that is to say: Mockery.
Now run phpspec with:

~ ./phpspec-composer.php -h
Usage: phpspec (FILE|DIRECTORY) + [options]
    -b, --backtrace          Enable full backtrace
    -c, --colour, --color    Enable color in the output
    -e, --example STRING     Run examples whose full nested names include STRING
    -f, --formater FORMATTER Choose a formatter
                              [p]rogress (default - dots)
                              [d]ocumentation (group and example names)
                              custom formatter class name
    --bootstrap FILENAME     Specify a bootstrap file to run before the tests
    -h, --help               You're looking at it
    --fail-fast              Abort the run on first failure.
    --version                Show version

I thank the Lord Jesus Christ. And the opportunity to have received much help through helpful top guys like Stof, Seldaek, igorw and the developers of composer.

One thought on “PHPSpec With Composer: The Short And Simple Story

  1. Hi Luis,

    This was a great tutorial and helped me a lot to setup my composer for my projects, however there was one little thing missing, that I know it should be obvious but didn’t notice until I got an exception , Composer\DependencyResolver\SolverProblemsException]        
     [Job(cmd=install, target=raul782/phpspec, packages=[]), ]

    The solution was simple, I had to register with an account in Packagist if I wanted to use my own forked phpspec from Github, when I used yours there was no problem, I guess this is more a customization problem than a problem with the tutorial, but could be useful for people using their own forked projects.

Leave a Reply

Your email address will not be published. Required fields are marked *