ControlStructureBracesFixerTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\ControlStructure;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer>
  20. */
  21. final class ControlStructureBracesFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'if' => [
  36. '<?php if ($foo) { foo(); }',
  37. '<?php if ($foo) foo();',
  38. ];
  39. yield 'else' => [
  40. '<?php
  41. if ($foo) { foo(); }
  42. else { bar(); }',
  43. '<?php
  44. if ($foo) { foo(); }
  45. else bar();',
  46. ];
  47. yield 'elseif' => [
  48. '<?php
  49. if ($foo) { foo(); }
  50. elseif ($bar) { bar(); }',
  51. '<?php
  52. if ($foo) { foo(); }
  53. elseif ($bar) bar();',
  54. ];
  55. yield 'else if' => [
  56. '<?php
  57. if ($foo) { foo(); }
  58. else if ($bar) { bar(); }',
  59. '<?php
  60. if ($foo) { foo(); }
  61. else if ($bar) bar();',
  62. ];
  63. yield 'for' => [
  64. '<?php for (;;) { foo(); }',
  65. '<?php for (;;) foo();',
  66. ];
  67. yield 'foreach' => [
  68. '<?php foreach ($foo as $bar) { foo(); }',
  69. '<?php foreach ($foo as $bar) foo();',
  70. ];
  71. yield 'while' => [
  72. '<?php while ($foo) { foo(); }',
  73. '<?php while ($foo) foo();',
  74. ];
  75. yield 'do while' => [
  76. '<?php
  77. do { foo(); }
  78. while ($foo);',
  79. '<?php
  80. do foo();
  81. while ($foo);',
  82. ];
  83. yield 'empty if' => [
  84. '<?php if ($foo);',
  85. ];
  86. yield 'empty else' => [
  87. '<?php
  88. if ($foo) { foo(); }
  89. else;',
  90. ];
  91. yield 'empty elseif' => [
  92. '<?php
  93. if ($foo) { foo(); }
  94. elseif ($bar);',
  95. ];
  96. yield 'empty else if' => [
  97. '<?php
  98. if ($foo) { foo(); }
  99. else if ($bar);',
  100. ];
  101. yield 'empty for' => [
  102. '<?php for (;;);',
  103. ];
  104. yield 'empty foreach' => [
  105. '<?php foreach ($foo as $bar);',
  106. ];
  107. yield 'empty while' => [
  108. '<?php while ($foo);',
  109. ];
  110. yield 'empty do while' => [
  111. '<?php do; while ($foo);',
  112. ];
  113. yield 'nested if using alternative syntax' => [
  114. '<?php if ($foo) { if ($bar): ?> foo <?php endif; } ?>',
  115. '<?php if ($foo) if ($bar): ?> foo <?php endif; ?>',
  116. ];
  117. yield 'nested for using alternative syntax' => [
  118. '<?php if ($foo) { for (;;): ?> foo <?php endfor; } ?>',
  119. '<?php if ($foo) for (;;): ?> foo <?php endfor; ?>',
  120. ];
  121. yield 'nested foreach using alternative syntax' => [
  122. '<?php if ($foo) { foreach ($foo as $bar): ?> foo <?php endforeach; } ?>',
  123. '<?php if ($foo) foreach ($foo as $bar): ?> foo <?php endforeach; ?>',
  124. ];
  125. yield 'nested while using alternative syntax' => [
  126. '<?php if ($foo) { while ($foo): ?> foo <?php endwhile; } ?>',
  127. '<?php if ($foo) while ($foo): ?> foo <?php endwhile; ?>',
  128. ];
  129. yield 'nested switch using alternative syntax' => [
  130. '<?php if ($foo) { switch ($foo): case 1: ?> foo <?php endswitch; } ?>',
  131. '<?php if ($foo) switch ($foo): case 1: ?> foo <?php endswitch; ?>',
  132. ];
  133. yield 'declare followed by closing tag' => [
  134. '<?php declare(strict_types=1) ?>',
  135. ];
  136. }
  137. }