VersionSelectorTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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\Package\Version;
  12. use Composer\Package\Version\VersionSelector;
  13. use Composer\Package\Package;
  14. use Composer\Package\Link;
  15. use Composer\Repository\PlatformRepository;
  16. use Composer\Semver\VersionParser;
  17. use Composer\Test\TestCase;
  18. class VersionSelectorTest extends TestCase
  19. {
  20. // A) multiple versions, get the latest one
  21. // B) targetPackageVersion will pass to repo set
  22. // C) No results, throw exception
  23. public function testLatestVersionIsReturned()
  24. {
  25. $packageName = 'foobar';
  26. $package1 = $this->createPackage('1.2.1');
  27. $package2 = $this->createPackage('1.2.2');
  28. $package3 = $this->createPackage('1.2.0');
  29. $packages = array($package1, $package2, $package3);
  30. $repositorySet = $this->createMockRepositorySet();
  31. $repositorySet->expects($this->once())
  32. ->method('findPackages')
  33. ->with($packageName, null)
  34. ->will($this->returnValue($packages));
  35. $versionSelector = new VersionSelector($repositorySet);
  36. $best = $versionSelector->findBestCandidate($packageName);
  37. // 1.2.2 should be returned because it's the latest of the returned versions
  38. $this->assertSame($package2, $best, 'Latest version should be 1.2.2');
  39. }
  40. public function testLatestVersionIsReturnedThatMatchesPhpRequirements()
  41. {
  42. $packageName = 'foobar';
  43. $platform = new PlatformRepository(array(), array('php' => '5.5.0'));
  44. $repositorySet = $this->createMockRepositorySet();
  45. $versionSelector = new VersionSelector($repositorySet, $platform);
  46. $parser = new VersionParser;
  47. $package1 = $this->createPackage('1.0.0');
  48. $package1->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.4'), 'requires', '>=5.4')));
  49. $package2 = $this->createPackage('2.0.0');
  50. $package2->setRequires(array('php' => new Link($packageName, 'php', $parser->parseConstraints('>=5.6'), 'requires', '>=5.6')));
  51. $packages = array($package1, $package2);
  52. $repositorySet->expects($this->any())
  53. ->method('findPackages')
  54. ->with($packageName, null)
  55. ->will($this->returnValue($packages));
  56. $best = $versionSelector->findBestCandidate($packageName);
  57. $this->assertSame($package1, $best, 'Latest version supporting php 5.5 should be returned (1.0.0)');
  58. $best = $versionSelector->findBestCandidate($packageName, null, 'stable', true);
  59. $this->assertSame($package2, $best, 'Latest version should be returned when ignoring platform reqs (2.0.0)');
  60. }
  61. public function testLatestVersionIsReturnedThatMatchesExtRequirements()
  62. {
  63. $packageName = 'foobar';
  64. $platform = new PlatformRepository(array(), array('ext-zip' => '5.3.0'));
  65. $repositorySet = $this->createMockRepositorySet();
  66. $versionSelector = new VersionSelector($repositorySet, $platform);
  67. $parser = new VersionParser;
  68. $package1 = $this->createPackage('1.0.0');
  69. $package1->setRequires(array('ext-zip' => new Link($packageName, 'ext-zip', $parser->parseConstraints('^5.2'), 'requires', '^5.2')));
  70. $package2 = $this->createPackage('2.0.0');
  71. $package2->setRequires(array('ext-zip' => new Link($packageName, 'ext-zip', $parser->parseConstraints('^5.4'), 'requires', '^5.4')));
  72. $packages = array($package1, $package2);
  73. $repositorySet->expects($this->any())
  74. ->method('findPackages')
  75. ->with($packageName, null)
  76. ->will($this->returnValue($packages));
  77. $best = $versionSelector->findBestCandidate($packageName);
  78. $this->assertSame($package1, $best, 'Latest version supporting ext-zip 5.3.0 should be returned (1.0.0)');
  79. $best = $versionSelector->findBestCandidate($packageName, null, 'stable', true);
  80. $this->assertSame($package2, $best, 'Latest version should be returned when ignoring platform reqs (2.0.0)');
  81. }
  82. public function testLatestVersionIsReturnedThatMatchesComposerRequirements()
  83. {
  84. $packageName = 'foobar';
  85. $platform = new PlatformRepository(array(), array('composer-runtime-api' => '1.0.0'));
  86. $repositorySet = $this->createMockRepositorySet();
  87. $versionSelector = new VersionSelector($repositorySet, $platform);
  88. $parser = new VersionParser;
  89. $package1 = $this->createPackage('1.0.0');
  90. $package1->setRequires(array('composer-runtime-api' => new Link($packageName, 'composer-runtime-api', $parser->parseConstraints('^1.0'), 'requires', '^1.0')));
  91. $package2 = $this->createPackage('1.1.0');
  92. $package2->setRequires(array('composer-runtime-api' => new Link($packageName, 'composer-runtime-api', $parser->parseConstraints('^2.0'), 'requires', '^2.0')));
  93. $packages = array($package1, $package2);
  94. $repositorySet->expects($this->any())
  95. ->method('findPackages')
  96. ->with($packageName, null)
  97. ->will($this->returnValue($packages));
  98. $best = $versionSelector->findBestCandidate($packageName);
  99. $this->assertSame($package1, $best, 'Latest version supporting composer 1 should be returned (1.0.0)');
  100. $best = $versionSelector->findBestCandidate($packageName, null, 'stable', true);
  101. $this->assertSame($package2, $best, 'Latest version should be returned when ignoring platform reqs (1.1.0)');
  102. }
  103. public function testMostStableVersionIsReturned()
  104. {
  105. $packageName = 'foobar';
  106. $package1 = $this->createPackage('1.0.0');
  107. $package2 = $this->createPackage('1.1.0-beta');
  108. $packages = array($package1, $package2);
  109. $repositorySet = $this->createMockRepositorySet();
  110. $repositorySet->expects($this->once())
  111. ->method('findPackages')
  112. ->with($packageName, null)
  113. ->will($this->returnValue($packages));
  114. $versionSelector = new VersionSelector($repositorySet);
  115. $best = $versionSelector->findBestCandidate($packageName);
  116. $this->assertSame($package1, $best, 'Latest most stable version should be returned (1.0.0)');
  117. }
  118. public function testMostStableVersionIsReturnedRegardlessOfOrder()
  119. {
  120. $packageName = 'foobar';
  121. $package1 = $this->createPackage('2.x-dev');
  122. $package2 = $this->createPackage('2.0.0-beta3');
  123. $packages = array($package1, $package2);
  124. $repositorySet = $this->createMockRepositorySet();
  125. $repositorySet->expects($this->at(0))
  126. ->method('findPackages')
  127. ->with($packageName, null)
  128. ->will($this->returnValue($packages));
  129. $repositorySet->expects($this->at(1))
  130. ->method('findPackages')
  131. ->with($packageName, null)
  132. ->will($this->returnValue(array_reverse($packages)));
  133. $versionSelector = new VersionSelector($repositorySet);
  134. $best = $versionSelector->findBestCandidate($packageName);
  135. $this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev');
  136. $best = $versionSelector->findBestCandidate($packageName);
  137. $this->assertSame($package2, $best, 'Expecting 2.0.0-beta3, cause beta is more stable than dev');
  138. }
  139. public function testHighestVersionIsReturned()
  140. {
  141. $packageName = 'foobar';
  142. $package1 = $this->createPackage('1.0.0');
  143. $package2 = $this->createPackage('1.1.0-beta');
  144. $packages = array($package1, $package2);
  145. $repositorySet = $this->createMockRepositorySet();
  146. $repositorySet->expects($this->once())
  147. ->method('findPackages')
  148. ->with($packageName, null)
  149. ->will($this->returnValue($packages));
  150. $versionSelector = new VersionSelector($repositorySet);
  151. $best = $versionSelector->findBestCandidate($packageName, null, 'dev');
  152. $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
  153. }
  154. public function testHighestVersionMatchingStabilityIsReturned()
  155. {
  156. $packageName = 'foobar';
  157. $package1 = $this->createPackage('1.0.0');
  158. $package2 = $this->createPackage('1.1.0-beta');
  159. $package3 = $this->createPackage('1.2.0-alpha');
  160. $packages = array($package1, $package2, $package3);
  161. $repositorySet = $this->createMockRepositorySet();
  162. $repositorySet->expects($this->once())
  163. ->method('findPackages')
  164. ->with($packageName, null)
  165. ->will($this->returnValue($packages));
  166. $versionSelector = new VersionSelector($repositorySet);
  167. $best = $versionSelector->findBestCandidate($packageName, null, 'beta');
  168. $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
  169. }
  170. public function testMostStableUnstableVersionIsReturned()
  171. {
  172. $packageName = 'foobar';
  173. $package2 = $this->createPackage('1.1.0-beta');
  174. $package3 = $this->createPackage('1.2.0-alpha');
  175. $packages = array($package2, $package3);
  176. $repositorySet = $this->createMockRepositorySet();
  177. $repositorySet->expects($this->once())
  178. ->method('findPackages')
  179. ->with($packageName, null)
  180. ->will($this->returnValue($packages));
  181. $versionSelector = new VersionSelector($repositorySet);
  182. $best = $versionSelector->findBestCandidate($packageName, null, 'stable');
  183. $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
  184. }
  185. public function testFalseReturnedOnNoPackages()
  186. {
  187. $repositorySet = $this->createMockRepositorySet();
  188. $repositorySet->expects($this->once())
  189. ->method('findPackages')
  190. ->will($this->returnValue(array()));
  191. $versionSelector = new VersionSelector($repositorySet);
  192. $best = $versionSelector->findBestCandidate('foobaz');
  193. $this->assertFalse($best, 'No versions are available returns false');
  194. }
  195. /**
  196. * @dataProvider getRecommendedRequireVersionPackages
  197. */
  198. public function testFindRecommendedRequireVersion($prettyVersion, $isDev, $stability, $expectedVersion, $branchAlias = null)
  199. {
  200. $repositorySet = $this->createMockRepositorySet();
  201. $versionSelector = new VersionSelector($repositorySet);
  202. $versionParser = new VersionParser();
  203. $package = $this->getMockBuilder('\Composer\Package\PackageInterface')->getMock();
  204. $package
  205. ->expects($this->any())
  206. ->method('getPrettyVersion')
  207. ->will($this->returnValue($prettyVersion));
  208. $package
  209. ->expects($this->any())
  210. ->method('getVersion')
  211. ->will($this->returnValue($versionParser->normalize($prettyVersion)));
  212. $package
  213. ->expects($this->any())
  214. ->method('isDev')
  215. ->will($this->returnValue($isDev));
  216. $package
  217. ->expects($this->any())
  218. ->method('getStability')
  219. ->will($this->returnValue($stability));
  220. $package
  221. ->expects($this->any())
  222. ->method('getTransportOptions')
  223. ->will($this->returnValue(array()));
  224. $branchAlias = $branchAlias === null ? array() : array('branch-alias' => array($prettyVersion => $branchAlias));
  225. $package->expects($this->any())
  226. ->method('getExtra')
  227. ->will($this->returnValue($branchAlias));
  228. $recommended = $versionSelector->findRecommendedRequireVersion($package);
  229. // assert that the recommended version is what we expect
  230. $this->assertSame($expectedVersion, $recommended);
  231. }
  232. public function getRecommendedRequireVersionPackages()
  233. {
  234. return array(
  235. // real version, is dev package, stability, expected recommendation, [branch-alias]
  236. array('1.2.1', false, 'stable', '^1.2'),
  237. array('1.2', false, 'stable', '^1.2'),
  238. array('v1.2.1', false, 'stable', '^1.2'),
  239. array('3.1.2-pl2', false, 'stable', '^3.1'),
  240. array('3.1.2-patch', false, 'stable', '^3.1'),
  241. array('2.0-beta.1', false, 'beta', '^2.0@beta'),
  242. array('3.1.2-alpha5', false, 'alpha', '^3.1@alpha'),
  243. array('3.0-RC2', false, 'RC', '^3.0@RC'),
  244. array('0.1.0', false, 'stable', '^0.1.0'),
  245. array('0.1.3', false, 'stable', '^0.1.3'),
  246. array('0.0.3', false, 'stable', '^0.0.3'),
  247. array('0.0.3-alpha', false, 'alpha', '^0.0.3@alpha'),
  248. // date-based versions are not touched at all
  249. array('v20121020', false, 'stable', 'v20121020'),
  250. array('v20121020.2', false, 'stable', 'v20121020.2'),
  251. // dev packages without alias are not touched at all
  252. array('dev-master', true, 'dev', 'dev-master'),
  253. array('3.1.2-dev', true, 'dev', '3.1.2-dev'),
  254. // dev packages with alias inherit the alias
  255. array('dev-master', true, 'dev', '^2.1@dev', '2.1.x-dev'),
  256. array('dev-master', true, 'dev', '^2.1@dev', '2.1-dev'),
  257. array('dev-master', true, 'dev', '^2.1@dev', '2.1.3.x-dev'),
  258. array('dev-master', true, 'dev', '^2.0@dev', '2.x-dev'),
  259. array('dev-master', true, 'dev', '^0.3.0@dev', '0.3.x-dev'),
  260. array('dev-master', true, 'dev', '^0.0.3@dev', '0.0.3.x-dev'),
  261. // numeric alias
  262. array('3.x-dev', true, 'dev', '^3.0@dev', '3.0.x-dev'),
  263. array('3.x-dev', true, 'dev', '^3.0@dev', '3.0-dev'),
  264. );
  265. }
  266. private function createPackage($version)
  267. {
  268. $parser = new VersionParser();
  269. return new Package('foo', $parser->normalize($version), $version);
  270. }
  271. private function createMockRepositorySet()
  272. {
  273. return $this->getMockBuilder('Composer\Repository\RepositorySet')
  274. ->disableOriginalConstructor()
  275. ->getMock();
  276. }
  277. }