ElseifFixerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * @author Leszek Prabucki <leszek.prabucki@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\ElseifFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\ElseifFixer>
  22. */
  23. final class ElseifFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield ['<?php if ($some) { $test = true; } else { $test = false; }'];
  38. yield [
  39. '<?php if ($some) { $test = true; } elseif ($some !== "test") { $test = false; }',
  40. '<?php if ($some) { $test = true; } else if ($some !== "test") { $test = false; }',
  41. ];
  42. yield [
  43. '<?php if ($some) { $test = true; } elseif ($some !== "test") { $test = false; }',
  44. '<?php if ($some) { $test = true; } else if ($some !== "test") { $test = false; }',
  45. ];
  46. yield [
  47. '<?php $js = \'if (foo.a) { foo.a = "OK"; } else if (foo.b) { foo.b = "OK"; }\';',
  48. ];
  49. yield [
  50. '<?php
  51. if ($a) {
  52. $x = 1;
  53. } elseif ($b) {
  54. $x = 2;
  55. }',
  56. '<?php
  57. if ($a) {
  58. $x = 1;
  59. } else
  60. if ($b) {
  61. $x = 2;
  62. }',
  63. ];
  64. yield [
  65. '<?php
  66. if ($a) {
  67. } elseif/**/ ($b) {
  68. }
  69. ',
  70. '<?php
  71. if ($a) {
  72. } else /**/ if ($b) {
  73. }
  74. ',
  75. ];
  76. yield [
  77. '<?php
  78. if ($a) {
  79. } elseif//
  80. ($b) {
  81. }
  82. ',
  83. '<?php
  84. if ($a) {
  85. } else //
  86. if ($b) {
  87. }
  88. ',
  89. ];
  90. yield [
  91. '<?php if ($a) {} /**/elseif ($b){}',
  92. '<?php if ($a) {} /**/else if ($b){}',
  93. ];
  94. yield ['<?php if ($x) { foo(); } else if ($y): bar(); endif;'];
  95. }
  96. }