FixerConfigurationResolverTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\AllowedValueSubset;
  15. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
  16. use PhpCsFixer\FixerConfiguration\FixerOption;
  17. use PhpCsFixer\Tests\TestCase;
  18. use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
  19. use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
  20. use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
  21. use Symfony\Component\OptionsResolver\Options;
  22. /**
  23. * @internal
  24. *
  25. * @group legacy
  26. *
  27. * @covers \PhpCsFixer\FixerConfiguration\FixerConfigurationResolver
  28. */
  29. final class FixerConfigurationResolverTest extends TestCase
  30. {
  31. public function testWithoutOptions(): void
  32. {
  33. $this->expectException(\LogicException::class);
  34. $this->expectExceptionMessage('Options cannot be empty.');
  35. new FixerConfigurationResolver([]);
  36. }
  37. public function testWithDuplicatesOptions(): void
  38. {
  39. $this->expectException(\LogicException::class);
  40. $this->expectExceptionMessage('The "foo" option is defined multiple times.');
  41. new FixerConfigurationResolver([
  42. new FixerOption('foo', 'Bar-1.'),
  43. new FixerOption('foo', 'Bar-2.'),
  44. ]);
  45. }
  46. public function testWithDuplicateAliasOptions(): void
  47. {
  48. $this->expectException(\LogicException::class);
  49. $this->expectExceptionMessage('The "foo" option is defined multiple times.');
  50. new FixerConfigurationResolver([
  51. new AliasedFixerOption(new FixerOption('foo', 'Bar-1.'), 'baz'),
  52. new FixerOption('foo', 'Bar-2.'),
  53. ]);
  54. }
  55. public function testGetOptions(): void
  56. {
  57. $options = [
  58. new FixerOption('foo', 'Bar.'),
  59. new FixerOption('baz', 'Qux.'),
  60. ];
  61. $configuration = new FixerConfigurationResolver($options);
  62. self::assertSame($options, $configuration->getOptions());
  63. }
  64. public function testResolve(): void
  65. {
  66. $configuration = new FixerConfigurationResolver([
  67. new FixerOption('foo', 'Bar.'),
  68. ]);
  69. self::assertSame(
  70. ['foo' => 'bar'],
  71. $configuration->resolve(['foo' => 'bar'])
  72. );
  73. }
  74. public function testResolveWithMissingRequiredOption(): void
  75. {
  76. $configuration = new FixerConfigurationResolver([
  77. new FixerOption('foo', 'Bar.'),
  78. ]);
  79. $this->expectException(MissingOptionsException::class);
  80. $configuration->resolve([]);
  81. }
  82. public function testResolveWithDefault(): void
  83. {
  84. $configuration = new FixerConfigurationResolver([
  85. new FixerOption('foo', 'Bar.', false, 'baz'),
  86. ]);
  87. self::assertSame(
  88. ['foo' => 'baz'],
  89. $configuration->resolve([])
  90. );
  91. }
  92. public function testResolveWithAllowedTypes(): void
  93. {
  94. $configuration = new FixerConfigurationResolver([
  95. new FixerOption('foo', 'Bar.', true, null, ['int']),
  96. ]);
  97. self::assertSame(
  98. ['foo' => 1],
  99. $configuration->resolve(['foo' => 1])
  100. );
  101. $this->expectException(InvalidOptionsException::class);
  102. $configuration->resolve(['foo' => '1']);
  103. }
  104. public function testResolveWithAllowedValues(): void
  105. {
  106. $configuration = new FixerConfigurationResolver([
  107. new FixerOption('foo', 'Bar.', true, null, null, [true, false]),
  108. ]);
  109. self::assertSame(
  110. ['foo' => true],
  111. $configuration->resolve(['foo' => true])
  112. );
  113. $this->expectException(InvalidOptionsException::class);
  114. $configuration->resolve(['foo' => 1]);
  115. }
  116. public function testResolveWithAllowedValuesSubset(): void
  117. {
  118. $configuration = new FixerConfigurationResolver([
  119. new FixerOption('foo', 'Bar.', true, null, null, [new AllowedValueSubset(['foo', 'bar'])]),
  120. ]);
  121. self::assertSame(
  122. ['foo' => ['bar']],
  123. $configuration->resolve(['foo' => ['bar']])
  124. );
  125. $this->expectException(InvalidOptionsException::class);
  126. $configuration->resolve(['foo' => ['baz']]);
  127. }
  128. public function testResolveWithUndefinedOption(): void
  129. {
  130. $configuration = new FixerConfigurationResolver([
  131. new FixerOption('bar', 'Bar.'),
  132. ]);
  133. $this->expectException(UndefinedOptionsException::class);
  134. $configuration->resolve(['foo' => 'foooo']);
  135. }
  136. public function testResolveWithNormalizers(): void
  137. {
  138. $configuration = new FixerConfigurationResolver([
  139. new FixerOption('foo', 'Bar.', true, null, null, null, static fn (Options $options, string $value): int => (int) $value),
  140. ]);
  141. self::assertSame(
  142. ['foo' => 1],
  143. $configuration->resolve(['foo' => '1'])
  144. );
  145. $exception = new InvalidOptionsException('');
  146. $configuration = new FixerConfigurationResolver([
  147. new FixerOption('foo', 'Bar.', true, null, null, null, static function (Options $options, $value) use ($exception): void {
  148. throw $exception;
  149. }),
  150. ]);
  151. $caught = null;
  152. try {
  153. $configuration->resolve(['foo' => '1']);
  154. } catch (InvalidOptionsException $caught) {
  155. }
  156. self::assertSame($exception, $caught);
  157. }
  158. public function testResolveWithAliasedDuplicateConfig(): void
  159. {
  160. $configuration = new FixerConfigurationResolver([
  161. new AliasedFixerOption(new FixerOption('bar', 'Bar.'), 'baz'),
  162. ]);
  163. $this->expectException(InvalidOptionsException::class);
  164. $this->expectExceptionMessage('Aliased option "bar"/"baz" is passed multiple times');
  165. $configuration->resolve([
  166. 'bar' => '1',
  167. 'baz' => '2',
  168. ]);
  169. }
  170. public function testResolveWithDeprecatedAlias(): void
  171. {
  172. $this->expectDeprecation('Option "baz" is deprecated, use "bar" instead.');
  173. $configuration = new FixerConfigurationResolver([
  174. new AliasedFixerOption(new FixerOption('bar', 'Bar.'), 'baz'),
  175. ]);
  176. $configuration->resolve([
  177. 'baz' => '1',
  178. ]);
  179. }
  180. }