NoLeadingNamespaceWhitespaceFixerTest.php 4.1 KB

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