Run phpunit on Docker

There is phpunit in the php testing framework. Since the test environment does not depend on the development machine and I want to make it easy to build, I tried to prepare it using docker. This time, I created an execution environment for phpunit using Dockerfile and composer.

Install composer with Dockerfile

Since phpunit is installed using composer, first install composer in docker container. To install it, use composer officially written multi-stage builds.

Contents of Dockerfile

FROM php:7.2-cli

ENV PATH $PATH:/composer/vendor/bin
#Install composer
COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN apt-get update -yqq \
    && apt-get install git zlib1g-dev libsqlite3-dev -y \
    && docker-php-ext-install zip \
    && docker-php-ext-install pdo_mysql

WORKDIR /var/www

Install phpunit with composer

Install phpunit using composer.

Create docker image

Create an image from the Dockerfile you created earlier.

docker build . -t php:composer

Mount the host directory and start the container

The reason for mounting and booting the host directory is to allow the code and test code you create to run inside the container.

docker run -it --rm -v $(pwd):/var/www php:composer /bin/bash

install phpunit

Let's install phpunit. If you have composer.json, install it with composer install.

composer require --dev phpunit/phpunit

When using composer.json, describe the following contents and execute composer install

    "require-dev": {
        "phpunit/phpunit": "^8.4"

Run phpunit

Prepare the following code to see if it actually works

use PHPUnit\Framework\TestCase;
class UserTest extends TestCase {
    public function testExample() {
        $expected = 'hoge';
        $this->assertEquals($expected, 'hoge');

Run the test from the command below

vendor/bin/phpunit test.php


root@4b1af08e1cd8:/var/www# vendor/bin/phpunit test.php 
PHPUnit 8.4.3 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 257 ms, Memory: 4.00 MB

OK (1 test, 1 assertion)

Recommended Posts

Run phpunit on Docker
Run VS Code on Docker
Run openvpn on Docker (windows)
Run SSE (Server-Sent-Event) samples on docker
Steps to run docker on Mac
Run puppeteer-core on Heroku (Docker edition)
Run React on a Docker container
Run the AWS CLI on Docker
How to run JavaFX on Docker
Run GUI application on Docker container
Run PureScript on a Docker container
Liberty on Docker
Redmine on Docker
Run NordVPN on Docker (Windows) Ubuntu container
Run Embulk on Docker to convert files
Until you run apache on ubuntu on docker
Run GUI application on Docker container (Japanese input)
Run STS4 on Mac
Docker installation on CentOS 6
python notes on docker
Test run on rails
Run Pico with docker
Install Docker on Manjaro
Run JSP Hello World with Tomcat on Docker
Run Payara with Docker
Run Processing on Ant
Run tiscamera on Ubuntu 18.04
M.S. docker on Windows
Run the Android emulator on Docker using Android Emulator Container Scripts
What is docker run -it?
Install Docker on Windows 10 PRO
Run TAO Core with Docker
Install Docker on Ubuntu Server 20.04
Oracle Java 8 on Docker Ubuntu
Run chromium-mir-kiosk on Ubuntu Core
Run Rails whenever with docker
Decomposing the Docker run command. .. ..
Run java applet on ubuntu
Hosted Apicurio Studio on Docker
Until you run Quarkus and run docker image on Amazon ECS
Run lilypond on Docker on macOS Catalina to create sheet music
Run Java EE applications on CICS
Install openjdk8 on Docker image (Debian)
Install Docker on AWS Ubunt 20.04 LTS
Run lambda with custom docker image
Run LibreOffice Basic macros in Docker
Until you put Ubuntu 20 on Windows 10 Home and WSL2 and run Docker
Run SQL Server with Docker ToolBox
[Microsoft] Run Azure Pipelines (VSTS) Agent on Docker or Azure Container Instance
Try using Redmine on Mac docker
JMeter Client/Server Remote Testing on Docker
To beginners launching Docker on AWS
Docker on NILFS2 filesystem (for raspbianOS)
WordPress with Docker Compose on CentOS 8
Build Clang x VSCode on Docker (1)
Deploy Flask's Docker image on Heroku
Use Docker on your M1 Mac
Rails on Docker environment construction procedure
Run tomcat shell script on java8
Try Docker on Windows Home (September 2020)
Make JupyterLab run anywhere with docker