BlankLineAfterNamespaceFixerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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\NamespaceNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\NamespaceNotation\BlankLineAfterNamespaceFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\NamespaceNotation\BlankLineAfterNamespaceFixer>
  23. */
  24. final class BlankLineAfterNamespaceFixerTest 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 namespace A\B?>
  40. <?php
  41. for($i=0; $i<10; ++$i) {echo $i;}',
  42. ];
  43. yield [
  44. '<?php namespace A\B?>',
  45. ];
  46. yield [
  47. '<?php
  48. namespace A\B;
  49. class C {}
  50. ',
  51. '<?php
  52. namespace A\B;
  53. class C {}
  54. ',
  55. ];
  56. yield [
  57. '<?php
  58. namespace A\B;
  59. class C {}
  60. ',
  61. '<?php
  62. namespace A\B;
  63. class C {}
  64. ',
  65. ];
  66. yield [
  67. '<?php
  68. namespace A\B;
  69. class C {}
  70. ',
  71. '<?php
  72. namespace A\B; class C {}
  73. ',
  74. ];
  75. yield [
  76. '<?php
  77. namespace A\B;
  78. class C {}
  79. ',
  80. '<?php
  81. namespace A\B;class C {}
  82. ',
  83. ];
  84. yield [
  85. '<?php
  86. namespace A\B {
  87. class C {
  88. public $foo;
  89. private $bar;
  90. }
  91. }
  92. ',
  93. ];
  94. yield [
  95. "<?php\rnamespace A\\B;
  96. class C {}\r",
  97. "<?php\rnamespace A\\B;\r\r\r\r\r\rclass C {}\r",
  98. ];
  99. yield [
  100. '<?php
  101. namespace A\B;
  102. namespace\C\func();
  103. foo();
  104. ',
  105. ];
  106. yield [
  107. '<?php
  108. namespace Foo;
  109. ',
  110. '<?php
  111. namespace Foo;
  112. ',
  113. ];
  114. yield [
  115. '<?php
  116. namespace Foo;
  117. ',
  118. '<?php
  119. namespace Foo;',
  120. ];
  121. yield [
  122. '<?php
  123. namespace Foo;
  124. ?>',
  125. '<?php
  126. namespace Foo;
  127. ?>',
  128. ];
  129. yield [
  130. '<?php
  131. namespace Foo;
  132. class Bar {}',
  133. ];
  134. yield [
  135. '<?php
  136. namespace Foo;
  137. class Bar {}',
  138. '<?php
  139. namespace Foo;
  140. class Bar {}',
  141. ];
  142. yield [
  143. '<?php
  144. namespace My\NS;
  145. class X extends Y {}',
  146. ];
  147. yield [
  148. '<?php
  149. namespace My\NS; // comment
  150. class X extends Y {}',
  151. ];
  152. yield [
  153. '<?php
  154. namespace My\NS; /* comment */
  155. class X extends Y {}',
  156. ];
  157. yield [
  158. '<?php
  159. namespace My\NS; /*
  160. comment 1
  161. comment 2
  162. */
  163. class X extends Y {}',
  164. '<?php
  165. namespace My\NS; /*
  166. comment 1
  167. comment 2
  168. */
  169. class X extends Y {}',
  170. ];
  171. yield [
  172. '<?php
  173. namespace My\NS; /** comment */
  174. class X extends Y {}',
  175. ];
  176. yield [
  177. '<?php
  178. namespace My\NS; /**
  179. comment 1
  180. comment 2
  181. */
  182. class X extends Y {}',
  183. '<?php
  184. namespace My\NS; /**
  185. comment 1
  186. comment 2
  187. */
  188. class X extends Y {}',
  189. ];
  190. }
  191. /**
  192. * @dataProvider provideWithWhitespacesConfigCases
  193. */
  194. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  195. {
  196. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  197. $this->doTest($expected, $input);
  198. }
  199. /**
  200. * @return iterable<array{string, string}>
  201. */
  202. public static function provideWithWhitespacesConfigCases(): iterable
  203. {
  204. yield [
  205. "<?php namespace A\\B;\r\n\r\nclass C {}",
  206. '<?php namespace A\B; class C {}',
  207. ];
  208. yield [
  209. "<?php namespace A\\B;\r\n\r\nclass C {}",
  210. "<?php namespace A\\B;\r\n\r\n\r\n\r\n\r\n\r\nclass C {}",
  211. ];
  212. }
  213. }