JsonConfigSourceTest.php 8.2 KB

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