From 5f85d448cfcc8b7b2f49c0d7f4228cc7166d70f2 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 15 Mar 2023 16:17:07 -0700 Subject: [PATCH] PHPUnit - Allow env-var to specify version (for tools/scripts/phpunit) --- tools/scripts/phpunit | 87 ++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 39 deletions(-) diff --git a/tools/scripts/phpunit b/tools/scripts/phpunit index e941348381..76010ddc35 100755 --- a/tools/scripts/phpunit +++ b/tools/scripts/phpunit @@ -15,46 +15,34 @@ $argFilters = []; if (PHP_SAPI !== 'cli') { die("phpunit can only be run from command line."); } -if (version_compare(PHP_VERSION, '7.2', '>=')) { - $phpunit = findCommand('phpunit8'); - $argFilters[] = function ($argv) { - $pos = array_search('--tap', $argv); - if ($pos !== FALSE) { - array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']); - } - return $argv; - }; -} -elseif (version_compare(PHP_VERSION, '7.1', '>=')) { - $phpunit = findCommand('phpunit7'); - $argFilters[] = function ($argv) { - $pos = array_search('--tap', $argv); - if ($pos !== FALSE) { - array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']); - } - return $argv; - }; -} -elseif (version_compare(PHP_VERSION, '7.0', '>=')) { - $phpunit = findCommand('phpunit6'); - $argFilters[] = function ($argv) { - $pos = array_search('--tap', $argv); - if ($pos !== FALSE) { - array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']); - } - return $argv; - }; -} -elseif (version_compare(PHP_VERSION, '5.6', '>=')) { - $phpunit = findCommand('phpunit5'); -} -else { - $phpunit = findCommand('phpunit4'); + +$phpunitCmd = pickPhpunitCommand(); +$phpunitPath = findCommand($phpunitCmd); +switch ($phpunitCmd) { + case 'phpunit9': + $argFilters[] = function ($argv) { + // WISHLIST: Update TAP adapter for phpunit9. It's easier to search logs for problems. + return preg_replace('/--tap/', '--debug', $argv); + }; + break; + + case 'phpunit8': + case 'phpunit7': + case 'phpunit6': + $argFilters[] = function ($argv) { + $pos = array_search('--tap', $argv); + if ($pos !== FALSE) { + array_splice($argv, $pos, 1, ['--printer', '\Civi\Test\TAP']); + } + return $argv; + }; + break; } -if (!$phpunit) { - $phpunit = findCommand('phpunit'); + +if (!$phpunitPath) { + $phpunitPath = findCommand('phpunit'); } -if (!$phpunit) { +if (!$phpunitPath) { echo "Plesae ensure that:\n"; echo " * PHPUnit is installed.\n"; echo " * The extensions for dbunit and selenium are installed.\n" ; @@ -101,11 +89,32 @@ $cmd = findPhp() // In case this system has multiple copies of PHP, use the active/preferred one. // . ' -ddisplay_errors=1' . ' ' - . escapeshellarg($phpunit) + . escapeshellarg($phpunitPath) . ' ' . implode(' ', array_map('escapeshellarg', $argv)); passthru($cmd); +function pickPhpunitCommand() { + if (getenv('PHPUNIT')) { + return getenv('PHPUNIT'); + } + elseif (version_compare(PHP_VERSION, '7.2', '>=')) { + return 'phpunit8'; + } + elseif (version_compare(PHP_VERSION, '7.1', '>=')) { + return 'phpunit7'; + } + elseif (version_compare(PHP_VERSION, '7.0', '>=')) { + return 'phpunit6'; + } + elseif (version_compare(PHP_VERSION, '5.6', '>=')) { + return 'phpunit5'; + } + else { + return 'phpunit'; + } +} + function findPhp() { // The autodetect behavior here is a potential point of contention. These two cases are hard to reconcile: // 1. `php` is actually a wrapper script which delegates to another PHP binary and -- 2.25.1