DeclareEqualNormalizeFixerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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\Fixer\LanguageConstruct;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\LanguageConstruct\DeclareEqualNormalizeFixer
  21. */
  22. final class DeclareEqualNormalizeFixerTest extends AbstractFixerTestCase
  23. {
  24. /**
  25. * @param array<string, mixed> $config
  26. *
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input, array $config): void
  30. {
  31. $this->fixer->configure($config);
  32. $this->doTest($expected, $input);
  33. }
  34. public static function provideFixCases(): iterable
  35. {
  36. yield 'minimal case remove whitespace (default config)' => [
  37. '<?php declare(ticks=1);',
  38. '<?php declare(ticks= 1);',
  39. [],
  40. ];
  41. yield 'minimal case remove whitespace (no space config)' => [
  42. '<?php declare(ticks=1);',
  43. '<?php declare(ticks = 1);',
  44. ['space' => 'none'],
  45. ];
  46. yield 'minimal case add whitespace' => [
  47. '<?php declare(ticks = 1);',
  48. '<?php declare(ticks=1);',
  49. ['space' => 'single'],
  50. ];
  51. yield 'to much whitespace case add whitespace' => [
  52. '<?php declare(ticks = 1);',
  53. "<?php declare(ticks\n\t = 1);",
  54. ['space' => 'single'],
  55. ];
  56. yield 'repeating case remove whitespace (default config)' => [
  57. '<?php declare(ticks=1);declare(ticks=1)?>',
  58. '<?php declare(ticks= 1);declare(ticks= 1)?>',
  59. [],
  60. ];
  61. yield 'repeating case add whitespace' => [
  62. '<?php declare ( ticks = 1 );declare( ticks = 1) ?>',
  63. '<?php declare ( ticks=1 );declare( ticks =1) ?>',
  64. ['space' => 'single'],
  65. ];
  66. yield 'minimal case add whitespace comments, single' => [
  67. '<?php declare(ticks#
  68. = #
  69. 1#
  70. );',
  71. '<?php declare(ticks#
  72. =#
  73. 1#
  74. );',
  75. ['space' => 'single'],
  76. ];
  77. yield 'minimal case add whitespace comments, none' => [
  78. '<?php declare(ticks#
  79. =#
  80. 1#
  81. );',
  82. null,
  83. ['space' => 'none'],
  84. ];
  85. yield 'declare having multiple directives, single' => [
  86. '<?php declare(strict_types=1, ticks=1);',
  87. '<?php declare(strict_types = 1, ticks = 1);',
  88. [],
  89. ];
  90. yield 'declare having multiple directives, none' => [
  91. '<?php declare(strict_types = 1, ticks = 1);',
  92. '<?php declare(strict_types=1, ticks=1);',
  93. ['space' => 'single'],
  94. ];
  95. }
  96. /**
  97. * @param array<mixed> $config
  98. *
  99. * @dataProvider provideInvalidConfigCases
  100. */
  101. public function testInvalidConfig(array $config, string $expectedMessage): void
  102. {
  103. $this->expectException(InvalidFixerConfigurationException::class);
  104. $this->expectExceptionMessage(sprintf('[declare_equal_normalize] Invalid configuration: %s', $expectedMessage));
  105. $this->fixer->configure($config);
  106. }
  107. public static function provideInvalidConfigCases(): iterable
  108. {
  109. yield [
  110. [1, 2],
  111. 'The options "0", "1" do not exist.',
  112. ];
  113. yield [
  114. ['space' => 'tab'],
  115. 'The option "space" with value "tab" is invalid.',
  116. ];
  117. }
  118. }