Chris@0: #!/usr/bin/env php Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: // Please update when phpunit needs to be reinstalled with fresh deps: Chris@0: // Cache-Id-Version: 2016-10-20 14:00 UTC Chris@0: Chris@0: error_reporting(-1); Chris@0: Chris@0: // PHPUnit 4.8 does not support PHP 7, while 5.1 requires PHP 5.6+ Chris@0: $PHPUNIT_VERSION = PHP_VERSION_ID >= 50600 ? getenv('SYMFONY_PHPUNIT_VERSION') ?: '5.4' : '4.8'; Chris@0: $oldPwd = getcwd(); Chris@0: $PHPUNIT_DIR = getenv('SYMFONY_PHPUNIT_DIR') ?: (__DIR__.'/.phpunit'); Chris@0: $PHP = defined('PHP_BINARY') ? PHP_BINARY : 'php'; Chris@0: $PHP = escapeshellarg($PHP); Chris@0: if ('phpdbg' === PHP_SAPI) { Chris@0: $PHP .= ' -qrr'; Chris@0: } Chris@0: Chris@0: $COMPOSER = file_exists($COMPOSER = $oldPwd.'/composer.phar') || ($COMPOSER = rtrim('\\' === DIRECTORY_SEPARATOR ? preg_replace('/[\r\n].*/', '', `where.exe composer.phar`) : `which composer.phar`)) Chris@0: ? $PHP.' '.escapeshellarg($COMPOSER) Chris@0: : 'composer'; Chris@0: Chris@0: if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__FILE__)."\n".getenv('SYMFONY_PHPUNIT_REMOVE') !== @file_get_contents("$PHPUNIT_DIR/.$PHPUNIT_VERSION.md5")) { Chris@0: // Build a standalone phpunit without symfony/yaml nor prophecy by default Chris@0: Chris@0: @mkdir($PHPUNIT_DIR); Chris@0: chdir($PHPUNIT_DIR); Chris@0: if (file_exists("phpunit-$PHPUNIT_VERSION")) { Chris@0: passthru(sprintf('\\' === DIRECTORY_SEPARATOR ? '(del /S /F /Q %s & rmdir %1$s) >nul': 'rm -rf %s', "phpunit-$PHPUNIT_VERSION")); Chris@0: } Chris@0: if (extension_loaded('openssl') && ini_get('allow_url_fopen') && !isset($_SERVER['http_proxy']) && !isset($_SERVER['https_proxy'])) { Chris@0: stream_copy_to_stream(fopen("https://github.com/sebastianbergmann/phpunit/archive/$PHPUNIT_VERSION.zip", 'rb'), fopen("$PHPUNIT_VERSION.zip", 'wb')); Chris@0: } else { Chris@0: @unlink("$PHPUNIT_VERSION.zip"); Chris@0: passthru("wget https://github.com/sebastianbergmann/phpunit/archive/$PHPUNIT_VERSION.zip"); Chris@0: } Chris@0: if (!class_exists('ZipArchive')) { Chris@0: throw new \Exception('simple-phpunit requires the "zip" PHP extension to be installed and enabled in order to uncompress the downloaded PHPUnit packages.'); Chris@0: } Chris@0: $zip = new ZipArchive(); Chris@0: $zip->open("$PHPUNIT_VERSION.zip"); Chris@0: $zip->extractTo(getcwd()); Chris@0: $zip->close(); Chris@0: chdir("phpunit-$PHPUNIT_VERSION"); Chris@0: passthru("$COMPOSER remove --no-update ".(getenv('SYMFONY_PHPUNIT_REMOVE') ?: 'phpspec/prophecy symfony/yaml')); Chris@0: if (5.1 <= $PHPUNIT_VERSION && $PHPUNIT_VERSION < 5.4) { Chris@0: passthru("$COMPOSER require --no-update phpunit/phpunit-mock-objects \"~3.1.0\""); Chris@0: } Chris@0: passthru("$COMPOSER require --no-update symfony/phpunit-bridge \">=3.2@dev\""); Chris@0: $prevRoot = getenv('COMPOSER_ROOT_VERSION'); Chris@0: putenv("COMPOSER_ROOT_VERSION=$PHPUNIT_VERSION"); Chris@0: $exit = proc_close(proc_open("$COMPOSER install --no-dev --prefer-dist --no-progress --ansi", array(), $p, getcwd(), null, array('bypass_shell' => true))); Chris@0: putenv('COMPOSER_ROOT_VERSION'.(false !== $prevRoot ? '='.$prevRoot : '')); Chris@0: if ($exit) { Chris@0: exit($exit); Chris@0: } Chris@0: file_put_contents('phpunit', <<<'EOPHP' Chris@0: setMaxDepth(getenv('SYMFONY_PHPUNIT_MAX_DEPTH') ?: 3); Chris@0: Chris@0: foreach ($finder as $file => $fileInfo) { Chris@0: if ('phpunit.xml.dist' === $file) { Chris@0: $components[] = dirname($fileInfo->getPathname()); Chris@0: } Chris@0: } Chris@0: if ($components) { Chris@0: array_shift($cmd); Chris@0: } Chris@0: } Chris@0: Chris@0: $cmd[0] = sprintf('%s %s --colors=always', $PHP, escapeshellarg("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit")); Chris@0: $cmd = str_replace('%', '%%', implode(' ', $cmd)).' %1$s'; Chris@0: Chris@0: if ('\\' === DIRECTORY_SEPARATOR) { Chris@0: $cmd = 'cmd /v:on /d /c "('.$cmd.')%2$s"'; Chris@0: } else { Chris@0: $cmd .= '%2$s'; Chris@0: } Chris@0: Chris@0: if ($components) { Chris@0: $skippedTests = isset($_SERVER['SYMFONY_PHPUNIT_SKIPPED_TESTS']) ? $_SERVER['SYMFONY_PHPUNIT_SKIPPED_TESTS'] : false; Chris@0: $runningProcs = array(); Chris@0: Chris@0: foreach ($components as $component) { Chris@0: // Run phpunit tests in parallel Chris@0: Chris@0: if ($skippedTests) { Chris@0: putenv("SYMFONY_PHPUNIT_SKIPPED_TESTS=$component/$skippedTests"); Chris@0: } Chris@0: Chris@0: $c = escapeshellarg($component); Chris@0: Chris@0: if ($proc = proc_open(sprintf($cmd, $c, " > $c/phpunit.stdout 2> $c/phpunit.stderr"), array(), $pipes)) { Chris@0: $runningProcs[$component] = $proc; Chris@0: } else { Chris@0: $exit = 1; Chris@0: echo "\033[41mKO\033[0m $component\n\n"; Chris@0: } Chris@0: } Chris@0: Chris@0: // Fixes for colors support on appveyor Chris@0: // See https://github.com/appveyor/ci/issues/373 Chris@0: $colorFixes = array( Chris@0: array("S\033[0m\033[0m\033[36m\033[1mS", "E\033[0m\033[0m\033[31m\033[1mE", "I\033[0m\033[0m\033[33m\033[1mI", "F\033[0m\033[0m\033[41m\033[37mF"), Chris@0: array("SS", "EE", "II", "FF"), Chris@0: ); Chris@0: $colorFixes[0] = array_merge($colorFixes[0], $colorFixes[0]); Chris@0: $colorFixes[1] = array_merge($colorFixes[1], $colorFixes[1]); Chris@0: Chris@0: while ($runningProcs) { Chris@0: usleep(300000); Chris@0: $terminatedProcs = array(); Chris@0: foreach ($runningProcs as $component => $proc) { Chris@0: $procStatus = proc_get_status($proc); Chris@0: if (!$procStatus['running']) { Chris@0: $terminatedProcs[$component] = $procStatus['exitcode']; Chris@0: unset($runningProcs[$component]); Chris@0: proc_close($proc); Chris@0: } Chris@0: } Chris@0: Chris@0: foreach ($terminatedProcs as $component => $procStatus) { Chris@0: foreach (array('out', 'err') as $file) { Chris@0: $file = "$component/phpunit.std$file"; Chris@0: Chris@0: if ('\\' === DIRECTORY_SEPARATOR) { Chris@0: $h = fopen($file, 'rb'); Chris@0: while (false !== $line = fgets($h)) { Chris@0: echo str_replace($colorFixes[0], $colorFixes[1], preg_replace( Chris@0: '/(\033\[[0-9]++);([0-9]++m)(?:(.)(\033\[0m))?/', Chris@0: "$1m\033[$2$3$4$4", Chris@0: $line Chris@0: )); Chris@0: } Chris@0: fclose($h); Chris@0: } else { Chris@0: readfile($file); Chris@0: } Chris@0: unlink($file); Chris@0: } Chris@0: Chris@0: // Fail on any individual component failures but ignore some error codes on Windows when APCu is enabled: Chris@0: // STATUS_STACK_BUFFER_OVERRUN (-1073740791/0xC0000409) Chris@0: // STATUS_ACCESS_VIOLATION (-1073741819/0xC0000005) Chris@0: // STATUS_HEAP_CORRUPTION (-1073740940/0xC0000374) Chris@0: if ($procStatus && ('\\' !== DIRECTORY_SEPARATOR || !extension_loaded('apcu') || !ini_get('apc.enable_cli') || !in_array($procStatus, array(-1073740791, -1073741819, -1073740940)))) { Chris@0: $exit = $procStatus; Chris@0: echo "\033[41mKO\033[0m $component\n\n"; Chris@0: } else { Chris@0: echo "\033[32mOK\033[0m $component\n\n"; Chris@0: } Chris@0: } Chris@0: } Chris@0: } elseif (!isset($argv[1]) || 'install' !== $argv[1] || file_exists('install')) { Chris@0: // Run regular phpunit in a subprocess Chris@0: Chris@0: $errFile = tempnam(sys_get_temp_dir(), 'phpunit.stderr.'); Chris@0: if ($proc = proc_open(sprintf($cmd, '', ' 2> '.escapeshellarg($errFile)), array(1 => array('pipe', 'w')), $pipes)) { Chris@0: stream_copy_to_stream($pipes[1], STDOUT); Chris@0: fclose($pipes[1]); Chris@0: $exit = proc_close($proc); Chris@0: Chris@0: readfile($errFile); Chris@0: unlink($errFile); Chris@0: } Chris@0: Chris@0: if (!file_exists($component = array_pop($argv))) { Chris@0: $component = basename($oldPwd); Chris@0: } Chris@0: Chris@0: if ($exit) { Chris@0: echo "\033[41mKO\033[0m $component\n\n"; Chris@0: } else { Chris@0: echo "\033[32mOK\033[0m $component\n\n"; Chris@0: } Chris@0: } Chris@0: Chris@0: exit($exit);