DeprecatedFixerOptionTest.php 5.0 KB

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