NewVersionCheckerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Console\SelfUpdate;
  13. use PhpCsFixer\Console\SelfUpdate\GithubClientInterface;
  14. use PhpCsFixer\Console\SelfUpdate\NewVersionChecker;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Console\SelfUpdate\NewVersionChecker
  20. */
  21. final class NewVersionCheckerTest extends TestCase
  22. {
  23. public function testGetLatestVersion(): void
  24. {
  25. $checker = new NewVersionChecker($this->createGithubClientDouble());
  26. self::assertSame('v2.4.1', $checker->getLatestVersion());
  27. }
  28. /**
  29. * @dataProvider provideGetLatestVersionOfMajorCases
  30. */
  31. public function testGetLatestVersionOfMajor(int $majorVersion, ?string $expectedVersion): void
  32. {
  33. $checker = new NewVersionChecker($this->createGithubClientDouble());
  34. self::assertSame($expectedVersion, $checker->getLatestVersionOfMajor($majorVersion));
  35. }
  36. /**
  37. * @return iterable<array{int, null|string}>
  38. */
  39. public static function provideGetLatestVersionOfMajorCases(): iterable
  40. {
  41. yield [1, 'v1.13.2'];
  42. yield [2, 'v2.4.1'];
  43. yield [4, null];
  44. }
  45. /**
  46. * @dataProvider provideCompareVersionsCases
  47. */
  48. public function testCompareVersions(string $versionA, string $versionB, int $expectedResult): void
  49. {
  50. $checker = new NewVersionChecker($this->createGithubClientDouble());
  51. self::assertSame(
  52. $expectedResult,
  53. $checker->compareVersions($versionA, $versionB)
  54. );
  55. self::assertSame(
  56. -$expectedResult,
  57. $checker->compareVersions($versionB, $versionA)
  58. );
  59. }
  60. /**
  61. * @return iterable<array{string, string, int}>
  62. */
  63. public static function provideCompareVersionsCases(): iterable
  64. {
  65. foreach ([
  66. ['1.0.0-alpha', '1.0.0', -1],
  67. ['1.0.0-beta', '1.0.0', -1],
  68. ['1.0.0-RC', '1.0.0', -1],
  69. ['1.0.0', '1.0.0', 0],
  70. ['1.0.0', '1.0.1', -1],
  71. ['1.0.0', '1.1.0', -1],
  72. ['1.0.0', '2.0.0', -1],
  73. ] as $case) {
  74. // X.Y.Z vs. X.Y.Z
  75. yield $case;
  76. // vX.Y.Z vs. X.Y.Z
  77. $case[0] = 'v'.$case[0];
  78. yield $case;
  79. // vX.Y.Z vs. vX.Y.Z
  80. $case[1] = 'v'.$case[1];
  81. yield $case;
  82. // X.Y.Z vs. vX.Y.Z
  83. $case[0] = substr($case[0], 1);
  84. yield $case;
  85. }
  86. }
  87. private function createGithubClientDouble(): GithubClientInterface
  88. {
  89. return new class implements GithubClientInterface {
  90. public function getTags(): array
  91. {
  92. return [
  93. 'v3.0.0-RC',
  94. 'v2.4.1',
  95. 'v2.4.0',
  96. 'v2.3.3',
  97. 'v2.3.2',
  98. 'v2.3.1',
  99. 'v2.3.0',
  100. 'v2.2.6',
  101. 'v2.2.5',
  102. 'v2.2.4',
  103. 'v2.2.3',
  104. 'v2.2.2',
  105. 'v2.2.1',
  106. 'v2.2.0',
  107. 'v2.1.3',
  108. 'v2.1.2',
  109. 'v2.1.1',
  110. 'v2.1.0',
  111. 'v2.0.1',
  112. 'v2.0.0',
  113. 'v2.0.0-beta',
  114. 'v2.0.0-alpha',
  115. 'v2.0.0-RC',
  116. 'v1.14.0-beta',
  117. 'v1.13.2',
  118. 'v1.13.1',
  119. 'v1.13.0',
  120. 'v1.12.4',
  121. 'v1.12.3',
  122. 'v1.12.2',
  123. 'v1.12.1',
  124. ];
  125. }
  126. };
  127. }
  128. }