VersionSelectorTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. class VersionSelectorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. // A) multiple versions, get the latest one
  16. // B) targetPackageVersion will pass to pool
  17. // C) No results, throw exception
  18. public function testLatestVersionIsReturned()
  19. {
  20. $packageName = 'foobar';
  21. $package1 = $this->createMockPackage('1.2.1');
  22. $package2 = $this->createMockPackage('1.2.2');
  23. $package3 = $this->createMockPackage('1.2.0');
  24. $packages = array($package1, $package2, $package3);
  25. $pool = $this->createMockPool();
  26. $pool->expects($this->once())
  27. ->method('whatProvides')
  28. ->with($packageName, null, true)
  29. ->will($this->returnValue($packages));
  30. $versionSelector = new VersionSelector($pool);
  31. $best = $versionSelector->findBestCandidate($packageName);
  32. // 1.2.2 should be returned because it's the latest of the returned versions
  33. $this->assertEquals($package2, $best, 'Latest version should be 1.2.2');
  34. }
  35. public function testFalseReturnedOnNoPackages()
  36. {
  37. $pool = $this->createMockPool();
  38. $pool->expects($this->once())
  39. ->method('whatProvides')
  40. ->will($this->returnValue(array()));
  41. $versionSelector = new VersionSelector($pool);
  42. $best = $versionSelector->findBestCandidate('foobaz');
  43. $this->assertFalse($best, 'No versions are available returns false');
  44. }
  45. /**
  46. * @dataProvider getRecommendedRequireVersionPackages
  47. */
  48. public function testFindRecommendedRequireVersion($prettyVersion, $isDev, $stability, $expectedVersion)
  49. {
  50. $pool = $this->createMockPool();
  51. $versionSelector = new VersionSelector($pool);
  52. $package = $this->getMock('\Composer\Package\PackageInterface');
  53. $package->expects($this->any())
  54. ->method('getPrettyVersion')
  55. ->will($this->returnValue($prettyVersion));
  56. $package->expects($this->any())
  57. ->method('isDev')
  58. ->will($this->returnValue($isDev));
  59. $package->expects($this->any())
  60. ->method('getStability')
  61. ->will($this->returnValue($stability));
  62. $recommended = $versionSelector->findRecommendedRequireVersion($package);
  63. // assert that the recommended version is what we expect
  64. $this->assertEquals($expectedVersion, $recommended);
  65. }
  66. public function getRecommendedRequireVersionPackages()
  67. {
  68. return array(
  69. // real version, is dev package, stability, expected recommendation
  70. array('1.2.1', false, 'stable', '~1.2'),
  71. array('1.2', false, 'stable', '~1.2'),
  72. array('v1.2.1', false, 'stable', '~1.2'),
  73. array('3.1.2-pl2', false, 'stable', '~3.1'),
  74. array('3.1.2-patch', false, 'stable', '~3.1'),
  75. // for non-stable versions, we add ~, but don't try the (1.2.1 -> 1.2) transformation
  76. array('2.0-beta.1', false, 'beta', '~2.0-beta.1'),
  77. array('3.1.2-alpha5', false, 'alpha', '~3.1.2-alpha5'),
  78. array('3.0-RC2', false, 'RC', '~3.0-RC2'),
  79. // dev packages are not touched at all
  80. array('dev-master', true, 'dev', 'dev-master'),
  81. array('3.1.2-dev', true, 'dev', '3.1.2-dev'),
  82. );
  83. }
  84. private function createMockPackage($version)
  85. {
  86. $package = $this->getMock('\Composer\Package\PackageInterface');
  87. $package->expects($this->any())
  88. ->method('getVersion')
  89. ->will($this->returnValue($version));
  90. return $package;
  91. }
  92. private function createMockPool()
  93. {
  94. return $this->getMock('Composer\DependencyResolver\Pool', array(), array(), '', true);
  95. }
  96. }