|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +error_reporting(-1); |
| 5 | + |
| 6 | +$PHPUNIT_VERSION = 4.8; |
| 7 | +$PHPUNIT_DIR = __DIR__.'/.phpunit'; |
| 8 | + |
| 9 | +if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit")) { |
| 10 | + # Build a standalone phpunit without symfony/yaml |
| 11 | + |
| 12 | + $oldPwd = getcwd(); |
| 13 | + mkdir($PHPUNIT_DIR); |
| 14 | + chdir($PHPUNIT_DIR); |
| 15 | + if (extension_loaded('openssl') && ini_get('allow_url_fopen')) { |
| 16 | + stream_copy_to_stream(fopen("https://github.com/sebastianbergmann/phpunit/archive/$PHPUNIT_VERSION.zip", 'rb'), fopen("$PHPUNIT_VERSION.zip", 'wb')); |
| 17 | + } else { |
| 18 | + passthru("wget https://github.com/sebastianbergmann/phpunit/archive/$PHPUNIT_VERSION.zip"); |
| 19 | + } |
| 20 | + $zip = new ZipArchive(); |
| 21 | + $zip->open("$PHPUNIT_VERSION.zip"); |
| 22 | + $zip->extractTo(getcwd()); |
| 23 | + $zip->close(); |
| 24 | + chdir("phpunit-$PHPUNIT_VERSION"); |
| 25 | + passthru("composer remove --no-update symfony/yaml"); |
| 26 | + passthru("composer install --prefer-source --no-progress --ansi"); |
| 27 | + chdir($oldPwd); |
| 28 | +} |
| 29 | + |
| 30 | +$cmd = array_map('escapeshellarg', $argv); |
| 31 | +$exit = 0; |
| 32 | + |
| 33 | +if (isset($argv[1]) && 'symfony' === $argv[1]) { |
| 34 | + # Find Symfony components in plain php for Windows portability |
| 35 | + |
| 36 | + $finder = new RecursiveDirectoryIterator('src/Symfony', FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::UNIX_PATHS); |
| 37 | + $finder = new RecursiveIteratorIterator($finder); |
| 38 | + $finder->setMaxDepth(3); |
| 39 | + |
| 40 | + array_shift($cmd); |
| 41 | + $cmd[0] = "php $PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit --colors=always"; |
| 42 | + $procs = array(); |
| 43 | + |
| 44 | + foreach ($finder as $file => $fileInfo) { |
| 45 | + if ('phpunit.xml.dist' === $file) { |
| 46 | + $component = dirname($fileInfo->getPathname()); |
| 47 | + |
| 48 | + # Run phpunit tests in parallel |
| 49 | + |
| 50 | + $c = escapeshellarg($component); |
| 51 | + |
| 52 | + if ($proc = proc_open(implode(' ', $cmd)." $c > $c/phpunit.stdout 2> $c/phpunit.stderr", array(), $pipes)) { |
| 53 | + $procs[$component] = $proc; |
| 54 | + } else { |
| 55 | + $exit = 1; |
| 56 | + echo "\033[41mKO\033[0m $component\n\n"; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($procs as $component => $proc) { |
| 62 | + $procStatus = proc_close($proc); |
| 63 | + |
| 64 | + foreach (array('out', 'err') as $file) { |
| 65 | + $file = "$component/phpunit.std$file"; |
| 66 | + $h = fopen($file, 'rb'); |
| 67 | + while (false !== $line = fgets($h)) { |
| 68 | + echo preg_replace_callback( |
| 69 | + '/\033\[[0-9]++(?:;[0-9]++)++m/', |
| 70 | + function ($m) {return str_replace(';', "m\033[", $m[0]);}, |
| 71 | + $line |
| 72 | + ); |
| 73 | + } |
| 74 | + fclose($h); |
| 75 | + unlink($file); |
| 76 | + } |
| 77 | + |
| 78 | + if ($procStatus) { |
| 79 | + $exit = 1; |
| 80 | + echo "\033[41mKO\033[0m $component\n\n"; |
| 81 | + } else { |
| 82 | + echo "\033[32mOK\033[0m $component\n\n"; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} elseif (!isset($argv[1]) || 'install' !== $argv[1]) { |
| 87 | + # Run regular phpunit in a subprocess |
| 88 | + |
| 89 | + $cmd[0] = "php $PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit --colors=always"; |
| 90 | + |
| 91 | + $errFile = tempnam(sys_get_temp_dir(), 'phpunit.stderr.'); |
| 92 | + if ($proc = proc_open(implode(' ', $cmd).' 2> '.escapeshellarg($errFile), array(1 => array('pipe', 'w')), $pipes)) { |
| 93 | + while (false !== $line = fgets($pipes[1])) { |
| 94 | + echo $line; |
| 95 | + } |
| 96 | + fclose($pipes[1]); |
| 97 | + $exit = proc_close($proc); |
| 98 | + |
| 99 | + $h = fopen($errFile, 'rb'); |
| 100 | + while (false !== $line = fgets($h)) { |
| 101 | + echo $line; |
| 102 | + } |
| 103 | + fclose($h); |
| 104 | + unlink($errFile); |
| 105 | + } |
| 106 | + |
| 107 | + if (file_exists($component = array_pop($argv))) { |
| 108 | + if ($exit) { |
| 109 | + echo "\033[41mKO\033[0m $component\n\n"; |
| 110 | + } else { |
| 111 | + echo "\033[32mOK\033[0m $component\n\n"; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +exit($exit); |
0 commit comments