NoLeadingNamespaceWhitespaceFixerTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 Bram Gotink <bram@gotink.me>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\NamespaceNotation\NoLeadingNamespaceWhitespaceFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\NamespaceNotation\NoLeadingNamespaceWhitespaceFixer>
  24. */
  25. final class NoLeadingNamespaceWhitespaceFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. */
  30. public function testFix(string $expected, ?string $input = null): void
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return iterable<array{0: string, 1?: string}>
  36. */
  37. public static function provideFixCases(): iterable
  38. {
  39. $manySpaces = [];
  40. for ($i = 1; $i <= 100; ++$i) {
  41. $manySpaces[] = 'namespace Test'.$i.';';
  42. }
  43. // with newline
  44. yield ["<?php\nnamespace Test1;"];
  45. yield ["<?php\n\nnamespace Test2;"];
  46. yield [
  47. "<?php\nnamespace Test3;",
  48. "<?php\n namespace Test3;",
  49. ];
  50. // without newline
  51. yield ['<?php namespace Test4;'];
  52. yield [
  53. '<?php namespace Test5;',
  54. '<?php namespace Test5;',
  55. ];
  56. // multiple namespaces with newline
  57. yield [
  58. '<?php
  59. namespace Test6a;
  60. namespace Test6b;',
  61. ];
  62. yield [
  63. '<?php
  64. namespace Test7a;
  65. /* abc */
  66. namespace Test7b;',
  67. '<?php
  68. namespace Test7a;
  69. /* abc */namespace Test7b;',
  70. ];
  71. yield [
  72. '<?php
  73. namespace Test8a;
  74. namespace Test8b;',
  75. '<?php
  76. namespace Test8a;
  77. namespace Test8b;',
  78. ];
  79. yield [
  80. '<?php
  81. namespace Test9a;
  82. class Test {}
  83. namespace Test9b;',
  84. '<?php
  85. namespace Test9a;
  86. class Test {}
  87. namespace Test9b;',
  88. ];
  89. yield [
  90. '<?php
  91. namespace Test10a;
  92. use Exception;
  93. namespace Test10b;',
  94. '<?php
  95. namespace Test10a;
  96. use Exception;
  97. namespace Test10b;',
  98. ];
  99. // multiple namespaces without newline
  100. yield ['<?php namespace Test11a; namespace Test11b;'];
  101. yield [
  102. '<?php namespace Test12a; namespace Test12b;',
  103. '<?php namespace Test12a; namespace Test12b;', ];
  104. yield [
  105. '<?php namespace Test13a; namespace Test13b;',
  106. '<?php namespace Test13a; namespace Test13b;', ];
  107. // namespaces without spaces in between
  108. yield [
  109. '<?php
  110. namespace Test14a{}
  111. namespace Test14b{}',
  112. '<?php
  113. namespace Test14a{}namespace Test14b{}',
  114. ];
  115. yield [
  116. '<?php
  117. namespace Test15a;
  118. namespace Test15b;',
  119. '<?php
  120. namespace Test15a;namespace Test15b;',
  121. ];
  122. yield [
  123. '<?php
  124. '.implode("\n", $manySpaces),
  125. '<?php
  126. '.implode('', $manySpaces),
  127. ];
  128. yield [
  129. '<?php
  130. #
  131. namespace TestComment;',
  132. '<?php
  133. #
  134. namespace TestComment;',
  135. ];
  136. }
  137. /**
  138. * @dataProvider provideWithWhitespacesConfigCases
  139. */
  140. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  141. {
  142. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  143. $this->doTest($expected, $input);
  144. }
  145. /**
  146. * @return iterable<array{string, string}>
  147. */
  148. public static function provideWithWhitespacesConfigCases(): iterable
  149. {
  150. yield [
  151. "<?php\r\nnamespace TestW1a{}\r\nnamespace TestW1b{}",
  152. "<?php\r\n namespace TestW1a{}\r\nnamespace TestW1b{}",
  153. ];
  154. yield [
  155. "<?php\r\nnamespace Test14a{}\r\nnamespace Test14b{}",
  156. "<?php\r\n namespace Test14a{}namespace Test14b{}",
  157. ];
  158. }
  159. }