FixerOptionTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\FixerOption;
  14. use PhpCsFixer\Tests\TestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\FixerConfiguration\FixerOption
  19. */
  20. final class FixerOptionTest extends TestCase
  21. {
  22. public function testGetName(): void
  23. {
  24. $option = new FixerOption('foo', 'Bar.');
  25. self::assertSame('foo', $option->getName());
  26. }
  27. public function testGetDescription(): void
  28. {
  29. $option = new FixerOption('foo', 'Bar.');
  30. self::assertSame('Bar.', $option->getDescription());
  31. }
  32. public function testHasDefault(): void
  33. {
  34. $option = new FixerOption('foo', 'Bar.');
  35. self::assertFalse($option->hasDefault());
  36. $option = new FixerOption('foo', 'Bar.', false, 'baz');
  37. self::assertTrue($option->hasDefault());
  38. }
  39. public function testGetDefault(): void
  40. {
  41. $option = new FixerOption('foo', 'Bar.', false, 'baz');
  42. self::assertSame('baz', $option->getDefault());
  43. }
  44. public function testGetUndefinedDefault(): void
  45. {
  46. $option = new FixerOption('foo', 'Bar.');
  47. $this->expectException(\LogicException::class);
  48. $this->expectExceptionMessage('No default value defined.');
  49. $option->getDefault();
  50. }
  51. public function testGetAllowedTypes(): void
  52. {
  53. $option = new FixerOption('foo', 'Bar.');
  54. self::assertNull($option->getAllowedTypes());
  55. $option = new FixerOption('foo', 'Bar.', true, null, ['bool']);
  56. self::assertSame(['bool'], $option->getAllowedTypes());
  57. $option = new FixerOption('foo', 'Bar.', true, null, ['bool', 'string']);
  58. self::assertSame(['bool', 'string'], $option->getAllowedTypes());
  59. }
  60. public function testGetAllowedValues(): void
  61. {
  62. $option = new FixerOption('foo', 'Bar.');
  63. self::assertNull($option->getAllowedValues());
  64. $option = new FixerOption('foo', 'Bar.', true, null, null, ['baz']);
  65. self::assertSame(['baz'], $option->getAllowedValues());
  66. $option = new FixerOption('foo', 'Bar.', true, null, null, ['baz', 'qux']);
  67. self::assertSame(['baz', 'qux'], $option->getAllowedValues());
  68. $option = new FixerOption('foo', 'Bar.', true, null, null, [static fn () => true]);
  69. $allowedTypes = $option->getAllowedValues();
  70. self::assertIsArray($allowedTypes);
  71. self::assertCount(1, $allowedTypes);
  72. self::assertArrayHasKey(0, $allowedTypes);
  73. self::assertInstanceOf(\Closure::class, $allowedTypes[0]);
  74. }
  75. public function testGetNormalizers(): void
  76. {
  77. $option = new FixerOption('foo', 'Bar.');
  78. self::assertNull($option->getNormalizer());
  79. $option = new FixerOption('foo', 'Bar.', true, null, null, null, static fn () => null);
  80. self::assertInstanceOf(\Closure::class, $option->getNormalizer());
  81. }
  82. public function testRequiredWithDefaultValue(): void
  83. {
  84. $this->expectException(\LogicException::class);
  85. $this->expectExceptionMessage('Required options cannot have a default value.');
  86. new FixerOption('foo', 'Bar.', true, false);
  87. }
  88. }