AliasedFixerOptionTest.php 5.5 KB

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