InstalledVersionsTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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;
  12. use Composer\Test\TestCase;
  13. use Composer\InstalledVersions;
  14. use Composer\Semver\VersionParser;
  15. class InstalledVersionsTest extends TestCase
  16. {
  17. public function setUp()
  18. {
  19. InstalledVersions::reload(require __DIR__.'/Repository/Fixtures/installed.php');
  20. }
  21. public function testGetInstalledPackages()
  22. {
  23. $names = array(
  24. '__root__',
  25. 'a/provider',
  26. 'a/provider2',
  27. 'b/replacer',
  28. 'c/c',
  29. 'foo/impl',
  30. 'foo/impl2',
  31. 'foo/replaced',
  32. );
  33. $this->assertSame($names, InstalledVersions::getInstalledPackages());
  34. }
  35. /**
  36. * @dataProvider isInstalledProvider
  37. */
  38. public function testIsInstalled($expected, $name, $constraint = null)
  39. {
  40. $this->assertSame($expected, InstalledVersions::isInstalled($name));
  41. }
  42. public static function isInstalledProvider()
  43. {
  44. return array(
  45. array(true, 'foo/impl'),
  46. array(true, 'foo/replaced'),
  47. array(true, 'c/c'),
  48. array(true, '__root__'),
  49. array(true, 'b/replacer'),
  50. array(false, 'not/there'),
  51. array(false, 'not/there', '^1.0'),
  52. );
  53. }
  54. /**
  55. * @dataProvider satisfiesProvider
  56. */
  57. public function testSatisfies($expected, $name, $constraint)
  58. {
  59. $this->assertSame($expected, InstalledVersions::satisfies(new VersionParser, $name, $constraint));
  60. }
  61. public static function satisfiesProvider()
  62. {
  63. return array(
  64. array(true, 'foo/impl', '1.5'),
  65. array(true, 'foo/impl', '1.2'),
  66. array(true, 'foo/impl', '^1.0'),
  67. array(true, 'foo/impl', '^3 || ^2'),
  68. array(false, 'foo/impl', '^3'),
  69. array(true, 'foo/replaced', '3.5'),
  70. array(true, 'foo/replaced', '^3.2'),
  71. array(false, 'foo/replaced', '4.0'),
  72. array(true, 'c/c', '3.0.0'),
  73. array(true, 'c/c', '^3'),
  74. array(false, 'c/c', '^3.1'),
  75. array(true, '__root__', 'dev-master'),
  76. array(true, '__root__', '^1.10'),
  77. array(false, '__root__', '^2'),
  78. array(true, 'b/replacer', '^2.1'),
  79. array(false, 'b/replacer', '^2.3'),
  80. array(true, 'a/provider2', '^1.2'),
  81. array(true, 'a/provider2', '^1.4'),
  82. array(false, 'a/provider2', '^1.5'),
  83. );
  84. }
  85. /**
  86. * @dataProvider getVersionRangesProvider
  87. */
  88. public function testGetVersionRanges($expected, $name)
  89. {
  90. $this->assertSame($expected, InstalledVersions::getVersionRanges($name));
  91. }
  92. public static function getVersionRangesProvider()
  93. {
  94. return array(
  95. array('dev-master || 1.10.x-dev', '__root__'),
  96. array('^1.1 || 1.2 || 1.4 || 2.0', 'foo/impl'),
  97. array('2.2 || 2.0', 'foo/impl2'),
  98. array('^3.0', 'foo/replaced'),
  99. array('1.1', 'a/provider'),
  100. array('1.2 || 1.4', 'a/provider2'),
  101. array('2.2', 'b/replacer'),
  102. array('3.0', 'c/c'),
  103. );
  104. }
  105. /**
  106. * @dataProvider getVersionProvider
  107. */
  108. public function testGetVersion($expected, $name)
  109. {
  110. $this->assertSame($expected, InstalledVersions::getVersion($name));
  111. }
  112. public static function getVersionProvider()
  113. {
  114. return array(
  115. array('dev-master', '__root__'),
  116. array(null, 'foo/impl'),
  117. array(null, 'foo/impl2'),
  118. array(null, 'foo/replaced'),
  119. array('1.1.0.0', 'a/provider'),
  120. array('1.2.0.0', 'a/provider2'),
  121. array('2.2.0.0', 'b/replacer'),
  122. array('3.0.0.0', 'c/c'),
  123. );
  124. }
  125. /**
  126. * @dataProvider getPrettyVersionProvider
  127. */
  128. public function testGetPrettyVersion($expected, $name)
  129. {
  130. $this->assertSame($expected, InstalledVersions::getPrettyVersion($name));
  131. }
  132. public static function getPrettyVersionProvider()
  133. {
  134. return array(
  135. array('dev-master', '__root__'),
  136. array(null, 'foo/impl'),
  137. array(null, 'foo/impl2'),
  138. array(null, 'foo/replaced'),
  139. array('1.1', 'a/provider'),
  140. array('1.2', 'a/provider2'),
  141. array('2.2', 'b/replacer'),
  142. array('3.0', 'c/c'),
  143. );
  144. }
  145. public function testGetVersionOutOfBounds()
  146. {
  147. $this->setExpectedException('OutOfBoundsException');
  148. InstalledVersions::getVersion('not/installed');
  149. }
  150. public function testGetRootPackage()
  151. {
  152. $this->assertSame(array(
  153. 'pretty_version' => 'dev-master',
  154. 'version' => 'dev-master',
  155. 'aliases' => array(
  156. '1.10.x-dev',
  157. ),
  158. 'reference' => 'sourceref-by-default',
  159. 'name' => '__root__',
  160. ), InstalledVersions::getRootPackage());
  161. }
  162. public function testGetRawData()
  163. {
  164. $this->assertSame(require __DIR__.'/Repository/Fixtures/installed.php', InstalledVersions::getRawData());
  165. }
  166. /**
  167. * @dataProvider getReferenceProvider
  168. */
  169. public function testGetReference($expected, $name)
  170. {
  171. $this->assertSame($expected, InstalledVersions::getReference($name));
  172. }
  173. public static function getReferenceProvider()
  174. {
  175. return array(
  176. array('sourceref-by-default', '__root__'),
  177. array(null, 'foo/impl'),
  178. array(null, 'foo/impl2'),
  179. array(null, 'foo/replaced'),
  180. array('distref-as-no-source', 'a/provider'),
  181. array('distref-as-installed-from-dist', 'a/provider2'),
  182. array(null, 'b/replacer'),
  183. array(null, 'c/c'),
  184. );
  185. }
  186. }