Setting up Bamboo and PHPSPEC

Setting up PHPSPEC tests on Atlassian Bamboo can be a real pain; it's fundamentally simple, but it's so easy to kludge and the build process logs aren't that great.  This guide will help you set things up from A to Z.  

Setting up your Stages

A single stage should do.  Click on the Create Stage button, give it a name, and save.

Stages tab, in the Plan Configuration area.

Stages tab, in the Plan Configuration area.

With your Stage created, access the Tasks tab, and configure your Source Code Checkout, defining the repository it will download.  It will draw a list of repositories from any source control tool you've linked.  [If you haven't yet done this, do it before continuing]  

Next, we configure our tasks.

The Composer Build Task

  1. Click on 'Add Task'
  2. Find the 'Script' task and click 'Add'
  3. In its configuration window, give Task Description a name of 'Composer Build'
  4. Interpreter will be Shell
  5. Script Location will be Inline
  6. Your Script Body will contain:
/usr/local/bin/composer update 1>&2

Leave the remaining details blank, and click save.

 

The PHPSPEC Task

If your developers are like mine, they run their spec tests in pretty mode, but we'll need the output as junit would expect.  Configure a script task as you previously did, changing the name to be something meaningful like Run PHPSpec, yet selecting an Interpreter of /bin/sh and use this Script body to sub pretty for junit:

#!/bin/bash
sed -i -e 's/formatter.name: pretty/formatter.name: junit/g' phpspec.yml
vendor/bin/phpspec run > build/junit.xml 2>/dev/null
exit 0

The remaining fields are left blank, and you click Save.  We do things this way, because phpspec will not exit with 0 if there are errors, and this is what Bamboo considers a success.  This'll let phpspec finish, and subsequent tasks will run (such as the one where your JUNIT results are evaluated).

The JUnit Task

Lastly, you'll need to configure the test task.

  1. Click on Add Task and select the JUNIT type
  2. Give it a Task Description of Evaluate Test Results
  3. Specify custom results directories should read build/junit.xml
  4. Click Save

Storing ARtifacts

Still on the same screen, click on the Artifacts tab up top.  Then, click on the Create definition button in the left hand corner of this tab.

  1. Location should read build
  2. Copy pattern should read *.xml
  3. The Shared checkbox remains unchecked
  4. Click Save

Clover Coverage

Done with Artifacts, click on the Miscellaneous tab, and check the User Clover to collect Code Coverage for this build checkbox.  A section will expand, in which you select Clover is already integrated into this build and a clover.xml file will be produced.  Lastly, specify Clover XML Location to be build/coverage.xml.

 

Putting it all together

The rest of the magic is actually done in your code's phpspec.yml configuration, and in your project's composer.json

Your phpspec configuration would typically look like this

suites:
    Lemonade:
        namespace: Lemonade
        spec_prefix: Spec
        src_path: module/Lemonade/src/
        spec_path: module/Lemonade/bundle
#
#  .... the rest of your suites are here
#
formatter.name: pretty
extensions:
  PhpSpecCodeCoverage\CodeCoverageExtension:
    format:
      - html
      - clover
    output:
      html: build/coverage
      clover: build/coverage.xml
    whitelist:
     - module

Note the inclusion of the PhpSpecCodeCoverage\CodeCoverageExtension that's outputting into our build folder, that's keeping all .xml artifacts in Bamboo.  Install that extension through composer, by installing henrikbjorn/phpspec-code-coverage