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. * @dataProvider provideWithShortOpenTagCases
  32. */
  33. public function testWithShortOpenTag(string $expected, ?string $input = null): void
  34. {
  35. if (!\ini_get('short_open_tag')) {
  36. self::markTestSkipped('The short_open_tag option is required to be enabled.');
  37. }
  38. $this->doTest($expected, $input);
  39. }
  40. /**
  41. * @return iterable<int|string, array{0: string, 1?: string}>
  42. */
  43. public static function provideWithFullOpenTagCases(): iterable
  44. {
  45. yield [
  46. '<?php echo \'Foo\';',
  47. '<?php echo \'Foo\'; ?>',
  48. ];
  49. yield [
  50. '<?php echo \'Foo\';',
  51. '<?php echo \'Foo\';?>',
  52. ];
  53. yield [
  54. '<?php echo \'Foo\'; ?> PLAIN TEXT',
  55. ];
  56. yield [
  57. 'PLAIN TEXT<?php echo \'Foo\'; ?>',
  58. ];
  59. yield [
  60. '<?php
  61. echo \'Foo\';',
  62. '<?php
  63. echo \'Foo\';
  64. ?>',
  65. ];
  66. yield [
  67. '<?php echo \'Foo\'; ?>
  68. <p><?php echo \'this is a template\'; ?></p>
  69. <?php echo \'Foo\'; ?>',
  70. ];
  71. yield [
  72. '<?php echo "foo";',
  73. '<?php echo "foo" ?>',
  74. ];
  75. yield [
  76. '<?php
  77. class foo
  78. {
  79. public function bar()
  80. {
  81. echo "Here I am!";
  82. }
  83. }',
  84. '<?php
  85. class foo
  86. {
  87. public function bar()
  88. {
  89. echo "Here I am!";
  90. }
  91. }?>',
  92. ];
  93. yield [
  94. '<?php
  95. function bar()
  96. {
  97. echo "Here I am!";
  98. }',
  99. '<?php
  100. function bar()
  101. {
  102. echo "Here I am!";
  103. }?>',
  104. ];
  105. yield [
  106. '<?php
  107. if (true) {
  108. echo "Here I am!";
  109. }',
  110. '<?php
  111. if (true) {
  112. echo "Here I am!";
  113. }?>',
  114. ];
  115. yield 'Trailing linebreak, priority issue with SingleBlankLineAtEofFixer.' => [
  116. '<?php echo 1;',
  117. "<?php echo 1;\n?>\n",
  118. ];
  119. yield 'Trailing comment.' => [
  120. '<?php echo 1;// test',
  121. "<?php echo 1;// test\n?>",
  122. ];
  123. yield 'No code' => [
  124. '<?php ',
  125. '<?php ?>',
  126. ];
  127. yield 'No code, only comment' => [
  128. '<?php /* license */',
  129. '<?php /* license */ ?>',
  130. ];
  131. yield [
  132. '<?php ?>aa',
  133. ];
  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. }