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

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 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:

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.

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:3

Adding new project
Adding new project

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

Pipeline configuration
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:

Build Docker image configuration
Build Docker image configuration

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:

Build Docker image configuration
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:

Adding custom service
Adding custom service

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

Custom service configuration
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:

Custom service configuration
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:

Custom service configuration
Custom service configuration

See also