SingleBlankLineAtEofFixerTest.php 3.8 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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\SingleBlankLineAtEofFixer>
  23. */
  24. final class SingleBlankLineAtEofFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<int|string, array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield 'Not adding an empty line in empty file.' => [
  39. '',
  40. ];
  41. yield 'Not adding an empty line in file with only white space.' => [
  42. ' ',
  43. ];
  44. yield [
  45. "<?php\n",
  46. ];
  47. yield [
  48. '<?php
  49. $a = 1;
  50. ',
  51. '<?php
  52. $a = 1;',
  53. ];
  54. yield [
  55. '<?php
  56. $a = 2;
  57. ',
  58. ];
  59. yield [
  60. '<?php
  61. $a = 3;
  62. ',
  63. '<?php
  64. $a = 3;
  65. ',
  66. ];
  67. yield [
  68. "<?php\r\n\$a = 4;\n",
  69. "<?php\r\n\$a = 4;",
  70. ];
  71. yield [
  72. "<?php\r\n\$a = 5;\n",
  73. "<?php\r\n\$a = 5;\r\n \r\n",
  74. ];
  75. yield [
  76. '<?php
  77. $a = 6;
  78. //test
  79. ?>
  80. ',
  81. ];
  82. yield [
  83. // test for not adding an empty line after PHP tag has been closed
  84. '<?php
  85. $a = 7;
  86. //test
  87. ?>',
  88. ];
  89. yield [
  90. // test for not adding an empty line after PHP tag has been closed
  91. '<?php
  92. $a = 8;
  93. //test
  94. ?>
  95. Outside of PHP tags rendering
  96. ',
  97. ];
  98. yield [
  99. // test for not adding an empty line after PHP tag has been closed
  100. "<?php
  101. //test
  102. ?>
  103. inline 1
  104. <?php
  105. ?>Inline2\r\n",
  106. ];
  107. yield [
  108. "<?php return true;\n// A comment\n",
  109. "<?php return true;\n// A comment",
  110. ];
  111. yield [
  112. "<?php return true;\n// A comment\n",
  113. "<?php return true;\n// A comment\n\n",
  114. ];
  115. yield [
  116. "<?php return true;\n# A comment\n",
  117. "<?php return true;\n# A comment",
  118. ];
  119. yield [
  120. "<?php return true;\n# A comment\n",
  121. "<?php return true;\n# A comment\n\n",
  122. ];
  123. yield [
  124. "<?php return true;\n/*\nA comment\n*/\n",
  125. "<?php return true;\n/*\nA comment\n*/",
  126. ];
  127. yield [
  128. "<?php return true;\n/*\nA comment\n*/\n",
  129. "<?php return true;\n/*\nA comment\n*/\n\n",
  130. ];
  131. yield [
  132. "<?= 1;\n",
  133. '<?= 1;',
  134. ];
  135. }
  136. /**
  137. * @dataProvider provideWithWhitespacesConfigCases
  138. */
  139. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  140. {
  141. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  142. $this->doTest($expected, $input);
  143. }
  144. /**
  145. * @return iterable<array{string, string}>
  146. */
  147. public static function provideWithWhitespacesConfigCases(): iterable
  148. {
  149. yield [
  150. "<?php\r\n\$a = 4;\r\n",
  151. "<?php\r\n\$a = 4;",
  152. ];
  153. yield [
  154. "<?php\r\n\$a = 5;\r\n",
  155. "<?php\r\n\$a = 5;\r\n \r\n",
  156. ];
  157. }
  158. }