2012-11-14 17:03:11 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2021-10-27 18:34:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-11-14 17:03:11 +00:00
|
|
|
protected function fixturePath($name)
|
|
|
|
{
|
|
|
|
return __DIR__.'/Fixtures/'.$name;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->fs = new Filesystem;
|
2016-01-21 12:01:55 +00:00
|
|
|
$this->workingDir = $this->getUniqueTmpDirectory();
|
2012-11-14 17:03:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
if (is_dir($this->workingDir)) {
|
|
|
|
$this->fs->removeDirectory($this->workingDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-16 14:58:58 +00:00
|
|
|
public function testAddRepository()
|
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
|
|
|
$jsonConfigSource->addRepository('example_tld', array('type' => 'git', 'url' => 'example.tld'));
|
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
|
|
|
|
}
|
|
|
|
|
2016-01-22 10:08:00 +00:00
|
|
|
public function testAddRepositoryWithOptions()
|
|
|
|
{
|
|
|
|
$config = $this->workingDir.'/composer.json';
|
|
|
|
copy($this->fixturePath('composer-repositories.json'), $config);
|
|
|
|
$jsonConfigSource = new JsonConfigSource(new JsonFile($config));
|
|
|
|
$jsonConfigSource->addRepository('example_tld', array(
|
|
|
|
'type' => 'composer',
|
|
|
|
'url' => 'https://example.tld',
|
|
|
|
'options' => array(
|
|
|
|
'ssl' => array(
|
2016-01-26 13:07:05 +00:00
|
|
|
'local_cert' => '/home/composer/.ssl/composer.pem',
|
|
|
|
),
|
|
|
|
),
|
2016-01-22 10:08:00 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository-and-options.json'), $config);
|
|
|
|
}
|
|
|
|
|
2015-02-16 14:58:58 +00:00
|
|
|
public function testRemoveRepository()
|
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddPackagistRepositoryWithFalseValue()
|
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRemovePackagist()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testAddLink($sourceFile, $type, $name, $value, $compareAgainst)
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
* @param string $fixtureBasename
|
|
|
|
* @param string $before
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @phpstan-return array{string, string, string, string, string}
|
|
|
|
*/
|
2012-11-14 17:03:11 +00:00
|
|
|
protected function addLinkDataArguments($type, $name, $value, $fixtureBasename, $before)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
$before,
|
|
|
|
$type,
|
|
|
|
$name,
|
|
|
|
$value,
|
|
|
|
$this->fixturePath('addLink/'.$fixtureBasename.'.json'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide data for testAddLink
|
|
|
|
*/
|
|
|
|
public function provideAddLinkData()
|
|
|
|
{
|
|
|
|
$empty = $this->fixturePath('composer-empty.json');
|
|
|
|
$oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
|
|
|
|
$twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-27 18:34:48 +00:00
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @param string $name
|
|
|
|
* @param string $fixtureBasename
|
|
|
|
* @param string|null $after
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*
|
|
|
|
* @phpstan-return array{string, string, string, string}
|
|
|
|
*/
|
2012-11-14 17:03:11 +00:00
|
|
|
protected function removeLinkDataArguments($type, $name, $fixtureBasename, $after = null)
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
$this->fixturePath('removeLink/'.$fixtureBasename.'.json'),
|
|
|
|
$type,
|
|
|
|
$name,
|
|
|
|
$after ?: $this->fixturePath('removeLink/'.$fixtureBasename.'-after.json'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide data for testRemoveLink
|
|
|
|
*/
|
|
|
|
public function provideRemoveLinkData()
|
|
|
|
{
|
|
|
|
$oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
|
|
|
|
$twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
|
|
|
|
|
|
|
|
return array(
|
|
|
|
$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),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|