NoClosingTagFixerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\NoClosingTagFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\NoClosingTagFixer>
  20. */
  21. final class NoClosingTagFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideWithFullOpenTagCases
  25. */
  26. public function testWithFullOpenTag(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 provideWithFullOpenTagCases(): iterable
  34. {
  35. yield [
  36. '<?php echo \'Foo\';',
  37. '<?php echo \'Foo\'; ?>',
  38. ];
  39. yield [
  40. '<?php echo \'Foo\';',
  41. '<?php echo \'Foo\';?>',
  42. ];
  43. yield [
  44. '<?php echo \'Foo\'; ?> PLAIN TEXT',
  45. ];
  46. yield [
  47. 'PLAIN TEXT<?php echo \'Foo\'; ?>',
  48. ];
  49. yield [
  50. '<?php
  51. echo \'Foo\';',
  52. '<?php
  53. echo \'Foo\';
  54. ?>',
  55. ];
  56. yield [
  57. '<?php echo \'Foo\'; ?>
  58. <p><?php echo \'this is a template\'; ?></p>
  59. <?php echo \'Foo\'; ?>',
  60. ];
  61. yield [
  62. '<?php echo "foo";',
  63. '<?php echo "foo" ?>',
  64. ];
  65. yield [
  66. '<?php
  67. class foo
  68. {
  69. public function bar()
  70. {
  71. echo "Here I am!";
  72. }
  73. }',
  74. '<?php
  75. class foo
  76. {
  77. public function bar()
  78. {
  79. echo "Here I am!";
  80. }
  81. }?>',
  82. ];
  83. yield [
  84. '<?php
  85. function bar()
  86. {
  87. echo "Here I am!";
  88. }',
  89. '<?php
  90. function bar()
  91. {
  92. echo "Here I am!";
  93. }?>',
  94. ];
  95. yield [
  96. '<?php
  97. if (true) {
  98. echo "Here I am!";
  99. }',
  100. '<?php
  101. if (true) {
  102. echo "Here I am!";
  103. }?>',
  104. ];
  105. yield 'Trailing linebreak, priority issue with SingleBlankLineAtEofFixer.' => [
  106. '<?php echo 1;',
  107. "<?php echo 1;\n?>\n",
  108. ];
  109. yield 'Trailing comment.' => [
  110. '<?php echo 1;// test',
  111. "<?php echo 1;// test\n?>",
  112. ];
  113. yield 'No code' => [
  114. '<?php ',
  115. '<?php ?>',
  116. ];
  117. yield 'No code, only comment' => [
  118. '<?php /* license */',
  119. '<?php /* license */ ?>',
  120. ];
  121. yield [
  122. '<?php ?>aa',
  123. ];
  124. }
  125. /**
  126. * @dataProvider provideWithShortOpenTagCases
  127. */
  128. public function testWithShortOpenTag(string $expected, ?string $input = null): void
  129. {
  130. if (!\ini_get('short_open_tag')) {
  131. self::markTestSkipped('The short_open_tag option is required to be enabled.');
  132. }
  133. $this->doTest($expected, $input);
  134. }
  135. /**
  136. * @return iterable<array{0: string, 1?: string}>
  137. */
  138. public static function provideWithShortOpenTagCases(): iterable
  139. {
  140. yield [
  141. '<? echo \'Foo\';',
  142. '<? echo \'Foo\'; ?>',
  143. ];
  144. yield [
  145. '<? echo \'Foo\';',
  146. '<? echo \'Foo\';?>',
  147. ];
  148. yield [
  149. '<? echo \'Foo\'; ?>
  150. <p><? echo \'this is a template\'; ?></p>
  151. <? echo \'Foo\'; ?>',
  152. ];
  153. yield [
  154. '<? /**/', '<? /**/?>',
  155. ];
  156. yield [
  157. '<?= "somestring"; ?> <?= "anotherstring"; ?>',
  158. ];
  159. yield [
  160. '<?= 1;',
  161. '<?= 1; ?>',
  162. ];
  163. }
  164. }