JsonConfigSourceTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. /**
  16. * JsonConfigSource Test
  17. *
  18. * @author Beau Simensen <beau@dflydev.com>
  19. */
  20. class JsonConfigSourceTest extends \PHPUnit_Framework_TestCase
  21. {
  22. private $workingDir;
  23. protected function fixturePath($name)
  24. {
  25. return __DIR__.'/Fixtures/'.$name;
  26. }
  27. protected function setUp()
  28. {
  29. $this->fs = new Filesystem;
  30. $this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest';
  31. $this->fs->ensureDirectoryExists($this->workingDir);
  32. }
  33. protected function tearDown()
  34. {
  35. if (is_dir($this->workingDir)) {
  36. $this->fs->removeDirectory($this->workingDir);
  37. }
  38. }
  39. protected function addLinkDataArguments($type, $name, $value, $fixtureBasename, $before)
  40. {
  41. return array(
  42. $before,
  43. $type,
  44. $name,
  45. $value,
  46. $this->fixturePath('addLink/'.$fixtureBasename.'.json'),
  47. );
  48. }
  49. /**
  50. * Provide data for testAddLink
  51. *
  52. * @return array
  53. */
  54. public function provideAddLinkData()
  55. {
  56. $empty = $this->fixturePath('composer-empty.json');
  57. $oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
  58. $twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
  59. return array(
  60. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-empty', $empty),
  61. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-oneOfEverything', $oneOfEverything),
  62. $this->addLinkDataArguments('require', 'my-vend/my-lib', '1.*', 'require-from-twoOfEverything', $twoOfEverything),
  63. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-empty', $empty),
  64. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-oneOfEverything', $oneOfEverything),
  65. $this->addLinkDataArguments('require-dev', 'my-vend/my-lib-tests', '1.*', 'require-dev-from-twoOfEverything', $twoOfEverything),
  66. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-empty', $empty),
  67. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-oneOfEverything', $oneOfEverything),
  68. $this->addLinkDataArguments('provide', 'my-vend/my-lib-interface', '1.*', 'provide-from-twoOfEverything', $twoOfEverything),
  69. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-empty', $empty),
  70. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-oneOfEverything', $oneOfEverything),
  71. $this->addLinkDataArguments('suggest', 'my-vend/my-optional-extension', '1.*', 'suggest-from-twoOfEverything', $twoOfEverything),
  72. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-empty', $empty),
  73. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-oneOfEverything', $oneOfEverything),
  74. $this->addLinkDataArguments('replace', 'my-vend/other-app', '1.*', 'replace-from-twoOfEverything', $twoOfEverything),
  75. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-empty', $empty),
  76. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-oneOfEverything', $oneOfEverything),
  77. $this->addLinkDataArguments('conflict', 'my-vend/my-old-app', '1.*', 'conflict-from-twoOfEverything', $twoOfEverything),
  78. );
  79. }
  80. /**
  81. * Test addLink()
  82. *
  83. * @param string $sourceFile Source file
  84. * @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
  85. * @param string $name Name
  86. * @param string $value Value
  87. * @param string $compareAgainst File to compare against after making changes
  88. *
  89. * @dataProvider provideAddLinkData
  90. */
  91. public function testAddLink($sourceFile, $type, $name, $value, $compareAgainst)
  92. {
  93. $composerJson = $this->workingDir.'/composer.json';
  94. copy($sourceFile, $composerJson);
  95. $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
  96. $jsonConfigSource->addLink($type, $name, $value);
  97. $this->assertFileEquals($compareAgainst, $composerJson);
  98. }
  99. protected function removeLinkDataArguments($type, $name, $fixtureBasename, $after = null)
  100. {
  101. return array(
  102. $this->fixturePath('removeLink/'.$fixtureBasename.'.json'),
  103. $type,
  104. $name,
  105. $after ?: $this->fixturePath('removeLink/'.$fixtureBasename.'-after.json'),
  106. );
  107. }
  108. /**
  109. * Provide data for testRemoveLink
  110. *
  111. * @return array
  112. */
  113. public function provideRemoveLinkData()
  114. {
  115. $oneOfEverything = $this->fixturePath('composer-one-of-everything.json');
  116. $twoOfEverything = $this->fixturePath('composer-two-of-everything.json');
  117. return array(
  118. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-empty'),
  119. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-oneOfEverything', $oneOfEverything),
  120. $this->removeLinkDataArguments('require', 'my-vend/my-lib', 'require-to-twoOfEverything', $twoOfEverything),
  121. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-empty'),
  122. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-oneOfEverything', $oneOfEverything),
  123. $this->removeLinkDataArguments('require-dev', 'my-vend/my-lib-tests', 'require-dev-to-twoOfEverything', $twoOfEverything),
  124. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-empty'),
  125. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-oneOfEverything', $oneOfEverything),
  126. $this->removeLinkDataArguments('provide', 'my-vend/my-lib-interface', 'provide-to-twoOfEverything', $twoOfEverything),
  127. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-empty'),
  128. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-oneOfEverything', $oneOfEverything),
  129. $this->removeLinkDataArguments('suggest', 'my-vend/my-optional-extension', 'suggest-to-twoOfEverything', $twoOfEverything),
  130. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-empty'),
  131. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-oneOfEverything', $oneOfEverything),
  132. $this->removeLinkDataArguments('replace', 'my-vend/other-app', 'replace-to-twoOfEverything', $twoOfEverything),
  133. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-empty'),
  134. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-oneOfEverything', $oneOfEverything),
  135. $this->removeLinkDataArguments('conflict', 'my-vend/my-old-app', 'conflict-to-twoOfEverything', $twoOfEverything),
  136. );
  137. }
  138. /**
  139. * Test removeLink()
  140. *
  141. * @param string $sourceFile Source file
  142. * @param string $type Type (require, require-dev, provide, suggest, replace, conflict)
  143. * @param string $name Name
  144. * @param string $compareAgainst File to compare against after making changes
  145. *
  146. * @dataProvider provideRemoveLinkData
  147. */
  148. public function testRemoveLink($sourceFile, $type, $name, $compareAgainst)
  149. {
  150. $composerJson = $this->workingDir.'/composer.json';
  151. copy($sourceFile, $composerJson);
  152. $jsonConfigSource = new JsonConfigSource(new JsonFile($composerJson));
  153. $jsonConfigSource->removeLink($type, $name);
  154. $this->assertFileEquals($compareAgainst, $composerJson);
  155. }
  156. }