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