From 1adfbacdccce90f1a5d5034b6e9af31ad1ab7443 Mon Sep 17 00:00:00 2001 From: Artem Lopata Date: Wed, 4 Dec 2019 11:31:52 +0100 Subject: [PATCH] Properly handle phpunit arguments for configuration file --- bin/simple-phpunit.php | 46 +++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/bin/simple-phpunit.php b/bin/simple-phpunit.php index 50873b61..4a8993dc 100644 --- a/bin/simple-phpunit.php +++ b/bin/simple-phpunit.php @@ -24,15 +24,47 @@ static $phpunitConfig = null; if (null === $phpunitConfig) { - $opt = min(array_search('-c', $opts = array_reverse($argv), true) ?: INF, array_search('--configuration', $opts, true) ?: INF); $phpunitConfigFilename = null; - if (INF !== $opt && isset($opts[$opt - 1])) { - $phpunitConfigFilename = $opts[$opt - 1]; - } elseif (file_exists('phpunit.xml')) { - $phpunitConfigFilename = 'phpunit.xml'; - } elseif (file_exists('phpunit.xml.dist')) { - $phpunitConfigFilename = 'phpunit.xml.dist'; + $getPhpUnitConfig = function ($probableConfig) use (&$getPhpUnitConfig) { + if (!$probableConfig) { + return null; + } + if (is_dir($probableConfig)) { + return $getPhpUnitConfig($probableConfig.DIRECTORY_SEPARATOR.'phpunit.xml'); + } + + if (file_exists($probableConfig)) { + return $probableConfig; + } + if (file_exists($probableConfig.'.dist')) { + return $probableConfig.'.dist'; + } + + return null; + }; + + foreach ($argv as $cliArgumentIndex => $cliArgument) { + if ('--' === $cliArgument) { + break; + } + // long option + if ('--configuration' === $cliArgument && array_key_exists($cliArgumentIndex + 1, $argv)) { + $phpunitConfigFilename = $getPhpUnitConfig($argv[$cliArgumentIndex + 1]); + break; + } + // short option + if (0 === strpos($cliArgument, '-c')) { + if ('-c' === $cliArgument && array_key_exists($cliArgumentIndex + 1, $argv)) { + $phpunitConfigFilename = $getPhpUnitConfig($argv[$cliArgumentIndex + 1]); + } else { + $phpunitConfigFilename = $getPhpUnitConfig(substr($cliArgument, 2)); + } + break; + } } + + $phpunitConfigFilename = $phpunitConfigFilename ?: $getPhpUnitConfig('phpunit.xml'); + if ($phpunitConfigFilename) { $phpunitConfig = new DomDocument(); $phpunitConfig->load($phpunitConfigFilename);