VersionConstraintParserTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /*
  3. * This file is part of PharIo\Version.
  4. *
  5. * (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PharIo\Version;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers \PharIo\Version\VersionConstraintParser
  14. */
  15. class VersionConstraintParserTest extends TestCase {
  16. /**
  17. * @dataProvider versionStringProvider
  18. *
  19. * @param string $versionString
  20. * @param VersionConstraint $expectedConstraint
  21. */
  22. public function testReturnsExpectedConstraint($versionString, VersionConstraint $expectedConstraint) {
  23. $parser = new VersionConstraintParser;
  24. $this->assertEquals($expectedConstraint, $parser->parse($versionString));
  25. }
  26. /**
  27. * @dataProvider unsupportedVersionStringProvider
  28. *
  29. * @param string $versionString
  30. */
  31. public function testThrowsExceptionIfVersionStringIsNotSupported($versionString) {
  32. $parser = new VersionConstraintParser;
  33. $this->expectException(UnsupportedVersionConstraintException::class);
  34. $parser->parse($versionString);
  35. }
  36. /**
  37. * @return array
  38. */
  39. public function versionStringProvider() {
  40. return [
  41. ['1.0.2', new ExactVersionConstraint('1.0.2')],
  42. [
  43. '~4.6',
  44. new AndVersionConstraintGroup(
  45. '~4.6',
  46. [
  47. new GreaterThanOrEqualToVersionConstraint('~4.6', new Version('4.6')),
  48. new SpecificMajorVersionConstraint('~4.6', 4)
  49. ]
  50. )
  51. ],
  52. [
  53. '~4.6.2',
  54. new AndVersionConstraintGroup(
  55. '~4.6.2',
  56. [
  57. new GreaterThanOrEqualToVersionConstraint('~4.6.2', new Version('4.6.2')),
  58. new SpecificMajorAndMinorVersionConstraint('~4.6.2', 4, 6)
  59. ]
  60. )
  61. ],
  62. [
  63. '^2.6.1',
  64. new AndVersionConstraintGroup(
  65. '^2.6.1',
  66. [
  67. new GreaterThanOrEqualToVersionConstraint('^2.6.1', new Version('2.6.1')),
  68. new SpecificMajorVersionConstraint('^2.6.1', 2)
  69. ]
  70. )
  71. ],
  72. ['5.1.*', new SpecificMajorAndMinorVersionConstraint('5.1.*', 5, 1)],
  73. ['5.*', new SpecificMajorVersionConstraint('5.*', 5)],
  74. ['*', new AnyVersionConstraint()],
  75. [
  76. '1.0.2 || 1.0.5',
  77. new OrVersionConstraintGroup(
  78. '1.0.2 || 1.0.5',
  79. [
  80. new ExactVersionConstraint('1.0.2'),
  81. new ExactVersionConstraint('1.0.5')
  82. ]
  83. )
  84. ],
  85. [
  86. '^5.6 || ^7.0',
  87. new OrVersionConstraintGroup(
  88. '^5.6 || ^7.0',
  89. [
  90. new AndVersionConstraintGroup(
  91. '^5.6', [
  92. new GreaterThanOrEqualToVersionConstraint('^5.6', new Version('5.6')),
  93. new SpecificMajorVersionConstraint('^5.6', 5)
  94. ]
  95. ),
  96. new AndVersionConstraintGroup(
  97. '^7.0', [
  98. new GreaterThanOrEqualToVersionConstraint('^7.0', new Version('7.0')),
  99. new SpecificMajorVersionConstraint('^7.0', 7)
  100. ]
  101. )
  102. ]
  103. )
  104. ]
  105. ];
  106. }
  107. public function unsupportedVersionStringProvider() {
  108. return [
  109. ['foo'],
  110. ['+1.0.2'],
  111. ['>=2.0'],
  112. ['^5.6 || >= 7.0'],
  113. ['2.0 || foo']
  114. ];
  115. }
  116. }