JsonConfigSourceTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Json;
  12. use Composer\Config\JsonConfigSource;
  13. use Composer\Json\JsonFile;
  14. use Composer\Util\Filesystem;
  15. class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /** @var Filesystem */
  18. private $fs;
  19. /** @var string */
  20. private $workingDir;
  21. protected function fixturePath($name)
  22. {
  23. return __DIR__.'/Fixtures/'.$name;
  24. }
  25. protected function setUp()
  26. {
  27. $this->fs = new Filesystem;
  28. $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest';
  29. $this->fs->ensureDirectoryExists($this->workingDir);
  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 testRemoveRepository()
  46. {
  47. $config = $this->workingDir.'/composer.json';
  48. copy($this->fixturePath('config/config-with-exampletld-repository.json'), $config);
  49. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  50. $jsonConfigSource->removeRepository('example_tld');
  51. $this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
  52. }
  53. public function testAddPackagistRepositoryWithFalseValue()
  54. {
  55. $config = $this->workingDir.'/composer.json';
  56. copy($this->fixturePath('composer-repositories.json'), $config);
  57. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  58. $jsonConfigSource->addRepository('packagist', false);
  59. $this->assertFileEquals($this->fixturePath('config/config-with-packagist-false.json'), $config);
  60. }
  61. public function testRemovePackagist()
  62. {
  63. $config = $this->workingDir.'/composer.json';
  64. copy($this->fixturePath('config/config-with-packagist-false.json'), $config);
  65. $jsonConfigSource = new JsonConfigSource(new JsonFile($config));
  66. $jsonConfigSource->removeRepository('packagist');
  67. $this->assertFileEquals($this->fixturePath('composer-repositories.json'), $config);
  68. }
  69. /**
  70. * Test addLink()
  71. *
  72. * @param string $sourceFile Source file
  73. * @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
  74. * @param string $name Name
  75. * @param string $value Value
  76. * @param string $compareAgainst File to compare against after making changes
  77. *
  78. * @dataProvider provideAddLinkData
  79. */
  80. public function testAddLink($sourceFile, $type, $name, $value, $compareAgainst)
  81. {
  82. $composerJson = $this->workingDir.'/composer.json';
  83. copy($sourceFile, $composerJson);
  84. $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
  85. $jsonConfigSource->addLink($type, $name, $value);
  86. $this->assertFileEquals($compareAgainst, $composerJson);
  87. }
  88. /**
  89. * Test removeLink()
  90. *
  91. * @param string $sourceFile Source file
  92. * @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
  93. * @param string $name Name
  94. * @param string $compareAgainst File to compare against after making changes
  95. *
  96. * @dataProvider provideRemoveLinkData
  97. */
  98. public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
  99. {
  100. $composerJson = $this->workingDir.'/composer.json';
  101. copy($sourceFile, $composerJson);
  102. $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
  103. $jsonConfigSource->removeLink($type, $name);
  104. $this->assertFileEquals($compareAgainst, $composerJson);
  105. }
  106. protected function addLinkDataArguments($type, $name, $value, $fixtureBasename, $before)
  107. {
  108. return array(
  109. $before,
  110. $type,
  111. $name,
  112. $value,
  113. $this->fixturePath('addLink/'.$fixtureBasename.'.json'),
  114. );
  115. }
  116. /**
  117. * Provide data for testAddLink
  118. *
  119. * @return array
  120. */
  121. public function provideAddLinkData()
  122. {
  123. $empty = $this->fixturePath('composer-empty.json');
  124. $oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
  125. $twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
  126. return array(
  127. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-empty', $empty),
  128. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-oneOfEverything', $oneOfEverything),
  129. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-twoOfEverything', $twoOfEverything),
  130. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-empty', $empty),
  131. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-oneOfEverything', $oneOfEverything),
  132. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-twoOfEverything', $twoOfEverything),
  133. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-empty', $empty),
  134. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-oneOfEverything', $oneOfEverything),
  135. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-twoOfEverything', $twoOfEverything),
  136. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-empty', $empty),
  137. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-oneOfEverything', $oneOfEverything),
  138. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-twoOfEverything', $twoOfEverything),
  139. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-empty', $empty),
  140. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-oneOfEverything', $oneOfEverything),
  141. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-twoOfEverything', $twoOfEverything),
  142. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-empty', $empty),
  143. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-oneOfEverything', $oneOfEverything),
  144. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-twoOfEverything', $twoOfEverything),
  145. );
  146. }
  147. protected function removeLinkDataArguments($type, $name, $fixtureBasename, $after = null)
  148. {
  149. return array(
  150. $this->fixturePath('removeLink/'.$fixtureBasename.'.json'),
  151. $type,
  152. $name,
  153. $after ?: $this->fixturePath('removeLink/'.$fixtureBasename.'-after.json'),
  154. );
  155. }
  156. /**
  157. * Provide data for testRemoveLink
  158. *
  159. * @return array
  160. */
  161. public function provideRemoveLinkData()
  162. {
  163. $oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
  164. $twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
  165. return array(
  166. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-empty'),
  167. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-oneOfEverything', $oneOfEverything),
  168. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-twoOfEverything', $twoOfEverything),
  169. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-empty'),
  170. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-oneOfEverything', $oneOfEverything),
  171. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-twoOfEverything', $twoOfEverything),
  172. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-empty'),
  173. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-oneOfEverything', $oneOfEverything),
  174. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-twoOfEverything', $twoOfEverything),
  175. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-empty'),
  176. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-oneOfEverything', $oneOfEverything),
  177. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-twoOfEverything', $twoOfEverything),
  178. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-empty'),
  179. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-oneOfEverything', $oneOfEverything),
  180. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-twoOfEverything', $twoOfEverything),
  181. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-empty'),
  182. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-oneOfEverything', $oneOfEverything),
  183. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-twoOfEverything', $twoOfEverything),
  184. );
  185. }
  186. }