mulertech / docker-tests
The MulerTech Docker-tests package
Requires
- php: >=8.0
README
The MulerTech Docker-tests package allows you to configure and run PHP tests in a Docker environment.
Description
This package configures a Docker container to run PHP tests using PHPUnit. It creates a .env.test file with the necessary information to build the Docker image and run the tests.
Prerequisites
- Docker
- PHP
- Composer
Installation
-
Include the package as a dev dependency with Composer :
composer require-dev mulertech/docker-tests
-
Run the following command to install the package :
composer install
Usage
Running the tests
To run the tests, use the following command:
./vendor/bin/mtdocker test
To run the tests with code coverage, use the following command:
./vendor/bin/mtdocker test-coverage
The code coverage report will be generated in the ./.phpunit.cache/coverage
folder.
These commands will:
- Check if the Docker container is running.
- If the container is not running, it will be started.
- Run the PHPUnit tests in the container.
- Stop the container if it was not running before the tests were executed.
Running phpstan
To run phpstan, use the following command:
./vendor/bin/mtdocker phpstan
This command will:
- Check if the Docker container is running.
- If the container is not running, it will be started.
- Run phpstan in the container.
- Stop the container if it was not running before phpstan was executed.
Running php-cs-fixer
To run php-cs-fixer, use the following command:
./vendor/bin/mtdocker php-cs-fixer
This command will:
- Check if the Docker container is running.
- If the container is not running, it will be started.
- Run php-cs-fixer in the container.
- Stop the container if it was not running before php-cs-fixer was executed.
Running php-cs-fixer, phpunit and phpstan
To run php-cs-fixer, phpunit and phpstan, use the following command:
./vendor/bin/mtdocker all
This command will:
- Check if the Docker container is running.
- If the container is not running, it will be started.
- Run php-cs-fixer, phpunit and phpstan in the container.
- Stop the container if it was not running before the checks were executed.
Starting the Docker container
To start the Docker container, use the following command :
./vendor/bin/mtdocker up
To start the Docker container in detached mode, use the following command :
./vendor/bin/mtdocker up -d
Stopping the Docker container
To stop the Docker container, use the following command :
./vendor/bin/mtdocker down
Configuring the Docker container into PHPStorm
To configure the Docker container into PHPStorm, follow these steps:
- Open the PHPStorm settings.
- Go to
PHP
. - Click on the
...
button next to theCLI Interpreter
field. - Click on the
+
button and selectFrom Docker, Vagrant, VM, WSL, Remote...
. - Configure the remote PHP interpreter as follows:
- Select
Docker Compose
. - Set the server to
Docker
or click onNew...
if this server does not exist and :- Set the name to
Docker
. - Select
Docker for Windows
orUnix socket
depending on your system. - Click on
OK
.
- Set the name to
- Set the configuration files to
./mt-compose.yml
. - Set the service to
php
. - Set the Environment variables to
COMPOSE_PROJECT_NAME=php
. - Click on
OK
.
- Select
- Click on
OK
to save the configuration.
To configure PHPUnit, follow these steps:
- Go to
PHP
>Test Frameworks
. - Click on the
+
button and selectPHPUnit by Remote Interpreter
. - Set the interpreter to
php
and click onOK
. - Set the path to script to
/var/www/html/vendor/autoload.php
. - Click on
OK
to save the configuration.
Creating the mt-compose.yml
file
The mt-compose.yml
file is created automatically when the container starts. It contains the following information :
services: php: build: context: . dockerfile: "./vendor/mulertech/docker-tests/Dockerfile" args: USER_ID: $uid GROUP_ID: $gid PHP_IMAGE: "$image" container_name: "$containerName" volumes: - "./:/var/www/html" environment: PHP_CS_FIXER_IGNORE_ENV: 1
The USER_ID
and GROUP_ID
are used to set the user and group of the current user in the Docker container.
This is done to avoid permission issues when running the tests and to create files or folders (if needed) with the correct permissions.
The PHP_IMAGE
is php:<php version>-fpm-alpine
, the php version is set from the required version in the composer.json
file.
The container_name
is set to mt-docker-<package name>-<php version>
.
Adding the database to mt-compose.yml
If your project requires a database (the composer.json file contains ext-pdo), the mt-compose.yml file will be automatically updated to include a database service. Here is an example configuration for MySQL:
services: php: build: context: . dockerfile: "./vendor/mulertech/docker-tests/Dockerfile" args: USER_ID: $uid GROUP_ID: $gid PHP_IMAGE: "$image" container_name: "$containerName" volumes: - "./:/var/www/html" environment: PHP_CS_FIXER_IGNORE_ENV: 1 DATABASE_HOST: db DATABASE_PORT: "3306" DATABASE_PATH: "/db" DATABASE_SCHEME: "mysql" DATABASE_QUERY: "serverVersion=5.7" DATABASE_USER: "user" DATABASE_PASS: "password" links: - db networks: - default db: image: "mysql:8" environment: MYSQL_DATABASE: db MYSQL_USER: user MYSQL_PASSWORD: password MYSQL_ROOT_PASSWORD: root networks: - default
This configuration adds a db service using the mysql:8 image and sets the necessary environment variables to connect to the database from the PHP container.