1
0
Fork 0

Test fixes

pull/10343/head
Jordi Boggiano 2021-12-10 13:14:04 +01:00
parent 4f7d06e2a7
commit ccd87c479c
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
9 changed files with 49 additions and 70 deletions

View File

@ -124,18 +124,10 @@ class AllFunctionalTest extends TestCase
'COMPOSER_CACHE_DIR' => $this->testDir.'cache', 'COMPOSER_CACHE_DIR' => $this->testDir.'cache',
); );
// TODO in v2.3 always call with an array $proc = Process::fromShellCommandline(escapeshellcmd(PHP_BINARY).' '.escapeshellarg(self::$pharPath).' --no-ansi '.$testData['RUN'], $this->testDir, $env, null, 300);
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);
}
$output = ''; $output = '';
$exitcode = $proc->run(function ($type, $buffer) use (&$output) { $exitCode = $proc->run(function ($type, $buffer) use (&$output) {
$output .= $buffer; $output .= $buffer;
}); });
@ -177,16 +169,16 @@ class AllFunctionalTest extends TestCase
} }
} }
if (isset($testData['EXPECT-REGEX'])) { if (isset($testData['EXPECT-REGEX'])) {
$this->assertRegExp($testData['EXPECT-REGEX'], $this->cleanOutput($output)); $this->assertMatchesRegularExpression($testData['EXPECT-REGEX'], $this->cleanOutput($output));
} }
if (isset($testData['EXPECT-REGEXES'])) { if (isset($testData['EXPECT-REGEXES'])) {
$cleanOutput = $this->cleanOutput($output); $cleanOutput = $this->cleanOutput($output);
foreach (explode("\n", $testData['EXPECT-REGEXES']) as $regex) { foreach (explode("\n", $testData['EXPECT-REGEXES']) as $regex) {
$this->assertRegExp($regex, $cleanOutput, 'Output: '.$output); $this->assertMatchesRegularExpression($regex, $cleanOutput, 'Output: '.$output);
} }
} }
if (isset($testData['EXPECT-EXIT-CODE'])) { if (isset($testData['EXPECT-EXIT-CODE'])) {
$this->assertSame($testData['EXPECT-EXIT-CODE'], $exitcode); $this->assertSame($testData['EXPECT-EXIT-CODE'], $exitCode);
} }
} }
@ -205,7 +197,7 @@ class AllFunctionalTest extends TestCase
/** /**
* @param string $file * @param string $file
* @return array<string, int|string> * @return array{RUN: string, EXPECT?: string, EXPECT-EXIT-CODE?: int, EXPECT-REGEX?: string, EXPECT-REGEXES?: string, TEST?: string}
*/ */
private function parseTestFile($file) private function parseTestFile($file)
{ {
@ -262,7 +254,7 @@ class AllFunctionalTest extends TestCase
throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-REGEX", or "EXPECT-REGEXES".'); throw new \RuntimeException('The test file must have a section named "EXPECT", "EXPECT-REGEX", or "EXPECT-REGEXES".');
} }
return $data; return $data; // @phpstan-ignore-line
} }
/** /**

View File

@ -1861,38 +1861,16 @@ EOF;
/** /**
* @param string $expected * @param string $expected
* @param string $actual * @param string $actual
* @param string $message * @param string|null $message
* @param bool $canonicalize
* @param bool $ignoreCase
* *
* @return void * @return void
*/ */
public static function assertFileContentEquals($expected, $actual, $message = '', $canonicalize = false, $ignoreCase = false) public static function assertFileContentEquals(string $expected, string $actual, ?string $message = null)
{ {
self::assertEqualsNormalized( self::assertSame(
file_get_contents($expected), str_replace("\r", '', (string) file_get_contents($expected)),
file_get_contents($actual), str_replace("\r", '', (string) file_get_contents($actual)),
$message ?: $expected.' equals '.$actual, $message ?? $expected.' equals '.$actual
0,
10,
$canonicalize,
$ignoreCase
); );
} }
/**
* @param string $expected
* @param string $actual
* @param string $message
* @param int $delta
* @param int $maxDepth
* @param bool $canonicalize
* @param bool $ignoreCase
*
* @return void
*/
public static function assertEqualsNormalized($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
{
parent::assertEquals(str_replace("\r", '', $expected), str_replace("\r", '', $actual), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
}
} }

