NoLeadingNamespaceWhitespaceFixerTest.php 4.1 KB

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