VersionSelectorTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. private function createMockPackage($version)
  46. {
  47. $package = $this->getMock('\Composer\Package\PackageInterface');
  48. $package->expects($this->any())
  49. ->method('getVersion')
  50. ->will($this->returnValue($version));
  51. return $package;
  52. }
  53. private function createMockPool()
  54. {
  55. return $this->getMock('Composer\DependencyResolver\Pool', array(), array(), '', true);
  56. }
  57. }