2012-08-13 23:16:40 +00:00
|
|
|
<?php
|
|
|
|
|
2013-05-27 08:41:50 +00:00
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-08-13 23:16:40 +00:00
|
|
|
namespace Composer\Test;
|
|
|
|
|
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
use Symfony\Component\Finder\Finder;
|
2016-01-21 12:01:55 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2012-08-13 23:16:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group slow
|
|
|
|
*/
|
2016-01-21 12:01:55 +00:00
|
|
|
class AllFunctionalTest extends TestCase
|
2012-08-13 23:16:40 +00:00
|
|
|
{
|
2021-10-15 14:26:22 +00:00
|
|
|
/** @var string|false */
|
2012-11-10 18:42:29 +00:00
|
|
|
protected $oldcwd;
|
2021-10-15 14:26:22 +00:00
|
|
|
/** @var ?string */
|
2012-11-10 20:54:23 +00:00
|
|
|
protected $testDir;
|
2021-10-15 14:26:22 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-11-11 16:09:57 +00:00
|
|
|
private static $pharPath;
|
2012-11-10 20:54:23 +00:00
|
|
|
|
2012-11-10 18:42:29 +00:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->oldcwd = getcwd();
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-10 18:42:29 +00:00
|
|
|
chdir(__DIR__.'/Fixtures/functional');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
2021-10-15 14:26:22 +00:00
|
|
|
if ($this->oldcwd) {
|
|
|
|
chdir($this->oldcwd);
|
|
|
|
}
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2012-11-10 20:54:23 +00:00
|
|
|
if ($this->testDir) {
|
2021-02-17 22:26:40 +00:00
|
|
|
$fs = new Filesystem;
|
2012-11-10 20:54:23 +00:00
|
|
|
$fs->removeDirectory($this->testDir);
|
|
|
|
$this->testDir = null;
|
|
|
|
}
|
2012-11-11 16:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
2016-01-21 12:01:55 +00:00
|
|
|
self::$pharPath = self::getUniqueTmpDirectory() . '/composer.phar';
|
2012-11-11 16:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass()
|
|
|
|
{
|
|
|
|
$fs = new Filesystem;
|
|
|
|
$fs->removeDirectory(dirname(self::$pharPath));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildPhar()
|
|
|
|
{
|
2014-12-02 13:31:37 +00:00
|
|
|
if (defined('HHVM_VERSION')) {
|
|
|
|
$this->markTestSkipped('Building the phar does not work on HHVM.');
|
|
|
|
}
|
2014-12-07 13:54:29 +00:00
|
|
|
|
2015-05-01 13:41:15 +00:00
|
|
|
$target = dirname(self::$pharPath);
|
2016-01-21 12:01:55 +00:00
|
|
|
$fs = new Filesystem();
|
2015-05-01 13:41:15 +00:00
|
|
|
chdir($target);
|
|
|
|
|
|
|
|
$it = new \RecursiveDirectoryIterator(__DIR__.'/../../../', \RecursiveDirectoryIterator::SKIP_DOTS);
|
|
|
|
$ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
|
|
|
|
|
|
|
|
foreach ($ri as $file) {
|
|
|
|
$targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName();
|
|
|
|
if ($file->isDir()) {
|
|
|
|
$fs->ensureDirectoryExists($targetPath);
|
|
|
|
} else {
|
|
|
|
copy($file->getPathname(), $targetPath);
|
|
|
|
}
|
|
|
|
}
|
2012-11-11 16:09:57 +00:00
|
|
|
|
2021-08-21 15:41:52 +00:00
|
|
|
// TODO in v2.3 always call with an array
|
|
|
|
if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) {
|
|
|
|
$proc = new Process(array((defined('PHP_BINARY') ? PHP_BINARY : 'php'), '-dphar.readonly=0', './bin/compile'), $target);
|
|
|
|
} else {
|
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
$proc = new Process((defined('PHP_BINARY') ? escapeshellcmd(PHP_BINARY) : 'php').' -dphar.readonly=0 '.escapeshellarg('./bin/compile'), $target);
|
|
|
|
}
|
2012-11-11 16:09:57 +00:00
|
|
|
$exitcode = $proc->run();
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2013-03-10 12:58:49 +00:00
|
|
|
if ($exitcode !== 0 || trim($proc->getOutput())) {
|
|
|
|
$this->fail($proc->getOutput());
|
|
|
|
}
|
2016-01-21 12:01:55 +00:00
|
|
|
|
2017-11-30 14:58:10 +00:00
|
|
|
$this->assertFileExists(self::$pharPath);
|
2012-11-10 18:42:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-13 23:16:40 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getTestFiles
|
2012-11-11 16:09:57 +00:00
|
|
|
* @depends testBuildPhar
|
2021-11-02 13:32:09 +00:00
|
|
|
* @param string $testFile
|
2012-08-13 23:16:40 +00:00
|
|
|
*/
|
2020-07-17 09:30:03 +00:00
|
|
|
public function testIntegration($testFile)
|
2012-08-13 23:16:40 +00:00
|
|
|
{
|
|
|
|
$testData = $this->parseTestFile($testFile);
|
2021-02-12 10:05:13 +00:00
|
|
|
$this->testDir = self::getUniqueTmpDirectory();
|
|
|
|
|
|
|
|
// if a dir is present with the name of the .test file (without .test), we
|
|
|
|
// copy all its contents in the $testDir to be used to run the test with
|
|
|
|
$testFileSetupDir = substr($testFile, 0, -5);
|
|
|
|
if (is_dir($testFileSetupDir)) {
|
|
|
|
$fs = new Filesystem();
|
|
|
|
$fs->copy($testFileSetupDir, $this->testDir);
|
|
|
|
}
|
2012-08-13 23:16:40 +00:00
|
|
|
|
2021-02-17 22:26:40 +00:00
|
|
|
$env = array(
|
|
|
|
'COMPOSER_HOME' => $this->testDir.'home',
|
|
|
|
'COMPOSER_CACHE_DIR' => $this->testDir.'cache',
|
|
|
|
);
|
2012-11-11 14:52:37 +00:00
|
|
|
|
2021-08-21 15:41:52 +00:00
|
|
|
// TODO in v2.3 always call with an array
|
|
|
|
if (method_exists('Symfony\Component\Process\Process', 'fromShellCommandline')) {
|
|
|
|
$cmd = array((defined('PHP_BINARY') ? PHP_BINARY : 'php'), self::$pharPath, '--no-ansi', $testData['RUN']);
|
|
|
|
$proc = new Process($cmd, $this->testDir, $env, null, 300);
|
|
|
|
} else {
|
|
|
|
$cmd = (defined('PHP_BINARY') ? escapeshellcmd(PHP_BINARY) : 'php') .' '.escapeshellarg(self::$pharPath).' --no-ansi '.$testData['RUN'];
|
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
$proc = new Process($cmd, $this->testDir, $env, null, 300);
|
|
|
|
}
|
2021-02-12 10:05:13 +00:00
|
|
|
$output = '';
|
|
|
|
|
|
|
|
$exitcode = $proc->run(function ($type, $buffer) use (&$output) {
|
|
|
|
$output .= $buffer;
|
|
|
|
});
|
2012-08-13 23:16:40 +00:00
|
|
|
|
|
|
|
if (isset($testData['EXPECT'])) {
|
2021-02-12 10:05:13 +00:00
|
|
|
$output = trim($this->cleanOutput($output));
|
|
|
|
$expected = $testData['EXPECT'];
|
|
|
|
|
|
|
|
$line = 1;
|
2021-02-25 12:46:52 +00:00
|
|
|
for ($i = 0, $j = 0; $i < strlen($expected);) {
|
2021-02-12 10:05:13 +00:00
|
|
|
if ($expected[$i] === "\n") {
|
|
|
|
$line++;
|
|
|
|
}
|
|
|
|
if ($expected[$i] === '%') {
|
|
|
|
preg_match('{%(.+?)%}', substr($expected, $i), $match);
|
|
|
|
$regex = $match[1];
|
|
|
|
|
|
|
|
if (preg_match('{'.$regex.'}', substr($output, $j), $match)) {
|
|
|
|
$i += strlen($regex) + 2;
|
|
|
|
$j += strlen($match[0]);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
$this->fail(
|
|
|
|
'Failed to match pattern '.$regex.' at line '.$line.' / abs offset '.$i.': '
|
2021-02-25 12:46:52 +00:00
|
|
|
.substr($output, $j, min(strpos($output, "\n", $j) - $j, 100)).PHP_EOL.PHP_EOL.
|
2021-02-12 10:05:13 +00:00
|
|
|
'Output:'.PHP_EOL.$output
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($expected[$i] !== $output[$j]) {
|
|
|
|
$this->fail(
|
|
|
|
'Output does not match expectation at line '.$line.' / abs offset '.$i.': '.PHP_EOL
|
2021-02-25 12:46:52 +00:00
|
|
|
.'-'.substr($expected, $i, min(strpos($expected, "\n", $i) - $i, 100)).PHP_EOL
|
|
|
|
.'+'.substr($output, $j, min(strpos($output, "\n", $j) - $j, 100)).PHP_EOL.PHP_EOL
|
2021-02-12 10:05:13 +00:00
|
|
|
.'Output:'.PHP_EOL.$output
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$i++;
|
|
|
|
$j++;
|
|
|
|
}
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
if (isset($testData['EXPECT-REGEX'])) {
|
2021-02-12 10:05:13 +00:00
|
|
|
$this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($output));
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
2021-02-12 10:05:13 +00:00
|
|
|
if (isset($testData['EXPECT-REGEXES'])) {
|
|
|
|
$cleanOutput = $this->cleanOutput($output);
|
|
|
|
foreach (explode("\n", $testData['EXPECT-REGEXES']) as $regex) {
|
|
|
|
$this->assertRegExp($regex, $cleanOutput, 'Output: '.$output);
|
|
|
|
}
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
if (isset($testData['EXPECT-EXIT-CODE'])) {
|
|
|
|
$this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 14:26:22 +00:00
|
|
|
/**
|
|
|
|
* @return array<string, array<string>>
|
|
|
|
*/
|
2012-08-13 23:16:40 +00:00
|
|
|
public function getTestFiles()
|
|
|
|
{
|
|
|
|
$tests = array();
|
|
|
|
foreach (Finder::create()->in(__DIR__.'/Fixtures/functional')->name('*.test')->files() as $file) {
|
2021-02-12 10:05:13 +00:00
|
|
|
$tests[basename($file)] = array($file->getRealPath());
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
}
|
|
|
|
|
2021-10-15 14:26:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $file
|
|
|
|
* @return array<string, int|string>
|
|
|
|
*/
|
2020-07-17 09:30:03 +00:00
|
|
|
private function parseTestFile($file)
|
2012-08-13 23:16:40 +00:00
|
|
|
{
|
PHP 8.1: fix deprecation warnings about incorrect default values (#10036)
* PHP 8.1/Tests: fix some deprecation warnings
The default value for the `preg_split()` `$limit` parameter is `-1`, not `null`.
Fixes numerous `preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated` notices when running the test suite.
Ref: https://www.php.net/manual/en/function.preg-split.php
* PHP 8.1/NoProxyPattern: fix deprecation warning
The default value for the `preg_split()` `$limit` parameter is `-1`, not `null`.
Fixes some `preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated` notices when running the test suite.
```
Deprecation triggered by Composer\Test\Util\Http\ProxyManagerTest::testGetProxyForRequest:
preg_split(): Passing null to parameter #3 ($limit) of type int is deprecated
Stack trace:
0 [internal function]: Symfony\Bridge\PhpUnit\DeprecationErrorHandler->handleError(8192, '...', '...', 42)
1 src/Composer/Util/NoProxyPattern.php(42): preg_split('...', '...', NULL, 1)
2 src/Composer/Util/Http/ProxyManager.php(148): Composer\Util\NoProxyPattern->__construct('...')
3 src/Composer/Util/Http/ProxyManager.php(50): Composer\Util\Http\ProxyManager->initProxyData()
4 src/Composer/Util/Http/ProxyManager.php(59): Composer\Util\Http\ProxyManager->__construct()
5 tests/Composer/Test/Util/Http/ProxyManagerTest.php(75): Composer\Util\Http\ProxyManager::getInstance()
...
```
Ref: https://www.php.net/manual/en/function.preg-split.php
* PHP 8.1: fix deprecation warnings / http_build_query()
This fixes all relevant calls to the PHP native `http_build_query()` function.
The second parameter of which is the _optional_ `$numeric_prefix` parameter which expects a `string`.
A parameter being optional, however, does not automatically make it nullable.
As of PHP 8.1, passing `null` to a non-nullable PHP native function will generate a deprecation notice.
In this case, these function calls yielded a `http_build_query(): Passing null to parameter #2 ($numeric_prefix) of type string is deprecated` notice.
Changing the `null` to an empty string fixes this without BC-break.
Fixes a few deprecation warnings found when running the tests.
Refs:
* https://www.php.net/manual/en/function.http-build-query.php
* https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg
* PHP 8.1: fix deprecation notices / PharData::__construct()
This fixes all relevant calls to the PHP native `PharData::__construct()` method.
The second parameter of this method is the _optional_ `$flags` parameter which expects an `int` of flags to be passed to the `Phar` parent class `RecursiveDirectoryIterator`.
Fixed by passing the default value for the `$flags` parameter as per the `RecursiveDirectoryIterator::__construct()` method.
The third parameter of the method is the _optional_ `$alias` parameter which expects an `string`.
Fixed by passing an empty string.
Fixes various notices along the lines of:
```
Deprecation triggered by Composer\Test\Package\Archiver\ArchiveManagerTest::testArchiveTar:
PharData::__construct(): Passing null to parameter #2 ($flags) of type int is deprecated
Stack trace:
0 [internal function]: Symfony\Bridge\PhpUnit\DeprecationErrorHandler->handleError(8192, '...', '...', 55)
1 src/Composer/Package/Archiver/PharArchiver.php(55): PharData->__construct('...', NULL, NULL, 2)
2 src/Composer/Package/Archiver/ArchiveManager.php(193): Composer\Package\Archiver\PharArchiver->archive('...', '...', '...', Array, false)
3 tests/Composer/Test/Package/Archiver/ArchiveManagerTest.php(65): Composer\Package\Archiver\ArchiveManager->archive(Object(Composer\Package\CompletePackage), '...', '...')
...
```
Refs:
* https://www.php.net/manual/en/phardata.construct.php
* https://www.php.net/manual/en/recursivedirectoryiterator.construct.php
Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
2021-08-11 11:05:45 +00:00
|
|
|
$tokens = preg_split('#(?:^|\n*)--([A-Z-]+)--\n#', file_get_contents($file), -1, PREG_SPLIT_DELIM_CAPTURE);
|
2012-08-13 23:16:40 +00:00
|
|
|
$data = array();
|
|
|
|
$section = null;
|
|
|
|
|
2019-07-24 00:39:40 +00:00
|
|
|
foreach ($tokens as $token) {
|
|
|
|
if ('' === $token && null === $section) {
|
2012-08-13 23:16:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle section headers.
|
|
|
|
if (null === $section) {
|
2019-07-24 00:39:40 +00:00
|
|
|
$section = $token;
|
2012-08-13 23:16:40 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:39:40 +00:00
|
|
|
$sectionData = $token;
|
2012-08-13 23:16:40 +00:00
|
|
|
|
|
|
|
// Allow sections to validate, or modify their section data.
|
|
|
|
switch ($section) {
|
|
|
|
case 'EXPECT-EXIT-CODE':
|
2017-03-08 14:07:29 +00:00
|
|
|
$sectionData = (int) $sectionData;
|
2016-08-15 04:30:20 +00:00
|
|
|
break;
|
2012-08-13 23:16:40 +00:00
|
|
|
|
2021-02-12 10:05:13 +00:00
|
|
|
case 'RUN':
|
2012-08-13 23:16:40 +00:00
|
|
|
case 'EXPECT':
|
|
|
|
case 'EXPECT-REGEX':
|
2021-02-12 10:05:13 +00:00
|
|
|
case 'EXPECT-REGEXES':
|
|
|
|
$sectionData = trim($sectionData);
|
2012-08-13 23:16:40 +00:00
|
|
|
break;
|
|
|
|
|
2021-02-15 13:07:58 +00:00
|
|
|
case 'TEST':
|
|
|
|
break;
|
|
|
|
|
2012-08-13 23:16:40 +00:00
|
|
|
default:
|
|
|
|
throw new \RuntimeException(sprintf(
|
2021-02-12 10:05:13 +00:00
|
|
|
'Unknown section "%s". Allowed sections: "RUN", "EXPECT", "EXPECT-EXIT-CODE", "EXPECT-REGEX", "EXPECT-REGEXES". '
|
2012-08-13 23:16:40 +00:00
|
|
|
.'Section headers must be written as "--HEADER_NAME--".',
|
2020-11-22 13:48:56 +00:00
|
|
|
$section
|
|
|
|
));
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data[$section] = $sectionData;
|
|
|
|
$section = $sectionData = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate data
|
|
|
|
if (!isset($data['RUN'])) {
|
|
|
|
throw new \RuntimeException('The test file must have a section named "RUN".');
|
|
|
|
}
|
2021-02-12 10:05:13 +00:00
|
|
|
if (!isset($data['EXPECT']) && !isset($data['EXPECT-REGEX']) && !isset($data['EXPECT-REGEXES'])) {
|
|
|
|
throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-REGEX", or "EXPECT-REGEXES".');
|
2012-08-13 23:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2021-10-15 14:26:22 +00:00
|
|
|
/**
|
|
|
|
* @param string $output
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-08-13 23:16:40 +00:00
|
|
|
private function cleanOutput($output)
|
|
|
|
{
|
|
|
|
$processed = '';
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($output); $i++) {
|
|
|
|
if ($output[$i] === "\x08") {
|
|
|
|
$processed = substr($processed, 0, -1);
|
|
|
|
} elseif ($output[$i] !== "\r") {
|
|
|
|
$processed .= $output[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $processed;
|
|
|
|
}
|
2012-08-18 14:22:15 +00:00
|
|
|
}
|