EmptyLoopConditionFixerTest.php 3.8 KB

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