ControlStructureBracesFixerTest.php 4.1 KB

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