Geen omschrijving

VersionTest.php 3.7KB

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