SingleBlankLineAtEofFixerTest.php 3.8 KB

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