DeclareEqualNormalizeFixerTest.php 4.0 KB

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