This Docker image watches files then runs PHPUnit tests when those files change. Files are watched with entr, then tests are run with PHPUnit built on the the PHP cli base image.
Create a simple Docker Compose service:
services:
test:
image: ideasonpurpose/phpunit-watch:2.0.0
volumes:
- ./:/app
Then run tests with docker compose run test
or watch for changes with docker compose run test watch
.
Add the following two scripts to package.json so the tests can be called with npm run test
and npm run test:watch
:
{
"scripts": {
"test": "docker compose run --rm test",
"test:watch": "docker compose run --rm test watch"
}
}
The default working directory is /app
, mount your project's test files there. If a project needs to mount a deeper tree, redefine working_dir
to the test root directory.
Source file chnages in the following directories will re-run tests: lib
,src
,test
and tests
.
The Docker command to directly run this image looks like this:
docker run --rm -v "${PWD}:/app" ideasonpurpose/phpunit-watch:dev watch
To see coverage in VSCode with the Coverage Gutters extension, add this to settings.json to remap the docker paths:
{
"coverage-gutters.remotePathResolve": ["/app/", "./"]
}
For projects set up on an earlier version of PHPUnit, it will report a deprecated schema error like this:
- Your XML configuration validates against a deprecated schema. Migrate your XML configuration using "--migrate-configuration"!
To upgrade the project's phpunit.xml file, run docker compose run --rm test phpunit --migrate-configuration
.
To iterate on this project locally, build the image using the same name as the Docker Hub remote. Docker will use the local copy. Specify dev
if you're using using versions.
docker build . --tag ideasonpurpose/phpunit-watch:dev
To update the included version of PHPUnit, update the phpunit-version.json with the latest release version from https://phar.phpunit.de/, then:
npm run bump
to update the Dockerfile and Readme- Commit the version bump and any other changes
npm version <patch|minor|major>
- Push to GitHub
A GitHub Action triggered by version-tagged commits will build and deploy to Docker Hub.
The GitHub Actions for this project require both a DockerHub Access Token and the account password. This is due to Docker Hub not yet supporting Access Tokens for the description API (see peter-evans/dockerhub-description#10).
This project is actively developed and used in production at Ideas On Purpose.