ElseifFixerTest.php 2.6 KB

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