SingleBlankLineAtEofFixerTest.php 3.7 KB

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