DeprecatedFixerOptionTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\FixerConfiguration;
  13. use PhpCsFixer\FixerConfiguration\DeprecatedFixerOption;
  14. use PhpCsFixer\FixerConfiguration\FixerOption;
  15. use PhpCsFixer\FixerConfiguration\FixerOptionInterface;
  16. use PhpCsFixer\Tests\TestCase;
  17. /**
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\FixerConfiguration\DeprecatedFixerOption
  21. */
  22. final class DeprecatedFixerOptionTest extends TestCase
  23. {
  24. public function testGetName(): void
  25. {
  26. $option = new DeprecatedFixerOption(
  27. new FixerOption('foo', 'Foo.'),
  28. 'deprecated'
  29. );
  30. self::assertSame('foo', $option->getName());
  31. }
  32. public function testGetDescription(): void
  33. {
  34. $option = new DeprecatedFixerOption(
  35. new FixerOption('foo', 'Foo.'),
  36. 'deprecated'
  37. );
  38. self::assertSame('Foo.', $option->getDescription());
  39. }
  40. /**
  41. * @dataProvider provideHasDefaultCases
  42. */
  43. public function testHasDefault(bool $isRequired): void
  44. {
  45. $option = new DeprecatedFixerOption(
  46. new FixerOption('foo', 'Foo.', $isRequired),
  47. 'deprecated'
  48. );
  49. self::assertSame(!$isRequired, $option->hasDefault());
  50. }
  51. /**
  52. * @return iterable<array{bool}>
  53. */
  54. public static function provideHasDefaultCases(): iterable
  55. {
  56. yield [true];
  57. yield [false];
  58. }
  59. /**
  60. * @param mixed $default
  61. *
  62. * @dataProvider provideGetDefaultCases
  63. */
  64. public function testGetDefault($default): void
  65. {
  66. $option = new DeprecatedFixerOption(
  67. new FixerOption('foo', 'Foo.', false, $default),
  68. 'deprecated'
  69. );
  70. self::assertSame($default, $option->getDefault());
  71. }
  72. /**
  73. * @return iterable<array{bool|string}>
  74. */
  75. public static function provideGetDefaultCases(): iterable
  76. {
  77. yield ['foo'];
  78. yield [true];
  79. }
  80. public function testGetAllowedTypes(): void
  81. {
  82. $allowedTypes = ['string', 'bool'];
  83. $option = new DeprecatedFixerOption(
  84. new FixerOption('foo', 'Foo.', true, null, $allowedTypes),
  85. 'deprecated'
  86. );
  87. self::assertSame($allowedTypes, $option->getAllowedTypes());
  88. }
  89. public function testGetAllowedValues(): void
  90. {
  91. $allowedValues = ['string', 'bool'];
  92. $option = new DeprecatedFixerOption(
  93. new FixerOption('foo', 'Foo.', true, null, [], $allowedValues),
  94. 'deprecated'
  95. );
  96. self::assertSame($allowedValues, $option->getAllowedValues());
  97. }
  98. public function testGetNormalizer(): void
  99. {
  100. $normalizer = static fn () => null;
  101. $option = new DeprecatedFixerOption(
  102. $this->createFixerOptionDouble($normalizer),
  103. 'deprecated'
  104. );
  105. self::assertSame($normalizer, $option->getNormalizer());
  106. }
  107. public function testGetDeprecationMessage(): void
  108. {
  109. $option = new DeprecatedFixerOption(
  110. new FixerOption('foo', 'Foo.'),
  111. 'Use option "bar" instead.'
  112. );
  113. self::assertSame('Use option "bar" instead.', $option->getDeprecationMessage());
  114. }
  115. private function createFixerOptionDouble(\Closure $normalizer): FixerOptionInterface
  116. {
  117. return new class($normalizer) implements FixerOptionInterface {
  118. private \Closure $normalizer;
  119. public function __construct(\Closure $normalizer)
  120. {
  121. $this->normalizer = $normalizer;
  122. }
  123. public function getName(): string
  124. {
  125. throw new \LogicException('Not implemented.');
  126. }
  127. public function getDescription(): string
  128. {
  129. throw new \LogicException('Not implemented.');
  130. }
  131. public function hasDefault(): bool
  132. {
  133. throw new \LogicException('Not implemented.');
  134. }
  135. public function getDefault(): void
  136. {
  137. throw new \LogicException('Not implemented.');
  138. }
  139. public function getAllowedTypes(): ?array
  140. {
  141. throw new \LogicException('Not implemented.');
  142. }
  143. public function getAllowedValues(): ?array
  144. {
  145. throw new \LogicException('Not implemented.');
  146. }
  147. public function getNormalizer(): \Closure
  148. {
  149. return $this->normalizer;
  150. }
  151. };
  152. }
  153. }