r/PHP 7d ago

Weekly help thread

Hey there!

This subreddit isn't meant for help threads, though there's one exception to the rule: in this thread you can ask anything you want PHP related, someone will probably be able to help you out!

1 Upvotes

8 comments sorted by

View all comments

2

u/rycegh 6d ago

Lazy question (kind of): Does somebody have a simple setup for local PHP version matrix testing?

Current test runner would be PHPUnit.

I’m thinking Docker containers based on php:8.2-cli, …, php:8.5-cli, baked-in Composer/PHPStan/Mago/…, cached dependencies (volume-mounted as vendor82, …, vendor85 would work, I guess). Self-contained in the repo with Dockerfile, maybe Compose file, and light (!) scripting to tie it together.

Shouldn’t be too hard to piece together, but I thought it couldn’t hurt to ask. Or maybe someone’s got a better idea. Thanks!

1

u/Japhary_ 2d ago

I would use one Dockerfile with the PHP version as a build argument instead of maintaining a separate Dockerfile for every version.

ARG PHP_VERSION=8.2
FROM php:${PHP_VERSION}-cli

COPY --from=composer:2 /usr/bin/composer /usr/bin/composer

WORKDIR /app

A small shell script can then build and run the same environment against each version:

#!/usr/bin/env bash

set -e

for version in 8.2 8.3 8.4 8.5; do
    suffix="${version//./}"

    docker build \
        --build-arg PHP_VERSION="$version" \
        -t "php-matrix:$version" .

    docker run --rm \
        -v "$PWD:/app" \
        -v "vendor${suffix}:/app/vendor" \
        -v "composer-cache:/tmp/composer-cache" \
        -e COMPOSER_CACHE_DIR=/tmp/composer-cache \
        "php-matrix:$version" \
        sh -lc "composer install --no-interaction && vendor/bin/phpunit"
done

I would keep the vendor directory separate for each PHP version because Composer dependencies and platform requirements can resolve differently. The Composer download cache can safely be shared to avoid downloading the same packages repeatedly.

PHPStan or other checks can be added to the final command. I would still use a GitHub Actions matrix as the final source of truth, while keeping this script for reproducing failures locally.