Skip to content

Commit d38886c

Browse files
committed
[Bridge/PhpUnit] Extract all the code but shebang from bin/simple-phpunit
The change allow `simple-phpunit` to be included without `#!/usr/bin/env php` string outputted. Also it's the first step to fix #27035 (the second one is to change phpunit bridge recipe).
1 parent b82b09e commit d38886c

File tree

3 files changed

+263
-250
lines changed

3 files changed

+263
-250
lines changed

src/Symfony/Bridge/PhpUnit/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `ClassExistsMock`
88
* bumped PHP version from 5.3.3 to 5.5.9
9+
* split simple-phpunit bin into php file with code and a shel script
910

1011
4.1.0
1112
-----

src/Symfony/Bridge/PhpUnit/bin/simple-phpunit

+1-250
Original file line numberDiff line numberDiff line change
@@ -10,253 +10,4 @@
1010
* file that was distributed with this source code.
1111
*/
1212

13-
// Please update when phpunit needs to be reinstalled with fresh deps:
14-
// Cache-Id-Version: 2018-11-20 15:30 UTC
15-
16-
error_reporting(-1);
17-
18-
$getEnvVar = function ($name, $default = false) {
19-
if (false !== $value = getenv($name)) {
20-
return $value;
21-
}
22-
23-
static $phpunitConfig = null;
24-
if (null === $phpunitConfig) {
25-
$phpunitConfigFilename = null;
26-
if (file_exists('phpunit.xml')) {
27-
$phpunitConfigFilename = 'phpunit.xml';
28-
} elseif (file_exists('phpunit.xml.dist')) {
29-
$phpunitConfigFilename = 'phpunit.xml.dist';
30-
}
31-
if ($phpunitConfigFilename) {
32-
$phpunitConfig = new DomDocument();
33-
$phpunitConfig->load($phpunitConfigFilename);
34-
} else {
35-
$phpunitConfig = false;
36-
}
37-
}
38-
if (false !== $phpunitConfig) {
39-
$var = new DOMXpath($phpunitConfig);
40-
foreach ($var->query('//php/server[@name="'.$name.'"]') as $var) {
41-
return $var->getAttribute('value');
42-
}
43-
foreach ($var->query('//php/env[@name="'.$name.'"]') as $var) {
44-
return $var->getAttribute('value');
45-
}
46-
}
47-
48-
return $default;
49-
};
50-
51-
if (PHP_VERSION_ID >= 70100) {
52-
// PHPUnit 7 requires PHP 7.1+
53-
$PHPUNIT_VERSION = $getEnvVar('SYMFONY_PHPUNIT_VERSION', '7.4');
54-
} elseif (PHP_VERSION_ID >= 70000) {
55-
// PHPUnit 6 requires PHP 7.0+
56-
$PHPUNIT_VERSION = $getEnvVar('SYMFONY_PHPUNIT_VERSION', '6.5');
57-
} elseif (PHP_VERSION_ID >= 50600) {
58-
// PHPUnit 5 requires PHP 5.6+
59-
$PHPUNIT_VERSION = $getEnvVar('SYMFONY_PHPUNIT_VERSION', '5.7');
60-
} else {
61-
$PHPUNIT_VERSION = '4.8';
62-
}
63-
64-
$COMPOSER_JSON = getenv('COMPOSER') ?: 'composer.json';
65-
66-
$root = __DIR__;
67-
while (!file_exists($root.'/'.$COMPOSER_JSON) || file_exists($root.'/DeprecationErrorHandler.php')) {
68-
if ($root === dirname($root)) {
69-
break;
70-
}
71-
$root = dirname($root);
72-
}
73-
74-
$oldPwd = getcwd();
75-
$PHPUNIT_DIR = $getEnvVar('SYMFONY_PHPUNIT_DIR', $root.'/vendor/bin/.phpunit');
76-
$PHP = defined('PHP_BINARY') ? PHP_BINARY : 'php';
77-
$PHP = escapeshellarg($PHP);
78-
if ('phpdbg' === PHP_SAPI) {
79-
$PHP .= ' -qrr';
80-
}
81-
82-
$defaultEnvs = array(
83-
'COMPOSER' => 'composer.json',
84-
'COMPOSER_VENDOR_DIR' => 'vendor',
85-
'COMPOSER_BIN_DIR' => 'bin',
86-
);
87-
88-
foreach ($defaultEnvs as $envName => $envValue) {
89-
if ($envValue !== getenv($envName)) {
90-
putenv("$envName=$envValue");
91-
$_SERVER[$envName] = $_ENV[$envName] = $envValue;
92-
}
93-
}
94-
95-
$COMPOSER = file_exists($COMPOSER = $oldPwd.'/composer.phar') || ($COMPOSER = rtrim('\\' === DIRECTORY_SEPARATOR ? preg_replace('/[\r\n].*/', '', `where.exe composer.phar`) : `which composer.phar 2> /dev/null`))
96-
? $PHP.' '.escapeshellarg($COMPOSER)
97-
: 'composer';
98-
99-
100-
$SYMFONY_PHPUNIT_REMOVE = $getEnvVar('SYMFONY_PHPUNIT_REMOVE', 'phpspec/prophecy'.($PHPUNIT_VERSION < 6.0 ? ' symfony/yaml': ''));
101-
102-
if (!file_exists("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit") || md5_file(__FILE__)."\n".$SYMFONY_PHPUNIT_REMOVE !== @file_get_contents("$PHPUNIT_DIR/.$PHPUNIT_VERSION.md5")) {
103-
// Build a standalone phpunit without symfony/yaml nor prophecy by default
104-
105-
@mkdir($PHPUNIT_DIR, 0777, true);
106-
chdir($PHPUNIT_DIR);
107-
if (file_exists("phpunit-$PHPUNIT_VERSION")) {
108-
passthru(sprintf('\\' === DIRECTORY_SEPARATOR ? 'rmdir /S /Q %s > NUL': 'rm -rf %s', "phpunit-$PHPUNIT_VERSION.old"));
109-
rename("phpunit-$PHPUNIT_VERSION", "phpunit-$PHPUNIT_VERSION.old");
110-
passthru(sprintf('\\' === DIRECTORY_SEPARATOR ? 'rmdir /S /Q %s': 'rm -rf %s', "phpunit-$PHPUNIT_VERSION.old"));
111-
}
112-
passthru("$COMPOSER create-project --no-install --prefer-dist --no-scripts --no-plugins --no-progress --ansi phpunit/phpunit phpunit-$PHPUNIT_VERSION \"$PHPUNIT_VERSION.*\"");
113-
chdir("phpunit-$PHPUNIT_VERSION");
114-
if ($SYMFONY_PHPUNIT_REMOVE) {
115-
passthru("$COMPOSER remove --no-update ".$SYMFONY_PHPUNIT_REMOVE);
116-
}
117-
if (5.1 <= $PHPUNIT_VERSION && $PHPUNIT_VERSION < 5.4) {
118-
passthru("$COMPOSER require --no-update phpunit/phpunit-mock-objects \"~3.1.0\"");
119-
}
120-
if (file_exists($path = $root.'/vendor/symfony/phpunit-bridge')) {
121-
passthru("$COMPOSER require --no-update symfony/phpunit-bridge \"*@dev\"");
122-
passthru("$COMPOSER config repositories.phpunit-bridge path ".escapeshellarg(str_replace('/', DIRECTORY_SEPARATOR, $path)));
123-
if ('\\' === DIRECTORY_SEPARATOR) {
124-
file_put_contents('composer.json', preg_replace('/^( {8})"phpunit-bridge": \{$/m', "$0\n$1 ".'"options": {"symlink": false},', file_get_contents('composer.json')));
125-
}
126-
} else {
127-
passthru("$COMPOSER require --no-update symfony/phpunit-bridge \"*\"");
128-
}
129-
$prevRoot = getenv('COMPOSER_ROOT_VERSION');
130-
putenv("COMPOSER_ROOT_VERSION=$PHPUNIT_VERSION.99");
131-
// --no-suggest is not in the list to keep compat with composer 1.0, which is shipped with Ubuntu 16.04LTS
132-
$exit = proc_close(proc_open("$COMPOSER install --no-dev --prefer-dist --no-progress --ansi", array(), $p, getcwd(), null, array('bypass_shell' => true)));
133-
putenv('COMPOSER_ROOT_VERSION'.(false !== $prevRoot ? '='.$prevRoot : ''));
134-
if ($exit) {
135-
exit($exit);
136-
}
137-
file_put_contents('phpunit', <<<'EOPHP'
138-
<?php
139-
140-
define('PHPUNIT_COMPOSER_INSTALL', __DIR__.'/vendor/autoload.php');
141-
require PHPUNIT_COMPOSER_INSTALL;
142-
143-
if (!class_exists('SymfonyBlacklistPhpunit', false)) {
144-
class SymfonyBlacklistPhpunit {}
145-
}
146-
if (class_exists('PHPUnit_Util_Blacklist')) {
147-
PHPUnit_Util_Blacklist::$blacklistedClassNames['SymfonyBlacklistPhpunit'] = 1;
148-
PHPUnit_Util_Blacklist::$blacklistedClassNames['SymfonyBlacklistSimplePhpunit'] = 1;
149-
} else {
150-
PHPUnit\Util\Blacklist::$blacklistedClassNames['SymfonyBlacklistPhpunit'] = 1;
151-
PHPUnit\Util\Blacklist::$blacklistedClassNames['SymfonyBlacklistSimplePhpunit'] = 1;
152-
}
153-
154-
Symfony\Bridge\PhpUnit\TextUI\Command::main();
155-
156-
EOPHP
157-
);
158-
chdir('..');
159-
file_put_contents(".$PHPUNIT_VERSION.md5", md5_file(__FILE__)."\n".$SYMFONY_PHPUNIT_REMOVE);
160-
chdir($oldPwd);
161-
162-
}
163-
164-
global $argv, $argc;
165-
$argv = isset($_SERVER['argv']) ? $_SERVER['argv'] : array();
166-
$argc = isset($_SERVER['argc']) ? $_SERVER['argc'] : 0;
167-
$components = array();
168-
$cmd = array_map('escapeshellarg', $argv);
169-
$exit = 0;
170-
171-
if (isset($argv[1]) && 'symfony' === $argv[1] && !file_exists('symfony') && file_exists('src/Symfony')) {
172-
$argv[1] = 'src/Symfony';
173-
}
174-
if (isset($argv[1]) && is_dir($argv[1]) && !file_exists($argv[1].'/phpunit.xml.dist')) {
175-
// Find Symfony components in plain PHP for Windows portability
176-
177-
$finder = new RecursiveDirectoryIterator($argv[1], FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::UNIX_PATHS);
178-
$finder = new RecursiveIteratorIterator($finder);
179-
$finder->setMaxDepth(getenv('SYMFONY_PHPUNIT_MAX_DEPTH') ?: 3);
180-
181-
foreach ($finder as $file => $fileInfo) {
182-
if ('phpunit.xml.dist' === $file) {
183-
$components[] = dirname($fileInfo->getPathname());
184-
}
185-
}
186-
if ($components) {
187-
array_shift($cmd);
188-
}
189-
}
190-
191-
$cmd[0] = sprintf('%s %s --colors=always', $PHP, escapeshellarg("$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit"));
192-
$cmd = str_replace('%', '%%', implode(' ', $cmd)).' %1$s';
193-
194-
if ('\\' === DIRECTORY_SEPARATOR) {
195-
$cmd = 'cmd /v:on /d /c "('.$cmd.')%2$s"';
196-
} else {
197-
$cmd .= '%2$s';
198-
}
199-
200-
if ($components) {
201-
$skippedTests = isset($_SERVER['SYMFONY_PHPUNIT_SKIPPED_TESTS']) ? $_SERVER['SYMFONY_PHPUNIT_SKIPPED_TESTS'] : false;
202-
$runningProcs = array();
203-
204-
foreach ($components as $component) {
205-
// Run phpunit tests in parallel
206-
207-
if ($skippedTests) {
208-
putenv("SYMFONY_PHPUNIT_SKIPPED_TESTS=$component/$skippedTests");
209-
}
210-
211-
$c = escapeshellarg($component);
212-
213-
if ($proc = proc_open(sprintf($cmd, $c, " > $c/phpunit.stdout 2> $c/phpunit.stderr"), array(), $pipes)) {
214-
$runningProcs[$component] = $proc;
215-
} else {
216-
$exit = 1;
217-
echo "\033[41mKO\033[0m $component\n\n";
218-
}
219-
}
220-
221-
while ($runningProcs) {
222-
usleep(300000);
223-
$terminatedProcs = array();
224-
foreach ($runningProcs as $component => $proc) {
225-
$procStatus = proc_get_status($proc);
226-
if (!$procStatus['running']) {
227-
$terminatedProcs[$component] = $procStatus['exitcode'];
228-
unset($runningProcs[$component]);
229-
proc_close($proc);
230-
}
231-
}
232-
233-
foreach ($terminatedProcs as $component => $procStatus) {
234-
foreach (array('out', 'err') as $file) {
235-
$file = "$component/phpunit.std$file";
236-
readfile($file);
237-
unlink($file);
238-
}
239-
240-
// Fail on any individual component failures but ignore some error codes on Windows when APCu is enabled:
241-
// STATUS_STACK_BUFFER_OVERRUN (-1073740791/0xC0000409)
242-
// STATUS_ACCESS_VIOLATION (-1073741819/0xC0000005)
243-
// STATUS_HEAP_CORRUPTION (-1073740940/0xC0000374)
244-
if ($procStatus && ('\\' !== DIRECTORY_SEPARATOR || !extension_loaded('apcu') || !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN) || !in_array($procStatus, array(-1073740791, -1073741819, -1073740940)))) {
245-
$exit = $procStatus;
246-
echo "\033[41mKO\033[0m $component\n\n";
247-
} else {
248-
echo "\033[32mOK\033[0m $component\n\n";
249-
}
250-
}
251-
}
252-
} elseif (!isset($argv[1]) || 'install' !== $argv[1] || file_exists('install')) {
253-
if (!class_exists('SymfonyBlacklistSimplePhpunit', false)) {
254-
class SymfonyBlacklistSimplePhpunit {}
255-
}
256-
array_splice($argv, 1, 0, array('--colors=always'));
257-
$_SERVER['argv'] = $argv;
258-
$_SERVER['argc'] = ++$argc;
259-
include "$PHPUNIT_DIR/phpunit-$PHPUNIT_VERSION/phpunit";
260-
}
261-
262-
exit($exit);
13+
require __DIR__.DIRECTORY_SEPARATOR.'simple-phpunit.php';

0 commit comments

Comments
 (0)