Run PHPUnit on multiple PHP versions using Dockerdocker phpunit php
One of the biggest advantage using Docker is the ability to switch between tools versions. Personally I think the easiest way to demonstrate how you can benefit from this advantage is to run PHPUnit tests against different PHP versions.
Prepare the application
For this example I will use a package build on top of Respect Validation validator called flex-validator which offers a simpler interface to validate multiple inputs together and return all the errors at once.
We will clone the repository, install all it’s dependencies and finally run all it’s tests on 7.0 and 7.2 versions of PHP.
Clone the repo
$ git clone email@example.com:iulyanp/flex-validator.git
Install the dependencies using Docker
$ docker run --rm -v $(pwd):/var/www composer install
Now it’s time to run the tests on PHP 7.0 version.
$ docker run --rm -v $(pwd):/var/www -w /var/www php:7.0 vendor/bin/phpunit PHPUnit 6.5.6 by Sebastian Bergmann and contributors. ..................... 21 / 21 (100%) Time: 624 ms, Memory: 4.00MB OK (21 tests, 46 assertions)
- downloaded the
- started a container from that image
- mapped the current folder into the
- made the
/var/wwwdirectory the current one in the container
- run the
vendor/bin/phpunitcommand inside the
- after tests executed the container closed and removed itself
Or you can run it on PHP 7.2 version to see everything will work nicely with the latest PHP version.
$ docker run --rm -v $(pwd):/var/www -w /var/www php:7.2 vendor/bin/phpunit PHPUnit 6.5.6 by Sebastian Bergmann and contributors. ..................... 21 / 21 (100%) Time: 751 ms, Memory: 4.00MB OK (21 tests, 46 assertions)
As you can see this is a very clean way to test our application or package on different PHP versions without installing them locally.