VersionSpecificationTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\FixerDefinition;
  12. use PhpCsFixer\FixerDefinition\VersionSpecification;
  13. use PhpCsFixer\Tests\TestCase;
  14. /**
  15. * @author Andreas Möller <am@localheinz.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\FixerDefinition\VersionSpecification
  20. */
  21. final class VersionSpecificationTest extends TestCase
  22. {
  23. public function testConstructorRequiresEitherMinimumOrMaximum()
  24. {
  25. $this->expectException(\InvalidArgumentException::class);
  26. new VersionSpecification();
  27. }
  28. /**
  29. * @dataProvider provideInvalidVersionCases
  30. *
  31. * @param mixed $minimum
  32. */
  33. public function testConstructorRejectsInvalidMinimum($minimum)
  34. {
  35. $this->expectException(\InvalidArgumentException::class);
  36. new VersionSpecification($minimum);
  37. }
  38. /**
  39. * @dataProvider provideInvalidVersionCases
  40. *
  41. * @param mixed $maximum
  42. */
  43. public function testConstructorRejectsInvalidMaximum($maximum)
  44. {
  45. $this->expectException(\InvalidArgumentException::class);
  46. new VersionSpecification(
  47. PHP_VERSION_ID,
  48. $maximum
  49. );
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function provideInvalidVersionCases()
  55. {
  56. return [
  57. 'negative' => [-1],
  58. 'zero' => [0],
  59. 'float' => [3.14],
  60. 'string' => ['foo'],
  61. 'integerish' => ['9000'],
  62. 'array' => [[]],
  63. 'object' => [new \stdClass()],
  64. ];
  65. }
  66. public function testConstructorRejectsMaximumLessThanMinimum()
  67. {
  68. $this->expectException(\InvalidArgumentException::class);
  69. new VersionSpecification(
  70. PHP_VERSION_ID,
  71. PHP_VERSION_ID - 1
  72. );
  73. }
  74. /**
  75. * @dataProvider provideIsSatisfiedByReturnsTrueCases
  76. *
  77. * @param null|int $minimum
  78. * @param null|int $maximum
  79. * @param int $actual
  80. */
  81. public function testIsSatisfiedByReturnsTrue($minimum, $maximum, $actual)
  82. {
  83. $versionSpecification = new VersionSpecification(
  84. $minimum,
  85. $maximum
  86. );
  87. $this->assertTrue($versionSpecification->isSatisfiedBy($actual));
  88. }
  89. /**
  90. * @return array
  91. */
  92. public function provideIsSatisfiedByReturnsTrueCases()
  93. {
  94. return [
  95. 'version-same-as-maximum' => [null, PHP_VERSION_ID, PHP_VERSION_ID],
  96. 'version-same-as-minimum' => [PHP_VERSION_ID, null, PHP_VERSION_ID],
  97. 'version-between-minimum-and-maximum' => [PHP_VERSION_ID - 1, PHP_VERSION_ID + 1, PHP_VERSION_ID],
  98. 'version-same-as-minimum-and-maximum' => [PHP_VERSION_ID, PHP_VERSION_ID, PHP_VERSION_ID],
  99. ];
  100. }
  101. /**
  102. * @dataProvider provideIsSatisfiedByReturnsFalseCases
  103. *
  104. * @param null|int $minimum
  105. * @param null|int $maximum
  106. * @param int $actual
  107. */
  108. public function testIsSatisfiedByReturnsFalse($minimum, $maximum, $actual)
  109. {
  110. $versionSpecification = new VersionSpecification(
  111. $minimum,
  112. $maximum
  113. );
  114. $this->assertFalse($versionSpecification->isSatisfiedBy($actual));
  115. }
  116. /**
  117. * @return array
  118. */
  119. public function provideIsSatisfiedByReturnsFalseCases()
  120. {
  121. return [
  122. 'version-greater-than-maximum' => [null, PHP_VERSION_ID, PHP_VERSION_ID + 1],
  123. 'version-less-than-minimum' => [PHP_VERSION_ID, null, PHP_VERSION_ID - 1],
  124. ];
  125. }
  126. }