EmptyLoopBodyFixerTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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\EmptyLoopBodyFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\EmptyLoopBodyFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\EmptyLoopBodyFixer
  22. */
  23. final class EmptyLoopBodyFixerTest 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. if ([] === $config) {
  33. $this->doTest($expected, $input);
  34. $this->fixer->configure(['style' => 'braces']);
  35. if (null === $input) {
  36. $this->doTest($expected, $input);
  37. } else {
  38. $this->doTest($input, $expected);
  39. }
  40. } else {
  41. $this->fixer->configure($config);
  42. $this->doTest($expected, $input);
  43. }
  44. }
  45. public static function provideFixCases(): iterable
  46. {
  47. yield 'simple "while"' => [
  48. '<?php while(foo());',
  49. '<?php while(foo()){}',
  50. ];
  51. yield 'simple "for"' => [
  52. '<?php for($i = 0;foo();++$i);',
  53. '<?php for($i = 0;foo();++$i){}',
  54. ];
  55. yield 'simple "foreach"' => [
  56. '<?php foreach (Foo() as $f);',
  57. '<?php foreach (Foo() as $f){}',
  58. ];
  59. yield '"while" followed by "do-while"' => [
  60. '<?php while(foo(static function(){})); do{ echo 1; }while(bar());',
  61. '<?php while(foo(static function(){})){} do{ echo 1; }while(bar());',
  62. ];
  63. yield 'empty "while" after "if"' => [
  64. '<?php
  65. if ($foo) {
  66. echo $bar;
  67. } while(foo());
  68. ',
  69. '<?php
  70. if ($foo) {
  71. echo $bar;
  72. } while(foo()){}
  73. ',
  74. ];
  75. yield 'nested and mixed loops' => [
  76. '<?php
  77. do {
  78. while($foo()) {
  79. while(B()); // fix
  80. for($i = 0;foo();++$i); // fix
  81. for($i = 0;foo();++$i) {
  82. foreach (Foo() as $f); // fix
  83. }
  84. }
  85. } while(foo());
  86. ',
  87. '<?php
  88. do {
  89. while($foo()) {
  90. while(B()){} // fix
  91. for($i = 0;foo();++$i){} // fix
  92. for($i = 0;foo();++$i) {
  93. foreach (Foo() as $f){} // fix
  94. }
  95. }
  96. } while(foo());
  97. ',
  98. ];
  99. yield 'not empty "while"' => [
  100. '<?php while(foo()){ bar(); };',
  101. ];
  102. yield 'not empty "for"' => [
  103. '<?php for($i = 0; foo(); ++$i){ bar(); }',
  104. ];
  105. yield 'not empty "foreach"' => [
  106. '<?php foreach (foo() as $f){ echo 1; }',
  107. ];
  108. yield 'test with lot of space' => [
  109. '<?php while (foo1())
  110. ;
  111. echo 1;
  112. ',
  113. '<?php while (foo1())
  114. {
  115. }
  116. echo 1;
  117. ',
  118. ['style' => 'semicolon'],
  119. ];
  120. yield 'empty "foreach" with comment' => [
  121. '<?php foreach (Foo() as $f) {
  122. // $this->add($f);
  123. }',
  124. ];
  125. }
  126. }