FullOpeningTagFixerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\PhpTag;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\PhpTag\FullOpeningTagFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\FullOpeningTagFixer>
  20. */
  21. final class FullOpeningTagFixerTest 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 ['<?php echo \'Foo\';', '<? echo \'Foo\';'];
  36. yield ['<?php echo \'Foo\';', '<?pHp echo \'Foo\';'];
  37. yield ['<?= \'Foo\';'];
  38. yield ['<?php echo \'Foo\'; ?> PLAIN TEXT'];
  39. yield ['PLAIN TEXT<?php echo \'Foo\'; ?>'];
  40. yield ['<?php $query = "SELECT .... FROM my_table WHERE id <? LIMIT 1";', '<? $query = "SELECT .... FROM my_table WHERE id <? LIMIT 1";'];
  41. yield ['<?php
  42. echo \'Foo\';
  43. ',
  44. '<?
  45. echo \'Foo\';
  46. ',
  47. ];
  48. yield [
  49. "<?php if ('<?php' === '<?') { }",
  50. "<? if ('<?php' === '<?') { }",
  51. ];
  52. yield [
  53. '<?php // <?php',
  54. '<?pHP // <?php',
  55. ];
  56. yield [
  57. "<?php
  58. '<?
  59. ';",
  60. ];
  61. yield [
  62. '<?php
  63. // Replace all <? with <?php !',
  64. ];
  65. yield [
  66. '<?php
  67. // Replace all <? with <?pHp !',
  68. ];
  69. yield [
  70. '<?php
  71. /**
  72. * Convert <?= ?> to long-form <?php echo ?> and <?php ?> to <?php ?>
  73. *
  74. */',
  75. ];
  76. yield [
  77. "<?php \$this->data = preg_replace('/<\\?(?!xml|php)/s', '<?php ', \$this->data);",
  78. ];
  79. yield [
  80. 'foo <?php echo "-"; echo "aaa <?php bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <?php echo "<? ";',
  81. ];
  82. yield [
  83. '<?php
  84. $a = <<< "TEST"
  85. <?Php <?
  86. TEST;?>
  87. TEST;
  88. ?>
  89. <?php $a = <<< \'TEST\'
  90. <?PHP <?
  91. TEST;?>
  92. TEST;
  93. ?>
  94. ',
  95. ];
  96. yield 'binary string' => [
  97. '<?php echo b\'Foo\';',
  98. '<? echo b\'Foo\';',
  99. ];
  100. yield ['<?php', '<?'];
  101. yield ["<?php\n", "<?\n"];
  102. yield ["<?php \n", "<? \n"];
  103. yield ["<?php \n?><?= 1?>", "<? \n?><?= 1?>"];
  104. yield [
  105. 'foo <?php echo "-"; echo "aaa <? bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <?php echo "<? ";',
  106. 'foo <? echo "-"; echo "aaa <? bbb <? ccc"; echo \'<? \'; /* <? */ /** <? */ ?> bar <? echo "<? ";',
  107. ];
  108. }
  109. }