SemicolonAfterInstructionFixerTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\Semicolon;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Semicolon\SemicolonAfterInstructionFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Semicolon\SemicolonAfterInstructionFixer>
  20. */
  21. final class SemicolonAfterInstructionFixerTest 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. /**
  31. * @return iterable<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'comment' => [
  36. '<?php $a++;//a ?>',
  37. '<?php $a++//a ?>',
  38. ];
  39. yield 'comment II' => [
  40. '<?php $b++; /**/ ?>',
  41. '<?php $b++ /**/ ?>',
  42. ];
  43. yield 'no space' => [
  44. '<?php $b++;?>',
  45. '<?php $b++?>',
  46. ];
  47. yield [
  48. '<?php echo 123; ?>',
  49. '<?php echo 123 ?>',
  50. ];
  51. yield [
  52. "<?php echo 123;\n\t?>",
  53. "<?php echo 123\n\t?>",
  54. ];
  55. yield ['<?php ?>'];
  56. yield ['<?php ; ?>'];
  57. yield ['<?php if($a){}'];
  58. yield ['<?php while($a > $b){}'];
  59. yield [
  60. '<?php if ($a == 5): ?>
  61. A is equal to 5
  62. <?php endif; ?>
  63. <?php switch ($foo): ?>
  64. <?php case 1: ?>
  65. ...
  66. <?php endswitch; ?>',
  67. '<?php if ($a == 5): ?>
  68. A is equal to 5
  69. <?php endif; ?>
  70. <?php switch ($foo): ?>
  71. <?php case 1: ?>
  72. ...
  73. <?php endswitch ?>',
  74. ];
  75. yield [
  76. '<?php if ($a == 5) { ?>
  77. A is equal to 5
  78. <?php } ?>',
  79. ];
  80. }
  81. /**
  82. * @dataProvider provideFixPre80Cases
  83. *
  84. * @requires PHP <8.0
  85. */
  86. public function testFixPre80(string $expected, ?string $input = null): void
  87. {
  88. $this->doTest($expected, $input);
  89. }
  90. /**
  91. * @return iterable<array{string, string}>
  92. */
  93. public static function provideFixPre80Cases(): iterable
  94. {
  95. yield [
  96. '<?php $a = [1,2,3]; echo $a{1}; ?>',
  97. '<?php $a = [1,2,3]; echo $a{1} ?>',
  98. ];
  99. }
  100. public function testOpenWithEcho(): void
  101. {
  102. if (!\ini_get('short_open_tag')) {
  103. self::markTestSkipped('The short_open_tag option is required to be enabled.');
  104. }
  105. $this->doTest(
  106. "<?= '1_'; ?> <?php ?><?= 1; ?>",
  107. "<?= '1_' ?> <?php ?><?= 1; ?>"
  108. );
  109. }
  110. }