NoAlternativeSyntaxFixerTest.php 4.8 KB

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