BlankLineAfterOpeningTagFixerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Ceeram <ceeram@cakephp.org>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer>
  24. */
  25. final class BlankLineAfterOpeningTagFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null): void
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return iterable<int|string, array{0: string, 1?: string}>
  36. */
  37. public static function provideFixCases(): iterable
  38. {
  39. yield [
  40. '<?php
  41. echo 1;',
  42. '<?php
  43. echo 1;',
  44. ];
  45. yield [
  46. '<?php
  47. $b = 2;
  48. echo 3;',
  49. '<?php $b = 2;
  50. echo 3;',
  51. ];
  52. yield [
  53. '<?php
  54. '.'
  55. $c = 4;
  56. echo 5;',
  57. ];
  58. yield [
  59. '<?php
  60. $a = function(){
  61. echo 1;
  62. };',
  63. '<?php $a = function(){
  64. echo 1;
  65. };',
  66. ];
  67. yield [
  68. '<?php
  69. class SomeClass
  70. {
  71. const VERSION = "1.1.1";
  72. const FOO = "bar";
  73. }
  74. ',
  75. ];
  76. yield [
  77. '<?php $foo = true; ?>',
  78. ];
  79. yield [
  80. '<?php $foo = true; ?>
  81. ',
  82. ];
  83. yield [
  84. '<?php
  85. $foo = true;
  86. ?>',
  87. '<?php
  88. $foo = true;
  89. ?>',
  90. ];
  91. yield [
  92. '<?php
  93. $foo = true;
  94. $bar = false;
  95. ',
  96. '<?php $foo = true;
  97. $bar = false;
  98. ',
  99. ];
  100. yield [
  101. '<?php
  102. $foo = true;
  103. ?>
  104. Html here
  105. <?php $bar = false;',
  106. ];
  107. yield [
  108. '<?php
  109. $foo = true;
  110. ?>
  111. Html here
  112. <?php $bar = false;
  113. ',
  114. ];
  115. yield [
  116. '<?= $bar;
  117. $foo = $bar;
  118. ?>',
  119. ];
  120. yield 'empty file with open tag without new line' => [
  121. '<?php',
  122. ];
  123. yield 'empty file with open tag with new line' => [
  124. "<?php\n",
  125. ];
  126. yield 'file with shebang' => [
  127. <<<'EOD'
  128. #!x
  129. <?php
  130. echo 1;
  131. EOD,
  132. <<<'EOD'
  133. #!x
  134. <?php
  135. echo 1;
  136. EOD,
  137. ];
  138. yield 'file starting with multi-line comment' => [
  139. <<<'PHP'
  140. <?php
  141. /**
  142. * @author yes
  143. */
  144. PHP,
  145. <<<'PHP'
  146. <?php
  147. /**
  148. * @author yes
  149. */
  150. PHP,
  151. ];
  152. }
  153. /**
  154. * @dataProvider provideWithWhitespacesConfigCases
  155. */
  156. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  157. {
  158. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  159. $this->doTest($expected, $input);
  160. }
  161. /**
  162. * @return iterable<array{string, string}>
  163. */
  164. public static function provideWithWhitespacesConfigCases(): iterable
  165. {
  166. yield [
  167. "<?php\r\n\r\n\$foo = true;\r\n",
  168. "<?php \$foo = true;\r\n",
  169. ];
  170. yield [
  171. "<?php\r\n\r\n\$foo = true;\r\n",
  172. "<?php\r\n\$foo = true;\r\n",
  173. ];
  174. }
  175. }