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.
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 COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
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 using composer.
Create an image from the Dockerfile you created earlier.
docker build . -t php:composer
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
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"
}
}
Prepare the following code to see if it actually works
<?php
require('vendor/autoload.php');
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
output
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)
root@4b1af08e1cd8:/var/www#
Recommended Posts