View File

@ -85,7 +85,7 @@ class FileDownloaderTest extends TestCase
->will($this->returnValue(array('url'))) ->will($this->returnValue(array('url')))
; ;
$path = tempnam($this->getUniqueTmpDirectory(), 'c'); $path = $this->createTempFile($this->getUniqueTmpDirectory());
$downloader = $this->getDownloader(); $downloader = $this->getDownloader();
try { try {

View File

@ -8,7 +8,7 @@ Checks that package versions in InstalledVersions are correct on initial install
update update
--EXPECT-- --EXPECT--
> Hooks::preUpdate > Hooks::preUpdate
!!PreUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process"] !!PreUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string"]
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
Loading composer repositories with package information Loading composer repositories with package information
Updating dependencies Updating dependencies
@ -26,12 +26,12 @@ Package operations: 6 installs, 0 updates, 0 removals%(\nAs there is no 'unzip'
- Downloading symfony/filesystem (%v?[2-8]\.\d+\.\d+%) - Downloading symfony/filesystem (%v?[2-8]\.\d+\.\d+%)
- Installing symfony/console (99999.1.2): Symlinking from symfony-console - Installing symfony/console (99999.1.2): Symlinking from symfony-console
- Installing plugin/a (1.1.1): Symlinking from plugin-a - Installing plugin/a (1.1.1): Symlinking from plugin-a
!!PluginAInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","root/pkg"] !!PluginAInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","root/pkg"]
!!PluginA:null !!PluginA:null
!!PluginB:null !!PluginB:null
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
- Installing plugin/b (2.2.2): Symlinking from plugin-b - Installing plugin/b (2.2.2): Symlinking from plugin-b
!!PluginBInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","root/pkg"] !!PluginBInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","root/pkg"]
!!PluginA:1.1.1.0 !!PluginA:1.1.1.0
!!PluginB:null !!PluginB:null
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
@ -42,7 +42,7 @@ Generating autoload files
2 packages you are using are looking for funding. 2 packages you are using are looking for funding.
Use the `composer fund` command to find out more! Use the `composer fund` command to find out more!
> Hooks::postUpdate > Hooks::postUpdate
!!PostUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PostUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
--EXPECT-EXIT-CODE-- --EXPECT-EXIT-CODE--

View File

