AliasedFixerOptionTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\AliasedFixerOption;
  14. use PhpCsFixer\FixerConfiguration\FixerOption;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author ntzm
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\FixerConfiguration\AliasedFixerOption
  22. */
  23. final class AliasedFixerOptionTest extends TestCase
  24. {
  25. /**
  26. * @dataProvider provideGetNameCases
  27. */
  28. public function testGetName(string $name): void
  29. {
  30. $option = new AliasedFixerOption(new FixerOption($name, 'Bar.'), 'baz');
  31. static::assertSame($name, $option->getName());
  32. }
  33. public static function provideGetNameCases(): array
  34. {
  35. return [
  36. ['foo'],
  37. ['bar'],
  38. ];
  39. }
  40. /**
  41. * @dataProvider provideGetDescriptionCases
  42. */
  43. public function testGetDescription(string $description): void
  44. {
  45. $option = new AliasedFixerOption(new FixerOption('foo', $description), 'baz');
  46. static::assertSame($description, $option->getDescription());
  47. }
  48. public static function provideGetDescriptionCases(): array
  49. {
  50. return [
  51. ['Foo.'],
  52. ['Bar.'],
  53. ];
  54. }
  55. /**
  56. * @dataProvider provideHasDefaultCases
  57. */
  58. public function testHasDefault(bool $hasDefault, AliasedFixerOption $input): void
  59. {
  60. static::assertSame($hasDefault, $input->hasDefault());
  61. }
  62. public static function provideHasDefaultCases(): array
  63. {
  64. return [
  65. [
  66. false,
  67. new AliasedFixerOption(new FixerOption('foo', 'Bar.'), 'baz'),
  68. ],
  69. [
  70. true,
  71. new AliasedFixerOption(new FixerOption('foo', 'Bar.', false, 'baz'), 'baz'),
  72. ],
  73. ];
  74. }
  75. /**
  76. * @dataProvider provideGetDefaultCases
  77. */
  78. public function testGetDefault(string $default): void
  79. {
  80. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', false, $default), 'baz');
  81. static::assertSame($default, $option->getDefault());
  82. }
  83. public static function provideGetDefaultCases(): array
  84. {
  85. return [
  86. ['baz'],
  87. ['foo'],
  88. ];
  89. }
  90. public function testGetUndefinedDefault(): void
  91. {
  92. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.'), 'baz');
  93. $this->expectException(\LogicException::class);
  94. $this->expectExceptionMessage('No default value defined.');
  95. $option->getDefault();
  96. }
  97. /**
  98. * @param null|list<string> $allowedTypes
  99. *
  100. * @dataProvider provideGetAllowedTypesCases
  101. */
  102. public function testGetAllowedTypes(?array $allowedTypes): void
  103. {
  104. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, $allowedTypes), 'baz');
  105. static::assertSame($allowedTypes, $option->getAllowedTypes());
  106. }
  107. public static function provideGetAllowedTypesCases(): array
  108. {
  109. return [
  110. [null],
  111. [['bool']],
  112. [['bool', 'string']],
  113. ];
  114. }
  115. /**
  116. * @param list<(callable(mixed): bool)|null|scalar>|null $allowedValues
  117. *
  118. * @dataProvider provideGetAllowedValuesCases
  119. */
  120. public function testGetAllowedValues(?array $allowedValues): void
  121. {
  122. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, null, $allowedValues), 'baz');
  123. static::assertSame($allowedValues, $option->getAllowedValues());
  124. }
  125. public static function provideGetAllowedValuesCases(): array
  126. {
  127. return [
  128. [null],
  129. [['baz']],
  130. [['baz', 'qux']],
  131. ];
  132. }
  133. public function testGetAllowedValuesClosure(): void
  134. {
  135. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, null, [static fn () => true]), 'baz');
  136. $allowedTypes = $option->getAllowedValues();
  137. static::assertIsArray($allowedTypes);
  138. static::assertCount(1, $allowedTypes);
  139. static::assertArrayHasKey(0, $allowedTypes);
  140. static::assertInstanceOf(\Closure::class, $allowedTypes[0]);
  141. }
  142. public function testGetNormalizers(): void
  143. {
  144. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.'), 'baz');
  145. static::assertNull($option->getNormalizer());
  146. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, null, null, static fn () => null), 'baz');
  147. static::assertInstanceOf(\Closure::class, $option->getNormalizer());
  148. }
  149. /**
  150. * @dataProvider provideGetAliasCases
  151. */
  152. public function testGetAlias(string $alias): void
  153. {
  154. $options = new AliasedFixerOption(new FixerOption('foo', 'Bar', true, null, null, null, null), $alias);
  155. static::assertSame($alias, $options->getAlias());
  156. }
  157. public static function provideGetAliasCases(): array
  158. {
  159. return [
  160. ['bar'],
  161. ['baz'],
  162. ];
  163. }
  164. public function testRequiredWithDefaultValue(): void
  165. {
  166. $this->expectException(\LogicException::class);
  167. $this->expectExceptionMessage('Required options cannot have a default value.');
  168. new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, false), 'baz');
  169. }
  170. }