AliasedFixerOptionTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 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 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 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 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. * @dataProvider provideGetAllowedTypesCases
  99. */
  100. public function testGetAllowedTypes(?array $allowedTypes): void
  101. {
  102. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, $allowedTypes), 'baz');
  103. static::assertSame($allowedTypes, $option->getAllowedTypes());
  104. }
  105. public function provideGetAllowedTypesCases(): array
  106. {
  107. return [
  108. [null],
  109. [['bool']],
  110. [['bool', 'string']],
  111. ];
  112. }
  113. /**
  114. * @dataProvider provideGetAllowedValuesCases
  115. */
  116. public function testGetAllowedValues(?array $allowedValues): void
  117. {
  118. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, null, $allowedValues), 'baz');
  119. static::assertSame($allowedValues, $option->getAllowedValues());
  120. }
  121. public function provideGetAllowedValuesCases(): array
  122. {
  123. return [
  124. [null],
  125. [['baz']],
  126. [['baz', 'qux']],
  127. ];
  128. }
  129. public function testGetAllowedValuesClosure(): void
  130. {
  131. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, null, [static function (): void {}]), 'baz');
  132. $allowedTypes = $option->getAllowedValues();
  133. static::assertIsArray($allowedTypes);
  134. static::assertCount(1, $allowedTypes);
  135. static::assertArrayHasKey(0, $allowedTypes);
  136. static::assertInstanceOf(\Closure::class, $allowedTypes[0]);
  137. }
  138. public function testGetNormalizers(): void
  139. {
  140. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.'), 'baz');
  141. static::assertNull($option->getNormalizer());
  142. $option = new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, null, null, null, static function (): void {}), 'baz');
  143. static::assertInstanceOf(\Closure::class, $option->getNormalizer());
  144. }
  145. /**
  146. * @dataProvider provideGetAliasCases
  147. */
  148. public function testGetAlias(string $alias): void
  149. {
  150. $options = new AliasedFixerOption(new FixerOption('foo', 'Bar', true, null, null, null, null), $alias);
  151. static::assertSame($alias, $options->getAlias());
  152. }
  153. public function provideGetAliasCases(): array
  154. {
  155. return [
  156. ['bar'],
  157. ['baz'],
  158. ];
  159. }
  160. public function testRequiredWithDefaultValue(): void
  161. {
  162. $this->expectException(\LogicException::class);
  163. $this->expectExceptionMessage('Required options cannot have a default value.');
  164. new AliasedFixerOption(new FixerOption('foo', 'Bar.', true, false), 'baz');
  165. }
  166. }