How to run Docker PHP integration tests

How to run Docker PHP integration tests

In this guide we will show you how to run integration tests on a Docker container with a running PHP application.

Hint

Actions used in this guide:

Use case

Here we have a simple calculator application written in PHP: https://github.com/buddy-works/php-demo/tree/fib To calculate the value of the Fibonacci sequence, it uses a legacy app whose code is stored in the legacy directory.

In order to run Docker PHP integration tests, the legacy app must be available for the parent application. For that, we'll use a Docker container with Apache serving the legacy app. The tests will be run with PHPUnit and are defined in the tests directory.

1. PHP application in Docker

The legacy directory (https://github.com/buddy-works/php-demo/tree/fib/legacy) contains two files. One is index.php with the source code of the legacy app. The second one is the dockerfile required to build the Docker image:

default
FROM php:apache COPY . /var/www/html

The FROM section defines the official PHP image with a preinstalled Apache server. The second line means that the index.php file will be added to the /var/www/html folder during the image building process.

Hint
If you have no experience with Docker, check out this excellent guide that will walk you through the basics.

2. Test pipeline configuration

Begin with forking the repository with the application from GitHub. Next, add a new project in Buddy and select the repo from the list:

Image loading...Adding new project

Add a new pipeline and set the Trigger mode to automatic so that the tests will be run on every push:

Image loading...Pipeline configuration

The first step is building a Docker image that will be used to launch the container with our legacy app. Add the corresponding action and select the dockerfile and the context in which the image will be built:

Image loading...Build Docker image configuration

Hint
You can define the registry to which the image will be pushed in the Options tab. This is not necessary, however, as it won't be used anywhere besides Buddy.

With the Docker build step configured, it's time to automate the tests. Select the PHP action and define the commands that will download the dependencies and trigger the tests:

Image loading...Build Docker image configuration

Now we need to attach a custom service that will run the Docker container with the application from the previously built image:

Image loading...Adding custom service

Select the image built in the previous action as the template and enter the hostname required to run the tests:

Image loading...Custom service configuration

3. Hostname setup

The last thing to do is to set the URL to the legacy app. To do that, paste the hostname from the custom service to the config file of your application.

You can also store the location of the legacy app as an environment variable. To do that, switch to the Variables tab and add a new variable, e.g. LEGACY_URL, with the value of the hostname:

Image loading...Custom service configuration

Summary

With everything properly configured, Buddy will automatically Dockerize and test your PHP application with the latest changes from the repository. You can extend the pipeline however you like – for example, by adding a notification action at the bottom that will notify your team in case the tests have failed:

Image loading...Custom service configuration

See also

Jarek Dylewski

Jarek Dylewski

Customer Support

A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.