EmptyLoopBodyFixerTest.php 3.0 KB

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