NoBlankLinesAfterClassOpeningFixerTest.php 3.8 KB

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