@ -7,14 +7,14 @@ Checks that package versions in InstalledVersions are correct during an upgrade.
--RUN-- --RUN--
update plugin/* symfony/console symfony/filesystem symfony/process update plugin/* symfony/console symfony/filesystem symfony/process
--EXPECT-- --EXPECT--
!!PluginA:1.1.1.0["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PluginA:1.1.1.0["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!PluginB:2.2.2.0 !!PluginB:2.2.2.0
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
!!PluginB:2.2.2.0["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PluginB:2.2.2.0["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!PluginA:1.1.1.0 !!PluginA:1.1.1.0
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
> Hooks::preUpdate > Hooks::preUpdate
!!PreUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PreUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
Loading composer repositories with package information Loading composer repositories with package information
Updating dependencies Updating dependencies
@ -30,12 +30,12 @@ Package operations: 0 installs, 5 updates, 0 removals%(\nAs there is no 'unzip'
- Downloading symfony/filesystem (%v?[2-8]\.\d+\.\d+%) - Downloading symfony/filesystem (%v?[2-8]\.\d+\.\d+%)
- Upgrading symfony/console (99999.1.2 => 99999.1.3): Mirroring from symfony-console - Upgrading symfony/console (99999.1.2 => 99999.1.3): Mirroring from symfony-console
- Upgrading plugin/a (1.1.1 => 1.1.2): Mirroring from plugin-a - Upgrading plugin/a (1.1.1 => 1.1.2): Mirroring from plugin-a
!!PluginAInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PluginAInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!PluginA:1.1.1.0 !!PluginA:1.1.1.0
!!PluginB:2.2.2.0 !!PluginB:2.2.2.0
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
- Upgrading plugin/b (2.2.2 => 2.2.3): Mirroring from plugin-b - Upgrading plugin/b (2.2.2 => 2.2.3): Mirroring from plugin-b
!!PluginBInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PluginBInit["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!PluginA:1.1.2.0 !!PluginA:1.1.2.0
!!PluginB:2.2.2.0 !!PluginB:2.2.2.0
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
@ -45,7 +45,7 @@ Generating autoload files
2 packages you are using are looking for funding. 2 packages you are using are looking for funding.
Use the `composer fund` command to find out more! Use the `composer fund` command to find out more!
> Hooks::postUpdate > Hooks::postUpdate
!!PostUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/log","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/debug","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-mbstring","symfony/process","plugin/a","plugin/b","root/pkg"] !!PostUpdate:["composer/ca-bundle","composer/composer","composer/metadata-minifier","composer/pcre","composer/semver","composer/spdx-licenses","composer/xdebug-handler","justinrainbow/json-schema","psr/container","psr/log","psr/log-implementation","react/promise","seld/jsonlint","seld/phar-utils","symfony/console","symfony/deprecation-contracts","symfony/filesystem","symfony/finder","symfony/polyfill-ctype","symfony/polyfill-intl-grapheme","symfony/polyfill-intl-normalizer","symfony/polyfill-mbstring","symfony/polyfill-php73","symfony/polyfill-php80","symfony/process","symfony/service-contracts","symfony/string","plugin/a","plugin/b","root/pkg"]
!!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0% !!Versions:console:%[2-8]\.\d+\.\d+.0%;process:%[2-8]\.\d+\.\d+.0%;filesystem:%[2-8]\.\d+\.\d+.0%
!!PluginA:1.1.2.0 !!PluginA:1.1.2.0
!!PluginB:2.2.3.0 !!PluginB:2.2.3.0

View File

@ -99,7 +99,7 @@ class JsonFileTest extends TestCase
public function testSchemaValidationError() public function testSchemaValidationError()
{ {
$file = tempnam(sys_get_temp_dir(), 'c'); $file = $this->createTempFile();
file_put_contents($file, '{ "name": null }'); file_put_contents($file, '{ "name": null }');
$json = new JsonFile($file); $json = new JsonFile($file);
$expectedMessage = sprintf('"%s" does not match the expected JSON schema', $file); $expectedMessage = sprintf('"%s" does not match the expected JSON schema', $file);
@ -123,7 +123,7 @@ class JsonFileTest extends TestCase
public function testSchemaValidationLaxAdditionalProperties() public function testSchemaValidationLaxAdditionalProperties()
{ {
$file = tempnam(sys_get_temp_dir(), 'c'); $file = $this->createTempFile();
file_put_contents($file, '{ "name": "vendor/package", "description": "generic description", "foo": "bar" }'); file_put_contents($file, '{ "name": "vendor/package", "description": "generic description", "foo": "bar" }');
$json = new JsonFile($file); $json = new JsonFile($file);
try { try {
@ -139,7 +139,7 @@ class JsonFileTest extends TestCase
public function testSchemaValidationLaxRequired() public function testSchemaValidationLaxRequired()
{ {
$file = tempnam(sys_get_temp_dir(), 'c'); $file = $this->createTempFile();
$json = new JsonFile($file); $json = new JsonFile($file);
$expectedMessage = sprintf('"%s" does not match the expected JSON schema', $file); $expectedMessage = sprintf('"%s" does not match the expected JSON schema', $file);
@ -209,10 +209,10 @@ class JsonFileTest extends TestCase
public function testCustomSchemaValidationLax() public function testCustomSchemaValidationLax()
{ {
$file = tempnam(sys_get_temp_dir(), 'c'); $file = $this->createTempFile();
file_put_contents($file, '{ "custom": "property", "another custom": "property" }'); file_put_contents($file, '{ "custom": "property", "another custom": "property" }');
$schema = tempnam(sys_get_temp_dir(), 'c'); $schema = $this->createTempFile();
file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}'); file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}');
$json = new JsonFile($file); $json = new JsonFile($file);
@ -225,10 +225,10 @@ class JsonFileTest extends TestCase
public function testCustomSchemaValidationStrict() public function testCustomSchemaValidationStrict()
{ {
$file = tempnam(sys_get_temp_dir(), 'c'); $file = $this->createTempFile();
file_put_contents($file, '{ "custom": "property" }'); file_put_contents($file, '{ "custom": "property" }');
$schema = tempnam(sys_get_temp_dir(), 'c'); $schema = $this->createTempFile();
file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}'); file_put_contents($schema, '{ "properties": { "custom": { "type": "string" }}}');
$json = new JsonFile($file); $json = new JsonFile($file);

View File

@ -12,6 +12,7 @@
namespace Composer\Test\Package; namespace Composer\Test\Package;
use Composer\Json\JsonFile;
use Composer\Package\Locker; use Composer\Package\Locker;
use Composer\Plugin\PluginInterface; use Composer\Plugin\PluginInterface;
use Composer\IO\NullIO; use Composer\IO\NullIO;
@ -282,16 +283,13 @@ class LockerTest extends TestCase
*/ */
private function createPackageMock() private function createPackageMock()
{ {
return $this->getMockBuilder('Composer\Package\PackageInterface') return $this->getMockBuilder('Composer\Package\PackageInterface')->getMock();
->getMock();
} }
/** /**
* @param array<string, string> $customData * @param array<string, string> $customData
*
* @return false|string
*/ */
private function getJsonContent(array $customData = array()) private function getJsonContent(array $customData = array()): string
{ {
$data = array_merge(array( $data = array_merge(array(
'minimum-stability' => 'beta', 'minimum-stability' => 'beta',
@ -300,6 +298,6 @@ class LockerTest extends TestCase
ksort($data); ksort($data);
return json_encode($data); return JsonFile::encode($data, 0);
} }
} }

View File

@ -243,4 +243,15 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
return $mock; return $mock;
} }
protected function createTempFile(?string $dir = null): string
{
$dir = $dir ?? sys_get_temp_dir();
$name = tempnam($dir, 'c');
if ($name === false) {
throw new \UnexpectedValueException('tempnam failed to create a temporary file in '.$dir);
}
return $name;
}
} }

