ElseifFixerTest.php 2.7 KB

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