NoBlankLinesAfterClassOpeningFixerTest.php 3.9 KB

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