NoAlternativeSyntaxFixerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. * @author Eddilbert Macharia <edd.cowan@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\NoAlternativeSyntaxFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoAlternativeSyntaxFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoAlternativeSyntaxFixer
  24. */
  25. final class NoAlternativeSyntaxFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @param _AutogeneratedInputConfiguration $configuration
  29. *
  30. * @dataProvider provideFixCases
  31. */
  32. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  33. {
  34. $this->fixer->configure($configuration);
  35. $this->doTest($expected, $input);
  36. }
  37. public static function provideFixCases(): iterable
  38. {
  39. yield [
  40. '<?php
  41. declare(ticks = 1) {
  42. }
  43. ',
  44. '<?php
  45. declare(ticks = 1) :
  46. enddeclare;
  47. ',
  48. ];
  49. yield [
  50. '<?php
  51. switch ($foo) {
  52. case 1:
  53. }
  54. switch ($foo) {
  55. case 1:
  56. } ?>',
  57. '<?php
  58. switch ($foo):
  59. case 1:
  60. endswitch;
  61. switch ($foo) :
  62. case 1:
  63. endswitch ?>',
  64. ];
  65. yield [
  66. '<?php
  67. if ($some1) {
  68. if ($some2) {
  69. if ($some3) {
  70. $test = true;
  71. }
  72. }
  73. }
  74. ',
  75. '<?php
  76. if ($some1) :
  77. if ($some2) :
  78. if ($some3) :
  79. $test = true;
  80. endif;
  81. endif;
  82. endif;
  83. ',
  84. ];
  85. yield [
  86. '<?php if ($some) { $test = true; } else { $test = false; }',
  87. ];
  88. yield [
  89. '<?php if ($some) /* foo */ { $test = true; } else { $test = false; }',
  90. '<?php if ($some) /* foo */ : $test = true; else :$test = false; endif;',
  91. ];
  92. yield [
  93. '<?php if ($some) { $test = true; } else { $test = false; }',
  94. '<?php if ($some) : $test = true; else :$test = false; endif;',
  95. ];
  96. yield [
  97. '<?php if ($some) { if($test){echo $test;}$test = true; } else { $test = false; }',
  98. '<?php if ($some) : if($test){echo $test;}$test = true; else : $test = false; endif;',
  99. ];
  100. yield [
  101. '<?php foreach (array("d") as $item) { echo $item;}',
  102. '<?php foreach (array("d") as $item):echo $item;endforeach;',
  103. ];
  104. yield [
  105. '<?php foreach (array("d") as $item) { if($item){echo $item;}}',
  106. '<?php foreach (array("d") as $item):if($item){echo $item;}endforeach;',
  107. ];
  108. yield [
  109. '<?php while (true) { echo "c";}',
  110. '<?php while (true):echo "c";endwhile;',
  111. ];
  112. yield [
  113. '<?php foreach (array("d") as $item) { while ($item) { echo "dd";}}',
  114. '<?php foreach (array("d") as $item):while ($item):echo "dd";endwhile;endforeach;',
  115. ];
  116. yield [
  117. '<?php foreach (array("d") as $item) { while ($item) { echo "dd" ; } }',
  118. '<?php foreach (array("d") as $item): while ($item) : echo "dd" ; endwhile; endforeach;',
  119. ];
  120. yield [
  121. '<?php if ($some) { $test = true; } elseif ($some !== "test") { $test = false; }',
  122. '<?php if ($some) : $test = true; elseif ($some !== "test") : $test = false; endif;',
  123. ];
  124. yield [
  125. '<?php if ($condition) { ?><p>This is visible.</p><?php } ?>',
  126. '<?php if ($condition): ?><p>This is visible.</p><?php endif; ?>',
  127. ];
  128. yield [
  129. '<?php if ($condition): ?><p>This is visible.</p><?php endif; ?>',
  130. null,
  131. ['fix_non_monolithic_code' => false],
  132. ];
  133. yield [
  134. '<?php if (true) { ?>Text display.<?php } ?>',
  135. '<?php if (true): ?>Text display.<?php endif; ?>',
  136. ['fix_non_monolithic_code' => true],
  137. ];
  138. yield [
  139. '<?php if (true): ?>Text display.<?php endif; ?>',
  140. null,
  141. ['fix_non_monolithic_code' => false],
  142. ];
  143. yield [
  144. '<?php if ($condition) { ?><?= "xd"; ?><?php } ?>',
  145. '<?php if ($condition): ?><?= "xd"; ?><?php endif; ?>',
  146. ['fix_non_monolithic_code' => true],
  147. ];
  148. yield [
  149. '<?php if ($condition): ?><?= "xd"; ?><?php endif; ?>',
  150. null,
  151. ['fix_non_monolithic_code' => false],
  152. ];
  153. }
  154. }