In Composer, you can define platform-specific packages based on the PHP version installed on the system. This allows you to require a different version of PHPUnit depending on the PHP version that Composer is running.

Here's how you can require a different version of PHPUnit based on the PHP version:

  1. Create a Composer JSON File: Start by creating a composer.json file in the root of your project (if it doesn't already exist).

  2. Define PHP Platform Requirements: In the composer.json file, use the platform property to specify PHP platform requirements. You can define different versions of packages based on the PHP version.

    For example, to require PHPUnit version 8 for PHP 7.2 and above, and version 7 for PHP 7.1 and below, your composer.json file would look like this:

    json
    { "require-dev": { "phpunit/phpunit": "7.*" }, "config": { "platform": { "php": "7.1.0" } } }

    In this example, PHPUnit version 7 will be required when the PHP version is less than 7.2.

  3. Require PHP Extensions (Optional): If your PHPUnit version requirements are more complex and depend on specific PHP extensions or features, you can specify those as well. For example:

    json
    { "require-dev": { "phpunit/phpunit": "8.*" }, "config": { "platform": { "php": "7.2.0", "ext-pdo": "7.2.0", "ext-mbstring": "7.2.0" } } }

    In this example, PHPUnit version 8 will be required when the PHP version is 7.2 or above and the ext-pdo and ext-mbstring extensions are available.

  4. Install Dependencies: After updating the composer.json file, run the following command to install or update the dependencies:

    bash
    composer update

    Composer will automatically handle the platform requirements and install the appropriate PHPUnit version based on the PHP version available.

With this setup, you can specify different PHPUnit versions based on the PHP version, allowing you to have a more flexible testing setup that adapts to the PHP environment.

Have questions or queries?
Get in Touch