EmptyLoopConditionFixerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\EmptyLoopConditionFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\EmptyLoopConditionFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\EmptyLoopConditionFixer
  22. */
  23. final class EmptyLoopConditionFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @param _AutogeneratedInputConfiguration $config
  27. *
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $config = []): void
  31. {
  32. $this->fixer->configure($config);
  33. $this->doTest($expected, $input);
  34. }
  35. public static function provideFixCases(): iterable
  36. {
  37. yield 'from `for` to `while`' => [
  38. '<?php
  39. while (true){ if(foo()) {break;}}
  40. for(;$i < 1;){ if(foo()) {break;}}',
  41. '<?php
  42. for(;;){ if(foo()) {break;}}
  43. for(;$i < 1;){ if(foo()) {break;}}',
  44. ];
  45. yield 'from `do while` to `while`' => [
  46. '<?php while (true){ if(foo()) {break;}}',
  47. '<?php do{ if(foo()) {break;}}while(true);',
  48. ];
  49. yield 'from `while` to `for`' => [
  50. '<?php
  51. for(;;){ if(foo()) {break;}}
  52. while(false){ echo 1; }
  53. while($a()) { echo 2; }
  54. ',
  55. '<?php
  56. while(true){ if(foo()) {break;}}
  57. while(false){ echo 1; }
  58. while($a()) { echo 2; }
  59. ',
  60. ['style' => 'for'],
  61. ];
  62. yield 'from `do while` to `for`' => [
  63. '<?php for(;;){ if(foo()) {break;}}',
  64. '<?php do{ if(foo()) {break;}}while(true);',
  65. ['style' => 'for'],
  66. ];
  67. yield 'multiple `do while` to `while`' => [
  68. '<?php while (true){}while (true){}while (true){}while (true){}while (true){}while (true){}',
  69. '<?php do{}while(true);do{}while(true);do{}while(true);do{}while(true);do{}while(true);do{}while(true);',
  70. ];
  71. yield 'multiple nested `do while` to `while`' => [
  72. '<?php while (true){while (true){while (true){while (true){while (true){while (true){while (true){}}}}}}}',
  73. '<?php do{do{do{do{do{do{do{}while(true);}while(true);}while(true);}while(true);}while(true);}while(true);}while(true);',
  74. ];
  75. // comment cases
  76. yield 'comment inside empty `for` condition' => [
  77. '<?php while (true)/* 1 *//* 2 */{}',
  78. '<?php for(/* 1 */;;/* 2 */){}',
  79. ];
  80. yield 'comment following empty `for` condition' => [
  81. '<?php for(;;)/* 3 */{}',
  82. '<?php while(true/* 3 */){}',
  83. ['style' => 'for'],
  84. ];
  85. // space cases
  86. yield 'lot of space' => [
  87. '<?php while (true){ foo3(); } ',
  88. '<?php do{ foo3(); } while(true) ; ',
  89. ];
  90. yield [
  91. '<?php
  92. while (true) {
  93. foo1();
  94. }
  95. ',
  96. '<?php
  97. do {
  98. foo1();
  99. }
  100. while(
  101. true
  102. )
  103. ;',
  104. ];
  105. // do not fix cases
  106. yield 'not empty `while` condition' => [
  107. '<?php while(true === foo()){ if(foo()) {break;}}',
  108. null,
  109. ['style' => 'for'],
  110. ];
  111. yield 'not empty `for` condition' => [
  112. '<?php for(;foo();){ if(foo()) {break;}}',
  113. ];
  114. yield 'not empty `do while` condition' => [
  115. '<?php do{ if(foo()) {break;}}while(foo());',
  116. ];
  117. yield '`while` false' => [
  118. '<?php while(false){ if(foo()) {break;}}',
  119. null,
  120. ['style' => 'for'],
  121. ];
  122. }
  123. }