View File

@ -163,7 +163,7 @@ class RemoteFilesystemTest extends TestCase
{ {
$fs = new RemoteFilesystem($this->getIOInterfaceMock(), $this->getConfigMock()); $fs = new RemoteFilesystem($this->getIOInterfaceMock(), $this->getConfigMock());
$file = tempnam(sys_get_temp_dir(), 'c'); $file = $this->createTempFile();
$this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file)); $this->assertTrue($fs->copy('http://example.org', 'file://'.__FILE__, $file));
$this->assertFileExists($file); $this->assertFileExists($file);
$this->assertStringContainsString('testCopy', file_get_contents($file)); $this->assertStringContainsString('testCopy', file_get_contents($file));
@ -182,7 +182,7 @@ class RemoteFilesystemTest extends TestCase
return ''; return '';
}); });
$file = tempnam(sys_get_temp_dir(), 'z'); $file = $this->createTempFile();
unlink($file); unlink($file);
$fs->copy( $fs->copy(
@ -221,7 +221,7 @@ class RemoteFilesystemTest extends TestCase
} }
}); });
$file = tempnam(sys_get_temp_dir(), 'z'); $file = $this->createTempFile();
$copyResult = $fs->copy( $copyResult = $fs->copy(
'http://example.org', 'http://example.org',