CompareVersionTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\App;
  8. use InvalidArgumentException;
  9. use OC\App\CompareVersion;
  10. use Test\TestCase;
  11. class CompareVersionTest extends TestCase {
  12. /** @var CompareVersion */
  13. private $compare;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->compare = new CompareVersion();
  17. }
  18. public function comparisonData() {
  19. return [
  20. // Compatible versions
  21. ['13.0.0.3', '13.0.0', '>=', true],
  22. ['13.0.0.3', '13.0.0', '<=', true],
  23. ['13.0.0', '13.0.0', '>=', true],
  24. ['13.0.0', '13.0', '<=', true],
  25. ['13.0.0', '13', '>=', true],
  26. ['13.0.1', '13', '>=', true],
  27. ['13.0.1', '13', '<=', true],
  28. ['13.0.1.9', '13', '<=', true],
  29. ['13.0.1-beta.1', '13', '<=', true],
  30. ['7.4.14', '7.4', '<=', true],
  31. ['7.4.14-ubuntu', '7.4', '<=', true],
  32. ['7.4.14-ubuntu', '7.4.15', '<=', true],
  33. ['7.4.16-ubuntu', '7.4.15', '<=', false],
  34. // Incompatible major versions
  35. ['13.0.0.3', '13.0.0', '<', false],
  36. ['12.0.0', '13.0.0', '>=', false],
  37. ['12.0.0', '13.0', '>=', false],
  38. ['12.0.0', '13', '>=', false],
  39. ['7.4.15-ubuntu', '7.4.15', '>=', true],
  40. // Incompatible minor and patch versions
  41. ['13.0.0', '13.0.1', '>=', false],
  42. ['13.0.0', '13.1', '>=', false],
  43. // Compatible minor and patch versions
  44. ['13.0.1', '13.0.0', '>=', true],
  45. ['13.2.0', '13.1', '>=', true],
  46. ];
  47. }
  48. /**
  49. * @dataProvider comparisonData
  50. */
  51. public function testComparison(string $actualVersion, string $requiredVersion,
  52. string $comparator, bool $expected) {
  53. $isCompatible = $this->compare->isCompatible($actualVersion, $requiredVersion,
  54. $comparator);
  55. $this->assertEquals($expected, $isCompatible);
  56. }
  57. public function testInvalidServerVersion() {
  58. $actualVersion = '13';
  59. $this->expectException(InvalidArgumentException::class);
  60. $this->compare->isCompatible($actualVersion, '13.0.0');
  61. }
  62. public function testInvalidRequiredVersion() {
  63. $actualVersion = '13.0.0';
  64. $this->expectException(InvalidArgumentException::class);
  65. $this->compare->isCompatible($actualVersion, '13.0.0.9');
  66. }
  67. }