NoBlankLinesAfterClassOpeningFixerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\ClassNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Ceeram <ceeram@cakephp.org>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\ClassNotation\NoBlankLinesAfterClassOpeningFixer
  21. */
  22. final class NoBlankLinesAfterClassOpeningFixerTest 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. /**
  32. * @dataProvider provideFixTraitsCases
  33. */
  34. public function testFixTraits(string $expected, ?string $input = null): void
  35. {
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixCases(): iterable
  39. {
  40. yield [
  41. '<?php
  42. class Good
  43. {
  44. public function firstMethod()
  45. {
  46. //code here
  47. }
  48. }',
  49. '<?php
  50. class Good
  51. {
  52. public function firstMethod()
  53. {
  54. //code here
  55. }
  56. }',
  57. ];
  58. yield [
  59. '<?php
  60. class Good
  61. {
  62. /**
  63. * Also no blank line before DocBlock
  64. */
  65. public function firstMethod()
  66. {
  67. //code here
  68. }
  69. }',
  70. '<?php
  71. class Good
  72. {
  73. /**
  74. * Also no blank line before DocBlock
  75. */
  76. public function firstMethod()
  77. {
  78. //code here
  79. }
  80. }',
  81. ];
  82. yield [
  83. '<?php
  84. interface Good
  85. {
  86. /**
  87. * Also no blank line before DocBlock
  88. */
  89. public function firstMethod();
  90. }',
  91. '<?php
  92. interface Good
  93. {
  94. /**
  95. * Also no blank line before DocBlock
  96. */
  97. public function firstMethod();
  98. }',
  99. ];
  100. // check if some fancy whitespaces aren't modified
  101. yield [
  102. '<?php
  103. class Good
  104. {public
  105. function firstMethod()
  106. {
  107. //code here
  108. }
  109. }',
  110. ];
  111. // check if line with spaces is removed when next token is indented
  112. yield [
  113. '<?php
  114. class Foo
  115. {
  116. function bar() {}
  117. }
  118. ',
  119. '<?php
  120. class Foo
  121. {
  122. '.'
  123. function bar() {}
  124. }
  125. ',
  126. ];
  127. // check if line with spaces is removed when next token is not indented
  128. yield [
  129. '<?php
  130. class Foo
  131. {
  132. function bar() {}
  133. }
  134. ',
  135. '<?php
  136. class Foo
  137. {
  138. '.'
  139. function bar() {}
  140. }
  141. ',
  142. ];
  143. }
  144. public static function provideFixTraitsCases(): iterable
  145. {
  146. yield [
  147. '<?php
  148. trait Good
  149. {
  150. /**
  151. * Also no blank line before DocBlock
  152. */
  153. public function firstMethod() {}
  154. }',
  155. '<?php
  156. trait Good
  157. {
  158. /**
  159. * Also no blank line before DocBlock
  160. */
  161. public function firstMethod() {}
  162. }',
  163. ];
  164. }
  165. /**
  166. * @dataProvider provideMessyWhitespacesCases
  167. */
  168. public function testMessyWhitespaces(string $expected, ?string $input = null): void
  169. {
  170. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  171. $this->doTest($expected, $input);
  172. }
  173. public static function provideMessyWhitespacesCases(): iterable
  174. {
  175. yield [
  176. "<?php\nclass Foo\n{\r\n public function bar() {}\n}",
  177. "<?php\nclass Foo\n{\n\n public function bar() {}\n}",
  178. ];
  179. yield [
  180. "<?php\nclass Foo\n{\r\n public function bar() {}\n}",
  181. "<?php\nclass Foo\n{\r\n\r\n public function bar() {}\n}",
  182. ];
  183. }
  184. /**
  185. * @dataProvider provideFix81Cases
  186. *
  187. * @requires PHP 8.1
  188. */
  189. public function testFix81(string $expected, string $input): void
  190. {
  191. $this->doTest($expected, $input);
  192. }
  193. public static function provideFix81Cases(): iterable
  194. {
  195. yield [
  196. '<?php
  197. enum Good
  198. {
  199. public function firstMethod()
  200. {}
  201. }',
  202. '<?php
  203. enum Good
  204. {
  205. public function firstMethod()
  206. {}
  207. }',
  208. ];
  209. }
  210. }