Bitbucket Pipelines, PHPUnit, PHPCS and CodeCeption
Mar 2017Background information
For a while now I’ve been thinking about starting a new project, and as with most such cases I’ve been thinking that I need to do things better and more proper this time around.
One of those things has always been the automated testing parts of the project that have fallen behind or lacking in many ways.
So I set a small private repository on Bitbucket to see what my options are, what I could work with, etc. and started looking into the various tools like Jenkins, Travis, and CircleCI.
But I quickly realised that this would become either too complicated or too expensive, especially if I wanted to keep the project private (for now anyway).
Luckily though I noticed a new feature in Bitbucket called Pipelines which seemed to fit my needs perfectly.
Pipelines Setup
Without getting too much in detail, here are the two most important parts for setting up this automated testing pipeline.
My composer.json
file for this simple project looks something like:
{
"name": "rihards-steinbergs/ci-email",
"description": "Pipeline CI test repository",
"autoload": {
"psr-4": {
"Rihards\\": "src/"
}
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "5.*",
"squizlabs/php_codesniffer": "2.*",
"codeception/codeception": "2.*"
}
}
And here’s my current bitbucket-pipelines.yml
configuration file:
image: phpunit/phpunit:5.7.12
pipelines:
default:
- step:
script:
- composer install --prefer-source --no-interaction --dev
- vendor/bin/phpcs --standard=PSR2 src/
- vendor/bin/phpunit
- php -S localhost:8001 --docroot web &>/dev/null&
- vendor/bin/codecept run
Step by step
So what exactly does this do? Well, everytime I push new code the following tests will be ran:
- PHP Coding Standards according to PSR-2
- Unit tests that I’ve written with PHPUnit
- Acceptance tests that have been written with CodeCeption
What does it look like?
Lessons learnt
- Current version of CodeCeption doesn’t (yet) support PHPUnit 6, so roll with 5.x
- It’s super easy (and fun) to write unit and acceptance tests
- Pipelines integrates greatly into other Bitbucket features (like pull requests)
- All of this was much easier to achieve than expected
Future additions
There are a couple of things that I’d like to add to my pipeline before I get started on my project still.
Namely, first figure out how to and add syntax checking and linting for JS and (S)CSS files, and of course add database support so that I can build the whole project.