Cees-Jan

Smoke testing ReactPHP applications with Cigar

Last week I came across Cigar, a smoke testing tool by Matt Brunt. Which, to me, is great stepping stone for my personal projects/sites to integration tests. In this post we not only go into Cigar, but also how to start your HTTP ReactPHP application, run cigar against it, and shut it down again. (Note that it doesn't have to be a ReactPHP application it can also be a NodeJS app, or PHP's build in webserver you use for testing.)

Setting up Cigar

First we need to install Cigar:

composer require brunty/cigar --dev

Secondly we create .cigar.json which contains a list of URL's, their expected status code, and optionally a chunk of the expected content. The list shown below is from one of my older sites/projects wow-screenshots.net I've converted to fully run on ReactPHP the past month:

[
  {
    "url": "http://localhost:20080/",
    "status": 200
  },
  {
    "url": "http://localhost:20080/404",
    "status": 404
  },
  {
    "url": "http://localhost:20080/favicon.ico",
    "status": 200
  },
  {
    "url": "http://localhost:20080/robots.txt",
    "status": 200,
    "content": "User-agent"
  }
]

You might have noticed that we call the server on a very specific local IP and port combination, this is the address the server runs at on development and CI. Now normally we would start our server run ./vendor/bin/cigar and get results:

Cigar output

All in one script

But we want to do all of that in one step. So we're going to craft a bash script that, in-order, starts the ReactPHP application, runs cigar, and then kills the ReactPHP application. Most of that is straight forward but we need to capture the PID (short for process identifier) so we can kill it later. But first we need a skeleton for our cigar.ash bash script.

#!/bin/bash

function main {
}

main

1) We add the first line to main() that will start our server and launch it into the background using &. web.php is a simple HTTP server like this ReactPHP HTTP Server example:

php ./server.php &

2) Next we add the following line which will capture the PID of our server:

local pid=$!

3) Optionally we'll wait for a few seconds to give the server time to start:

sleep 5

4) Now for the main attraction of this script, we run Cigar:

./vendor/bin/cigar

5) As bonus we capture Cigar's exit code and store it for later use:

local ec=$?

6) We shut down the application, rather aggressively using -9 which kills it without giving it the change to clean up:

kill -9 $pid

7) And finally we re-emit the Cigar exit code:

exit $ec

The end result

All of the above combined is the cigar.ash script I run do my smoke testing:

#!/bin/bash

function main {
    php ./server.php &
    local pid=$!
    sleep 5
    ./vendor/bin/cigar
    local ec=$?
    kill -9 $pid
    exit $ec
}

main

Categories: PHP - Bash - ReactPHP - ReactPHP SeriesTags: ReactPHP - PHP - Bash