VersionTest.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Version
  14. */
  15. class VersionTest extends TestCase {
  16. /**
  17. * @dataProvider versionProvider
  18. *
  19. * @param string $versionString
  20. * @param string $expectedMajor
  21. * @param string $expectedMinor
  22. * @param string $expectedPatch
  23. * @param string $expectedPreReleaseValue
  24. * @param int $expectedReleaseCount
  25. */
  26. public function testParsesVersionNumbers($versionString, $expectedMajor, $expectedMinor, $expectedPatch, $expectedPreReleaseValue = '', $expectedReleaseCount = 0) {
  27. $version = new Version($versionString);
  28. $this->assertSame($expectedMajor, $version->getMajor()->getValue());
  29. $this->assertSame($expectedMinor, $version->getMinor()->getValue());
  30. $this->assertSame($expectedPatch, $version->getPatch()->getValue());
  31. if ($expectedPreReleaseValue !== '') {
  32. $this->assertSame($expectedPreReleaseValue, $version->getPreReleaseSuffix()->getValue());
  33. }
  34. if ($expectedReleaseCount !== 0) {
  35. $this->assertSame($expectedReleaseCount, $version->getPreReleaseSuffix()->getNumber());
  36. }
  37. $this->assertSame($versionString, $version->getVersionString());
  38. }
  39. public function versionProvider() {
  40. return [
  41. ['0.0.1', '0', '0', '1'],
  42. ['0.1.2', '0', '1', '2'],
  43. ['1.0.0-alpha', '1', '0', '0', 'alpha'],
  44. ['3.4.12-dev3', '3', '4', '12', 'dev', 3],
  45. ];
  46. }
  47. /**
  48. * @dataProvider versionGreaterThanProvider
  49. *
  50. * @param Version $versionA
  51. * @param Version $versionB
  52. * @param bool $expectedResult
  53. */
  54. public function testIsGreaterThan(Version $versionA, Version $versionB, $expectedResult) {
  55. $this->assertSame($expectedResult, $versionA->isGreaterThan($versionB));
  56. }
  57. /**
  58. * @return array
  59. */
  60. public function versionGreaterThanProvider() {
  61. return [
  62. [new Version('1.0.0'), new Version('1.0.1'), false],
  63. [new Version('1.0.1'), new Version('1.0.0'), true],
  64. [new Version('1.1.0'), new Version('1.0.1'), true],
  65. [new Version('1.1.0'), new Version('2.0.1'), false],
  66. [new Version('1.1.0'), new Version('1.1.0'), false],
  67. [new Version('2.5.8'), new Version('1.6.8'), true],
  68. [new Version('2.5.8'), new Version('2.6.8'), false],
  69. [new Version('2.5.8'), new Version('3.1.2'), false],
  70. ];
  71. }
  72. /**
  73. * @dataProvider invalidVersionStringProvider
  74. *
  75. * @param string $versionString
  76. */
  77. public function testThrowsExceptionIfVersionStringDoesNotFollowSemVer($versionString)
  78. {
  79. $this->expectException(InvalidVersionException::class);
  80. new Version($versionString);
  81. }
  82. /**
  83. * @return array
  84. */
  85. public function invalidVersionStringProvider()
  86. {
  87. return [
  88. ['foo'],
  89. ['0.0.1-dev+ABC', '0', '0', '1', 'dev', 'ABC'],
  90. ['1.0.0-x.7.z.92', '1', '0', '0', 'x.7.z.92']
  91. ];
  92. }
  93. }