VersionConstraintTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\LinkConstraint;
  12. use Composer\Package\LinkConstraint\VersionConstraint;
  13. class VersionConstraintTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public static function successfulVersionMatches()
  16. {
  17. return array(
  18. // require provide
  19. array('==', '1', '==', '1'),
  20. array('>=', '1', '>=', '2'),
  21. array('>=', '2', '>=', '1'),
  22. array('>=', '2', '>', '1'),
  23. array('<=', '2', '>=', '1'),
  24. array('>=', '1', '<=', '2'),
  25. array('==', '2', '>=', '2'),
  26. array('!=', '1', '!=', '1'),
  27. array('!=', '1', '==', '2'),
  28. array('!=', '1', '<', '1'),
  29. array('!=', '1', '<=', '1'),
  30. array('!=', '1', '>', '1'),
  31. array('!=', '1', '>=', '1'),
  32. array('==', 'dev-foo-bar', '==', 'dev-foo-bar'),
  33. array('==', 'dev-foo-xyz', '==', 'dev-foo-xyz'),
  34. array('>=', 'dev-foo-bar', '>=', 'dev-foo-xyz'),
  35. array('<=', 'dev-foo-bar', '<', 'dev-foo-xyz'),
  36. array('!=', 'dev-foo-bar', '<', 'dev-foo-xyz'),
  37. array('>=', 'dev-foo-bar', '!=', 'dev-foo-bar'),
  38. array('!=', 'dev-foo-bar', '!=', 'dev-foo-xyz'),
  39. );
  40. }
  41. /**
  42. * @dataProvider successfulVersionMatches
  43. */
  44. public function testVersionMatchSucceeds($requireOperator, $requireVersion, $provideOperator, $provideVersion)
  45. {
  46. $versionRequire = new VersionConstraint($requireOperator, $requireVersion);
  47. $versionProvide = new VersionConstraint($provideOperator, $provideVersion);
  48. $this->assertTrue($versionRequire->matches($versionProvide));
  49. }
  50. public static function failingVersionMatches()
  51. {
  52. return array(
  53. // require provide
  54. array('==', '1', '==', '2'),
  55. array('>=', '2', '<=', '1'),
  56. array('>=', '2', '<', '2'),
  57. array('<=', '2', '>', '2'),
  58. array('>', '2', '<=', '2'),
  59. array('<=', '1', '>=', '2'),
  60. array('>=', '2', '<=', '1'),
  61. array('==', '2', '<', '2'),
  62. array('!=', '1', '==', '1'),
  63. array('==', '1', '!=', '1'),
  64. array('==', 'dev-foo-dist', '==', 'dev-foo-zist'),
  65. array('==', 'dev-foo-bist', '==', 'dev-foo-aist'),
  66. array('<=', 'dev-foo-bist', '>=', 'dev-foo-aist'),
  67. array('>=', 'dev-foo-bist', '<', 'dev-foo-aist'),
  68. array('<', '0.12', '==', 'dev-foo'), // branches are not comparable
  69. array('>', '0.12', '==', 'dev-foo'), // branches are not comparable
  70. );
  71. }
  72. /**
  73. * @dataProvider failingVersionMatches
  74. */
  75. public function testVersionMatchFails($requireOperator, $requireVersion, $provideOperator, $provideVersion)
  76. {
  77. $versionRequire = new VersionConstraint($requireOperator, $requireVersion);
  78. $versionProvide = new VersionConstraint($provideOperator, $provideVersion);
  79. $this->assertFalse($versionRequire->matches($versionProvide));
  80. }
  81. public function testComparableBranches()
  82. {
  83. $versionRequire = new VersionConstraint('>', '0.12');
  84. $versionProvide = new VersionConstraint('==', 'dev-foo');
  85. $this->assertFalse($versionRequire->matches($versionProvide));
  86. $this->assertFalse($versionRequire->matchSpecific($versionProvide, true));
  87. $versionRequire = new VersionConstraint('<', '0.12');
  88. $versionProvide = new VersionConstraint('==', 'dev-foo');
  89. $this->assertFalse($versionRequire->matches($versionProvide));
  90. $this->assertTrue($versionRequire->matchSpecific($versionProvide, true));
  91. }
  92. }