NoLeadingNamespaceWhitespaceFixerTest.php 3.9 KB

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