JsonConfigSourceTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Config;
  12. use Composer\Config\JsonConfigSource;
  13. use Composer\Json\JsonFile;
  14. use Composer\Test\TestCase;
  15. use Composer\Util\Filesystem;
  16. class JsonConfigSourceTest extends TestCase
  17. {
  18. /** @var Filesystem */
  19. private $fs;
  20. /** @var string */
  21. private $workingDir;
  22. protected function fixturePath($name)
  23. {
  24. return __DIR__.'/Fixtures/'.$name;
  25. }
  26. protected function setUp()
  27. {
  28. $this->fs = new Filesystem;
  29. $this->workingDir = $this->getUniqueTmpDirectory();
  30. }
  31. protected function tearDown()
  32. {
  33. if (is_dir($this->workingDir)) {
  34. $this->fs->removeDirectory($this->workingDir);
  35. }
  36. }
  37. public function testAddRepository()
  38. {
  39. $config = $this->workingDir.'/composer.json';
  40. copy($this->fixturePath('composer-repositories.json'), $config);
  41. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  42. $jsonConfigSource->addRepository('example_tld', array('type' => 'git', 'url' => 'example.tld'));
  43. $this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
  44. }
  45. public function testAddRepositoryWithOptions()
  46. {
  47. $config = $this->workingDir.'/composer.json';
  48. copy($this->fixturePath('composer-repositories.json'), $config);
  49. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  50. $jsonConfigSource->addRepository('example_tld', array(
  51. 'type' => 'composer',
  52. 'url' => 'https://example.tld',
  53. 'options' => array(
  54. 'ssl' => array(
  55. 'local_cert' => '/home/composer/.ssl/composer.pem',
  56. ),
  57. ),
  58. ));
  59. $this->assertFileEquals($this->fixturePath('config/config-with-exampletld-repository-and-options.json'), $config);
  60. }
  61. public function testRemoveRepository()
  62. {
  63. $config = $this->workingDir.'/composer.json';
  64. copy($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
  65. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  66. $jsonConfigSource->removeRepository('example_tld');
  67. $this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
  68. }
  69. public function testAddPackagistRepositoryWithFalseValue()
  70. {
  71. $config = $this->workingDir.'/composer.json';
  72. copy($this->fixturePath('composer-repositories.json'), $config);
  73. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  74. $jsonConfigSource->addRepository('packagist', false);
  75. $this->assertFileEquals($this->fixturePath('config/config-with-packagist-false.json'), $config);
  76. }
  77. public function testRemovePackagist()
  78. {
  79. $config = $this->workingDir.'/composer.json';
  80. copy($this->fixturePath('config/config-with-packagist-false.json'), $config);
  81. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  82. $jsonConfigSource->removeRepository('packagist');
  83. $this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
  84. }
  85. /**
  86. * Test addLink()
  87. *
  88. * @param string $sourceFile Source file
  89. * @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
  90. * @param string $name Name
  91. * @param string $value Value
  92. * @param string $compareAgainst File to compare against after making changes
  93. *
  94. * @dataProvider provideAddLinkData
  95. */
  96. public function testAddLink($sourceFile, $type, $name, $value, $compareAgainst)
  97. {
  98. $composerJson = $this->workingDir.'/composer.json';
  99. copy($sourceFile, $composerJson);
  100. $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
  101. $jsonConfigSource->addLink($type, $name, $value);
  102. $this->assertFileEquals($compareAgainst, $composerJson);
  103. }
  104. /**
  105. * Test removeLink()
  106. *
  107. * @param string $sourceFile Source file
  108. * @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
  109. * @param string $name Name
  110. * @param string $compareAgainst File to compare against after making changes
  111. *
  112. * @dataProvider provideRemoveLinkData
  113. */
  114. public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
  115. {
  116. $composerJson = $this->workingDir.'/composer.json';
  117. copy($sourceFile, $composerJson);
  118. $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
  119. $jsonConfigSource->removeLink($type, $name);
  120. $this->assertFileEquals($compareAgainst, $composerJson);
  121. }
  122. protected function addLinkDataArguments($type, $name, $value, $fixtureBasename, $before)
  123. {
  124. return array(
  125. $before,
  126. $type,
  127. $name,
  128. $value,
  129. $this->fixturePath('addLink/'.$fixtureBasename.'.json'),
  130. );
  131. }
  132. /**
  133. * Provide data for testAddLink
  134. *
  135. * @return array
  136. */
  137. public function provideAddLinkData()
  138. {
  139. $empty = $this->fixturePath('composer-empty.json');
  140. $oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
  141. $twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
  142. return array(
  143. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-empty', $empty),
  144. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-oneOfEverything', $oneOfEverything),
  145. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-twoOfEverything', $twoOfEverything),
  146. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-empty', $empty),
  147. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-oneOfEverything', $oneOfEverything),
  148. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-twoOfEverything', $twoOfEverything),
  149. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-empty', $empty),
  150. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-oneOfEverything', $oneOfEverything),
  151. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-twoOfEverything', $twoOfEverything),
  152. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-empty', $empty),
  153. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-oneOfEverything', $oneOfEverything),
  154. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-twoOfEverything', $twoOfEverything),
  155. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-empty', $empty),
  156. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-oneOfEverything', $oneOfEverything),
  157. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-twoOfEverything', $twoOfEverything),
  158. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-empty', $empty),
  159. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-oneOfEverything', $oneOfEverything),
  160. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-twoOfEverything', $twoOfEverything),
  161. );
  162. }
  163. protected function removeLinkDataArguments($type, $name, $fixtureBasename, $after = null)
  164. {
  165. return array(
  166. $this->fixturePath('removeLink/'.$fixtureBasename.'.json'),
  167. $type,
  168. $name,
  169. $after ?: $this->fixturePath('removeLink/'.$fixtureBasename.'-after.json'),
  170. );
  171. }
  172. /**
  173. * Provide data for testRemoveLink
  174. *
  175. * @return array
  176. */
  177. public function provideRemoveLinkData()
  178. {
  179. $oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
  180. $twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
  181. return array(
  182. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-empty'),
  183. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-oneOfEverything', $oneOfEverything),
  184. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-twoOfEverything', $twoOfEverything),
  185. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-empty'),
  186. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-oneOfEverything', $oneOfEverything),
  187. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-twoOfEverything', $twoOfEverything),
  188. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-empty'),
  189. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-oneOfEverything', $oneOfEverything),
  190. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-twoOfEverything', $twoOfEverything),
  191. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-empty'),
  192. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-oneOfEverything', $oneOfEverything),
  193. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-twoOfEverything', $twoOfEverything),
  194. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-empty'),
  195. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-oneOfEverything', $oneOfEverything),
  196. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-twoOfEverything', $twoOfEverything),
  197. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-empty'),
  198. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-oneOfEverything', $oneOfEverything),
  199. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-twoOfEverything', $twoOfEverything),
  200. );
  201. }
  202. }