AliasedFixerOptionTest.php 5.3 KB

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