In this guide we will show you how to run integration tests on a Docker container with a running PHP application.
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
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
1. PHP application in Docker
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
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
Add a new pipeline and set the trigger mode to automatic so that the tests will be run on every push:
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:
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:
Now we need to attach a custom service that will run the Docker container with the application from the previously built image:
Select the image built in the previous action as the template and enter the hostname required to run the tests:
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:
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: