SingleBlankLineAtEofFixerTest.php 3.5 KB

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