VersionParserTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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\VersionParser;
  13. use Composer\Package\LinkConstraint\MultiConstraint;
  14. use Composer\Package\LinkConstraint\VersionConstraint;
  15. class VersionParserTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @dataProvider successfulNormalizedVersions
  19. */
  20. public function testNormalizeSucceeds($input, $expected)
  21. {
  22. $parser = new VersionParser;
  23. $this->assertEquals($expected, $parser->normalize($input));
  24. }
  25. public function successfulNormalizedVersions()
  26. {
  27. return array(
  28. 'none' => array('1.0.0', '1.0.0'),
  29. 'parses state' => array('1.0.0RC1dev', '1.0.0-rc1-dev'),
  30. 'CI parsing' => array('1.0.0-rC15-dev', '1.0.0-rc15-dev'),
  31. 'forces x.y.z' => array('1.0-dev', '1.0.0-dev'),
  32. 'parses long' => array('10.4.13-beta', '10.4.13-beta'),
  33. 'strips leading v' => array('v1.0.0', '1.0.0'),
  34. 'strips leading v' => array('v20100102', '20100102'),
  35. 'parses dates w/ .' => array('2010.01.02', '2010-01-02'),
  36. 'parses dates w/ -' => array('2010-01-02', '2010-01-02'),
  37. 'parses numbers' => array('2010-01-02.5', '2010-01-02-5'),
  38. 'parses datetime' => array('20100102-203040', '20100102-203040'),
  39. 'parses dt+number' => array('20100102203040-10', '20100102203040-10'),
  40. );
  41. }
  42. /**
  43. * @dataProvider failingNormalizedVersions
  44. * @expectedException UnexpectedValueException
  45. */
  46. public function testNormalizeFails($input)
  47. {
  48. $parser = new VersionParser;
  49. $parser->normalize($input);
  50. }
  51. public function failingNormalizedVersions()
  52. {
  53. return array(
  54. 'empty ' => array(''),
  55. 'invalid chars' => array('a'),
  56. 'invalid type' => array('1.0.0-meh'),
  57. 'too many bits' => array('1.0.0.0'),
  58. );
  59. }
  60. /**
  61. * @dataProvider simpleConstraints
  62. */
  63. public function testParseConstraintsSimple($input, $expected)
  64. {
  65. $parser = new VersionParser;
  66. $this->assertEquals((string) $expected, (string) $parser->parseConstraints($input));
  67. }
  68. public function simpleConstraints()
  69. {
  70. return array(
  71. 'greater than' => array('>1.0.0', new VersionConstraint('>', '1.0.0')),
  72. 'lesser than' => array('<1.2.3', new VersionConstraint('<', '1.2.3')),
  73. 'less/eq than' => array('<=1.2.3', new VersionConstraint('<=', '1.2.3')),
  74. 'great/eq than' => array('>=1.2.3', new VersionConstraint('>=', '1.2.3')),
  75. 'equals' => array('=1.2.3', new VersionConstraint('=', '1.2.3')),
  76. 'double equals' => array('==1.2.3', new VersionConstraint('=', '1.2.3')),
  77. 'no op means eq' => array('1.2.3', new VersionConstraint('=', '1.2.3')),
  78. 'completes version' => array('=1.0', new VersionConstraint('=', '1.0.0')),
  79. 'accepts spaces' => array('>= 1.2.3', new VersionConstraint('>=', '1.2.3')),
  80. );
  81. }
  82. /**
  83. * @dataProvider wildcardConstraints
  84. */
  85. public function testParseConstraintsWildcard($input, $min, $max)
  86. {
  87. $parser = new VersionParser;
  88. $expected = new MultiConstraint(array($min, $max));
  89. $this->assertEquals((string) $expected, (string) $parser->parseConstraints($input));
  90. }
  91. public function wildcardConstraints()
  92. {
  93. return array(
  94. array('2.*', new VersionConstraint('>=', '2.0.0'), new VersionConstraint('<', '3.0.0')),
  95. array('20.*', new VersionConstraint('>=', '20.0.0'), new VersionConstraint('<', '21.0.0')),
  96. array('2.0.*', new VersionConstraint('>=', '2.0.0'), new VersionConstraint('<', '2.1.0')),
  97. array('2.2.*', new VersionConstraint('>=', '2.2.0'), new VersionConstraint('<', '2.3.0')),
  98. array('2.10.*', new VersionConstraint('>=', '2.10.0'), new VersionConstraint('<', '2.11.0')),
  99. );
  100. }
  101. /**
  102. * @dataProvider failingConstraints
  103. * @expectedException UnexpectedValueException
  104. */
  105. public function testParseConstraintsFails($input)
  106. {
  107. $parser = new VersionParser;
  108. $parser->parseConstraints($input);
  109. }
  110. public function failingConstraints()
  111. {
  112. return array(
  113. 'empty ' => array(''),
  114. 'invalid version' => array('1.0.0-meh'),
  115. );
  116. }
  117. }