2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-11-14 17:03:11 +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.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 14:23:32 +00:00
|
|
|
namespace Composer\Test\Config;
|
2012-11-14 17:03:11 +00:00
|
|
|
|
|
|
|
use Composer\Config\JsonConfigSource;
|
|
|
|
use Composer\Json\JsonFile;
|
2018-11-12 14:23:32 +00:00
|
|
|
use Composer\Test\TestCase;
|
2012-11-14 17:03:11 +00:00
|
|
|
use Composer\Util\Filesystem;
|
|
|
|
|
2016-01-21 12:01:55 +00:00
|
|
|
class JsonConfigSourceTest extends TestCase
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
2015-02-16 14:58:58 +00:00
|
|
|
/** @var Filesystem */
|
|
|
|
private $fs;
|
|
|
|
/** @var string */
|
2012-11-14 17:03:11 +00:00
|
|
|
private $workingDir;
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
protected function fixturePath(string $name): string
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
|
|
|
return __DIR__.'/Fixtures/'.$name;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
|
|
|
$this->fs = new Filesystem;
|
2022-05-11 14:05:35 +00:00
|
|
|
$this->workingDir = self::getUniqueTmpDirectory();
|
2012-11-14 17:03:11 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function tearDown(): void
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
parent::tearDown();
|
2012-11-14 17:03:11 +00:00
|
|
|
if (is_dir($this->workingDir)) {
|
|
|
|
$this->fs->removeDirectory($this->workingDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddRepository(): void
|
2015-02-16 14:58:58 +00:00
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
2022-08-17 12:20:07 +00:00
|
|
|
$jsonConfigSource->addRepository('example_tld', ['type' => 'git', 'url' => 'example.tld']);
|
2015-02-16 14:58:58 +00:00
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddRepositoryWithOptions(): void
|
2016-01-22 10:08:00 +00:00
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
2022-08-17 12:20:07 +00:00
|
|
|
$jsonConfigSource->addRepository('example_tld', [
|
2016-01-22 10:08:00 +00:00
|
|
|
'type' => 'composer',
|
|
|
|
'url' => 'https://example.tld',
|
2022-08-17 12:20:07 +00:00
|
|
|
'options' => [
|
|
|
|
'ssl' => [
|
2016-01-26 13:07:05 +00:00
|
|
|
'local_cert' => '/home/composer/.ssl/composer.pem',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
2016-01-22 10:08:00 +00:00
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository-and-options.json'), $config);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testRemoveRepository(): void
|
2015-02-16 14:58:58 +00:00
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
|
|
|
$jsonConfigSource->removeRepository('example_tld');
|
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddPackagistRepositoryWithFalseValue(): void
|
2015-02-16 14:58:58 +00:00
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
|
|
|
$jsonConfigSource->addRepository('packagist', false);
|
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('config/config-with-packagist-false.json'), $config);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testRemovePackagist(): void
|
2015-02-16 14:58:58 +00:00
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('config/config-with-packagist-false.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
|
|
|
$jsonConfigSource->removeRepository('packagist');
|
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test addLink()
|
|
|
|
*
|
|
|
|
* @param string $sourceFile Source file
|
|
|
|
* @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
|
|
|
|
* @param string $name Name
|
|
|
|
* @param string $value Value
|
|
|
|
* @param string $compareAgainst File to compare against after making changes
|
|
|
|
*
|
|
|
|
* @dataProvider provideAddLinkData
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddLink(string $sourceFile, string $type, string $name, string $value, string $compareAgainst): void
|
2015-02-16 14:58:58 +00:00
|
|
|
{
|
|
|
|
$composerJson = $this->workingDir.'/composer.json';
|
|
|
|
copy($sourceFile, $composerJson);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
|
|
|
|
|
|
|
|
$jsonConfigSource->addLink($type, $name, $value);
|
|
|
|
|
|
|
|
$this->assertFileEquals($compareAgainst, $composerJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test removeLink()
|
|
|
|
*
|
|
|
|
* @param string $sourceFile Source file
|
|
|
|
* @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
|
|
|
|
* @param string $name Name
|
|
|
|
* @param string $compareAgainst File to compare against after making changes
|
|
|
|
*
|
|
|
|
* @dataProvider provideRemoveLinkData
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testRemoveLink(string $sourceFile, string $type, string $name, string $compareAgainst): void
|
2015-02-16 14:58:58 +00:00
|
|
|
{
|
|
|
|
$composerJson = $this->workingDir.'/composer.json';
|
|
|
|
copy($sourceFile, $composerJson);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
|
|
|
|
|
|
|
|
$jsonConfigSource->removeLink($type, $name);
|
|
|
|
|
|
|
|
$this->assertFileEquals($compareAgainst, $composerJson);
|
|
|
|
}
|
|
|
|
|
2021-10-27 18:34:48 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @phpstan-return array{string, string, string, string, string}
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
protected function addLinkDataArguments(string $type, string $name, string $value, string $fixtureBasename, string $before): array
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2012-11-14 17:03:11 +00:00
|
|
|
$before,
|
|
|
|
$type,
|
|
|
|
$name,
|
|
|
|
$value,
|
|
|
|
$this->fixturePath('addLink/'.$fixtureBasename.'.json'),
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2012-11-14 17:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide data for testAddLink
|
|
|
|
*/
|
2022-02-21 12:42:28 +00:00
|
|
|
public function provideAddLinkData(): array
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
|
|
|
$empty = $this->fixturePath('composer-empty.json');
|
|
|
|
$oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
|
|
|
|
$twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2012-11-14 17:03:11 +00:00
|
|
|
$this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-empty', $empty),
|
|
|
|
$this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-empty', $empty),
|
|
|
|
$this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-empty', $empty),
|
|
|
|
$this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-empty', $empty),
|
|
|
|
$this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-empty', $empty),
|
|
|
|
$this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-empty', $empty),
|
|
|
|
$this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-twoOfEverything', $twoOfEverything),
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2012-11-14 17:03:11 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 18:34:48 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @phpstan-return array{string, string, string, string}
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
protected function removeLinkDataArguments(string $type, string $name, string $fixtureBasename, ?string $after = null): array
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2012-11-14 17:03:11 +00:00
|
|
|
$this->fixturePath('removeLink/'.$fixtureBasename.'.json'),
|
|
|
|
$type,
|
|
|
|
$name,
|
|
|
|
$after ?: $this->fixturePath('removeLink/'.$fixtureBasename.'-after.json'),
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2012-11-14 17:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide data for testRemoveLink
|
|
|
|
*/
|
2022-02-21 12:42:28 +00:00
|
|
|
public function provideRemoveLinkData(): array
|
2012-11-14 17:03:11 +00:00
|
|
|
{
|
|
|
|
$oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
|
|
|
|
$twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
2012-11-14 17:03:11 +00:00
|
|
|
$this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-empty'),
|
|
|
|
$this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-empty'),
|
|
|
|
$this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-empty'),
|
|
|
|
$this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-empty'),
|
|
|
|
$this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-empty'),
|
|
|
|
$this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-twoOfEverything', $twoOfEverything),
|
|
|
|
|
|
|
|
$this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-empty'),
|
|
|
|
$this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-oneOfEverything', $oneOfEverything),
|
|
|
|
$this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-twoOfEverything', $twoOfEverything),
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2012-11-14 17:03:11 +00:00
|
|
|
}
|
|
|
|
}
|