aklump/phpswap

0.0.8 2024-08-15 16:31 UTC

This package is auto-updated.

Last update: 2024-11-15 17:05:16 UTC


README

Summary

Provides a means to easily execute code with PHP versions other than the default. This was first built to run PhpUnit tests within Composer projects across multiple PHP versions. See example below.

Quick Start

This simple code example should give you an idea of how this works.

mkdir foo
cd foo
composer init
composer require aklump/phpswap
php -v
./vendor/bin/phpswap use 5.6 "php -v; echo"
./vendor/bin/phpswap use 8.1 "php -v; echo"

What It Does

  • Temporarily modifies $PATH with a different PHP version binary.
  • If composer.json is present, runs composer update so that dependencies appropriate for the swapped PHP version get installed.
  • Runs the given executable, which can be a command or a script path.
  • Lastly, if necessary, runs composer update with the original PHP to restore the Composer dependencies.

What PHP Versions Are Supported?

To see the available versions, which will echo those versions provided by MAMP you can use the show command.

./vendor/bin/phpswap show

Dependencies

Getting Started

  1. Ensure you have MAMP installed.
  2. Download all PHP versions using MAMP that you hope to swap.
  3. composer require aklump/phpswap in your project.
  4. Use vendor/bin/phpswap show to see what versions are available.
  5. ./phpswap list to see all available commands.

Examples with PhpUnit

Here is a pattern you can use to run PhpUnit under PHP 7.1, 7.4 and 8.1.

  • Given you have installed phpunit in your project with Composer
  • And you run your tests using ./vendor/bin/phpunit -c phpunit.xml
  • Then you can implement PhpSwap in the following way:
  • See also Controller File Example further down.
./vendor/bin/phpswap use 7.1 './vendor/bin/phpunit -c phpunit.xml'
./vendor/bin/phpswap use 7.4 './vendor/bin/phpunit -c phpunit.xml'
./vendor/bin/phpswap use 8.1 './vendor/bin/phpunit -c phpunit.xml'

CLI Options

-v

In verbose mode you will see the Composer output.

--working-dir

This sets the working directory from which your script is called. This is optional.

Troubleshooting

During execution, a file called composer.lock.phpswap is temporarily created in your project. It contains a copy of the composer.lock file that was in your project before the swap. This file is used to refresh composer.lock at the end of a swap. In some error situations this file may not be deleted. Use the snippet below to recover.

You may also see "Composer detected issues in your platform:" after a swap executed. The same applies here, try the snippet below.

mv composer.lock.phpswap composer.lock;composer update

Controller File Example

Here is a complete snippet for controlling tests. Save as bin/run_unit_tests.sh and call it like this: bin/run_unit_tests.sh -v. You may leave off the verbose -v flag unless troubleshooting.

#!/usr/bin/env bash
s="${BASH_SOURCE[0]}";[[ "$s" ]] || s="${(%):-%N}";while [ -h "$s" ];do d="$(cd -P "$(dirname "$s")" && pwd)";s="$(readlink "$s")";[[ $s != /* ]] && s="$d/$s";done;__DIR__=$(cd -P "$(dirname "$s")" && pwd)

cd "$__DIR__/.."

verbose=''
if [[ "${*}" == *'-v'* ]]; then
  verbose='-v'
fi
./vendor/bin/phpswap use 7.3 $verbose './vendor/bin/phpunit -c tests_unit/phpunit.xml'
./vendor/bin/phpswap use 7.4 $verbose './vendor/bin/phpunit -c tests_unit/phpunit.xml'
./vendor/bin/phpswap use 8.0 $verbose './vendor/bin/phpunit -c tests_unit/phpunit.xml'
./vendor/bin/phpswap use 8.1 $verbose './vendor/bin/phpunit -c tests_unit/phpunit.xml'
./vendor/bin/phpswap use 8.2 $verbose './vendor/bin/phpunit -c tests_unit/phpunit.xml'