EmptyLoopBodyFixerTest.php 3.2 KB

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