CleanNamespaceFixerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\NamespaceNotation\CleanNamespaceFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\NamespaceNotation\CleanNamespaceFixer>
  20. */
  21. final class CleanNamespaceFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @requires PHP <8.0
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, string $input): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{string, string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php use function FooLibrary\Bar\Baz\ClassA as Foo ?>',
  39. '<?php use function FooLibrary \ Bar \ Baz \ /* A */ ClassA as Foo ?>',
  40. ];
  41. yield [
  42. '<?php use function FooLibrary\Bar\Baz\ClassA as Foo;',
  43. '<?php use function FooLibrary \ Bar \ Baz \ /* A */ ClassA as Foo;',
  44. ];
  45. yield [
  46. '<?php namespace AT\B # foo 3
  47. ;',
  48. '<?php namespace AT # foo 1
  49. \ /**
  50. 2
  51. */
  52. B # foo 3
  53. ;',
  54. ];
  55. yield [
  56. '<?php namespace AX\B;',
  57. '<?php namespace AX /* foo */ \ B;',
  58. ];
  59. yield [
  60. '<?php namespace A1\B\C ; // foo',
  61. '<?php namespace A1 \ B \ C ; // foo',
  62. ];
  63. yield [
  64. '<?php echo A\B();A\B();A\B();A\B();',
  65. '<?php echo A \ B();A \ B();A \ B();A \ B();',
  66. ];
  67. yield [
  68. '<?php namespace A\B ?>',
  69. '<?php namespace A \ B ?>',
  70. ];
  71. yield [
  72. '<?php echo /* */ x\y() ?>',
  73. '<?php echo /* */ x \ y() ?>',
  74. ];
  75. yield [
  76. '<?php namespace A\B\C\D;?>',
  77. '<?php namespace A\/* 1 */B/* 2 */\C/* 3 *//* 4 */\/* 5 */D;?>',
  78. ];
  79. yield [
  80. '<?php namespace A\B ?>',
  81. '<?php namespace A \/* 1 */ B ?>',
  82. ];
  83. yield [
  84. '<?php echo A\B(1, 2, 3) ?>',
  85. '<?php echo A \ /* 1 */ B(1, 2, 3) ?>',
  86. ];
  87. yield [
  88. '<?php
  89. namespace A\B\C\D\E /* 5.1 */{ }
  90. namespace B\B\C\D\E /* 5.2 */{ }
  91. namespace C\B\C\D\E /* 5.3 */{ }
  92. namespace D\B\C\D\E /* 5.4 */{ }
  93. namespace E\B\C\D\E /* 5.5 */{ }
  94. ',
  95. '<?php
  96. namespace A /* 1 */ \ B \ /** 2 */ C \ /* 3 */ D /* 4 */ \ E /* 5.1 */{ }
  97. namespace B /* 1 */ \ B \ /* 2 */ C \ /** 3 */ D /* 4 */ \ E /* 5.2 */{ }
  98. namespace C /* 1 */ \ B \ /* 2 */ C \ /* 3 */ D /** 4 */ \ E /* 5.3 */{ }
  99. namespace D /* 1 */ \ B \ /* 2 */ C \ /* 3 */ D /* 4 */ \ E /* 5.4 */{ }
  100. namespace E /* 1 */ \ B \ /* 2 */ C \ /* 3 */ D /* 4 */ \ E /* 5.5 */{ }
  101. ',
  102. ];
  103. yield [
  104. '<?php
  105. namespace A\B;
  106. try {
  107. foo();
  108. } catch ( \ParseError\A\B $e) {
  109. }
  110. if (
  111. !a instanceof \EventDispatcher\EventDispatcherInterface
  112. ) {
  113. foo();
  114. }
  115. ',
  116. '<?php
  117. namespace A \ B;
  118. try {
  119. foo();
  120. } catch ( \ ParseError\ A \ B $e) {
  121. }
  122. if (
  123. !a instanceof \EventDispatcher\/* 1 */EventDispatcherInterface
  124. ) {
  125. foo();
  126. }
  127. ',
  128. ];
  129. yield [
  130. '<?php use function Foo\iter\ { range, map, filter, apply, reduce, foo\operator };
  131. class Foo
  132. {
  133. private function foo1(): \Exception\A /** 2 */ // trailing comment
  134. {
  135. }
  136. private function foo2(): \Exception // trailing comment
  137. {
  138. }
  139. }',
  140. '<?php use function Foo \ iter /* A */ \ { range, map, filter, apply, reduce, foo \ operator };
  141. class Foo
  142. {
  143. private function foo1(): \Exception \ /* 1 */ A /** 2 */ // trailing comment
  144. {
  145. }
  146. private function foo2(): \Exception // trailing comment
  147. {
  148. }
  149. }',
  150. ];
  151. }
  152. }