DEV Community

Cover image for Testing Laravel Packages Locally with Docker
Patrick Organ
Patrick Organ

Posted on

Testing Laravel Packages Locally with Docker

Recently I've been helping the team over at Spatie to upgrade their numerous repositories with support for the new version of PHP, 8.2. Their test workflows run tests using the composer options --prefer-lowest and --prefer-stable, which install different versions of dependencies; this can sometimes cause some tests to fail. Throw in support for numerous versions of PHP and Laravel, and you have a recipe for problems during test runs.

To avoid failing tests on my Pull Requests, I wanted to run them locally, but installing and running the tests took a long time and was problematic - it'd require having every version of PHP installed, for instance.

This led me to the act utility - available here - which uses docker containers to allow you to run GitHub workflows locally.

After fumbling with various docker images, I came across one created by the author of the setup-php action, which was designed to allow PHP workflows to run with act .I quickly discovered that it could have been more optimal for Laravel workflows, which require many PHP extensions. The images worked but took a long time to run as they had to download and install several of these extensions.

Eventually, I settled on forking the original repository to create an optimized version.

permafrost-dev/laravel-act-docker comes with PHP versions 7.2 through 8.2 and has most PHP extensions installed by default, which translates into a short setup time for PHP. It is meant to run Github workflows for Laravel applications and packages locally using act.

Quickly using the image with act is as easy as creating an alias for your shell by adding the following to your ~/.zshrc or ~/.bashrc file:

alias actphp="act -P ubuntu-latest=permafrostsoftware/laravel-node:latest-amd64"
Enter fullscreen mode Exit fullscreen mode

Once that's done, you can run your Laravel-based workflows locally using actphp and the -W flag:

actphp -W ./github/workflows/run-tests.yml
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

This docker image is a work in progress and can be further optimized for Laravel - any comments or contributions are welcome.

Top comments (0)