VersionSpecificationTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\FixerDefinition;
  13. use PhpCsFixer\FixerDefinition\VersionSpecification;
  14. use PhpCsFixer\Tests\TestCase;
  15. /**
  16. * @author Andreas Möller <am@localheinz.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\FixerDefinition\VersionSpecification
  21. */
  22. final class VersionSpecificationTest extends TestCase
  23. {
  24. public function testConstructorRequiresEitherMinimumOrMaximum(): void
  25. {
  26. $this->expectException(\InvalidArgumentException::class);
  27. new VersionSpecification();
  28. }
  29. /**
  30. * @dataProvider provideInvalidVersionCases
  31. */
  32. public function testConstructorRejectsInvalidMinimum(int $minimum): void
  33. {
  34. $this->expectException(\InvalidArgumentException::class);
  35. new VersionSpecification($minimum);
  36. }
  37. /**
  38. * @dataProvider provideInvalidVersionCases
  39. */
  40. public function testConstructorRejectsInvalidMaximum(int $maximum): void
  41. {
  42. $this->expectException(\InvalidArgumentException::class);
  43. new VersionSpecification(
  44. \PHP_VERSION_ID,
  45. $maximum
  46. );
  47. }
  48. public static function provideInvalidVersionCases(): iterable
  49. {
  50. yield 'negative' => [-1];
  51. yield 'zero' => [0];
  52. }
  53. public function testConstructorRejectsMaximumLessThanMinimum(): void
  54. {
  55. $this->expectException(\InvalidArgumentException::class);
  56. new VersionSpecification(
  57. \PHP_VERSION_ID,
  58. \PHP_VERSION_ID - 1
  59. );
  60. }
  61. /**
  62. * @dataProvider provideIsSatisfiedByReturnsTrueCases
  63. */
  64. public function testIsSatisfiedByReturnsTrue(?int $minimum, ?int $maximum, int $actual): void
  65. {
  66. $versionSpecification = new VersionSpecification(
  67. $minimum,
  68. $maximum
  69. );
  70. self::assertTrue($versionSpecification->isSatisfiedBy($actual));
  71. }
  72. public static function provideIsSatisfiedByReturnsTrueCases(): iterable
  73. {
  74. yield 'version-same-as-maximum' => [null, \PHP_VERSION_ID, \PHP_VERSION_ID];
  75. yield 'version-same-as-minimum' => [\PHP_VERSION_ID, null, \PHP_VERSION_ID];
  76. yield 'version-between-minimum-and-maximum' => [\PHP_VERSION_ID - 1, \PHP_VERSION_ID + 1, \PHP_VERSION_ID];
  77. yield 'version-same-as-minimum-and-maximum' => [\PHP_VERSION_ID, \PHP_VERSION_ID, \PHP_VERSION_ID];
  78. }
  79. /**
  80. * @dataProvider provideIsSatisfiedByReturnsFalseCases
  81. */
  82. public function testIsSatisfiedByReturnsFalse(?int $minimum, ?int $maximum, int $actual): void
  83. {
  84. $versionSpecification = new VersionSpecification(
  85. $minimum,
  86. $maximum
  87. );
  88. self::assertFalse($versionSpecification->isSatisfiedBy($actual));
  89. }
  90. public static function provideIsSatisfiedByReturnsFalseCases(): iterable
  91. {
  92. yield 'version-greater-than-maximum' => [null, \PHP_VERSION_ID, \PHP_VERSION_ID + 1];
  93. yield 'version-less-than-minimum' => [\PHP_VERSION_ID, null, \PHP_VERSION_ID - 1];
  94. }
  95. }