NameQualifiedTransformerTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\Tokenizer\Transformer;
  13. use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
  14. use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
  15. use PhpCsFixer\Tokenizer\CT;
  16. use PhpCsFixer\Tokenizer\Token;
  17. use PhpCsFixer\Tokenizer\Tokens;
  18. /**
  19. * @internal
  20. *
  21. * @requires PHP 8.0
  22. *
  23. * @covers \PhpCsFixer\Tokenizer\Transformer\NameQualifiedTransformer
  24. */
  25. final class NameQualifiedTransformerTest extends AbstractTransformerTestCase
  26. {
  27. use AssertTokensTrait;
  28. /**
  29. * @param list<Token> $expected
  30. * @param null|list<Token> $input
  31. *
  32. * @dataProvider provideProcessCases
  33. *
  34. * @requires PHP 8.0
  35. */
  36. public function testProcess(array $expected, ?array $input = null): void
  37. {
  38. $expectedTokens = Tokens::fromArray($expected);
  39. $tokens = null === $input
  40. ? Tokens::fromArray($expected)
  41. : Tokens::fromArray($input);
  42. $tokenCount = \count($tokens);
  43. for ($i = 0; $i < $tokenCount; ++$i) {
  44. $this->transformer->process($tokens, $tokens[$i], $i);
  45. }
  46. self::assertTokens($expectedTokens, $tokens);
  47. if (null === $input) {
  48. self::assertFalse($tokens->isChanged());
  49. } else {
  50. self::testProcess($expected);
  51. self::assertTrue($tokens->isChanged());
  52. }
  53. }
  54. public static function provideProcessCases(): iterable
  55. {
  56. yield 'string' => [
  57. [
  58. new Token([T_OPEN_TAG, "<?php\n"]),
  59. new Token([T_STRING, 'Foo']),
  60. new Token(';'),
  61. ],
  62. ];
  63. if (\defined('T_NAME_RELATIVE')) { // @TODO: drop condition when PHP 8.0+ is required
  64. yield 'relative 1' => [
  65. [
  66. new Token([T_OPEN_TAG, "<?php\n"]),
  67. new Token([T_NAMESPACE, 'namespace']),
  68. new Token([T_NS_SEPARATOR, '\\']),
  69. new Token([T_STRING, 'Transformer']),
  70. new Token(';'),
  71. ],
  72. [
  73. new Token([T_OPEN_TAG, "<?php\n"]),
  74. new Token([T_NAME_RELATIVE, 'namespace\Transformer']),
  75. new Token(';'),
  76. ],
  77. ];
  78. yield 'relative 2' => [
  79. [
  80. new Token([T_OPEN_TAG, "<?php\n"]),
  81. new Token([T_NAMESPACE, 'namespace']),
  82. new Token([T_NS_SEPARATOR, '\\']),
  83. new Token([T_STRING, 'Transformer']),
  84. new Token([T_NS_SEPARATOR, '\\']),
  85. new Token([T_STRING, 'Foo']),
  86. new Token([T_NS_SEPARATOR, '\\']),
  87. new Token([T_STRING, 'Bar']),
  88. new Token(';'),
  89. ],
  90. [
  91. new Token([T_OPEN_TAG, "<?php\n"]),
  92. new Token([T_NAME_RELATIVE, 'namespace\Transformer\Foo\Bar']),
  93. new Token(';'),
  94. ],
  95. ];
  96. }
  97. if (\defined('T_NAME_FULLY_QUALIFIED')) { // @TODO: drop condition when PHP 8.0+ is required
  98. yield 'name fully qualified 1' => [
  99. [
  100. new Token([T_OPEN_TAG, "<?php\n"]),
  101. new Token([T_NS_SEPARATOR, '\\']),
  102. new Token([T_STRING, 'Foo']),
  103. new Token(';'),
  104. ],
  105. [
  106. new Token([T_OPEN_TAG, "<?php\n"]),
  107. new Token([T_NAME_FULLY_QUALIFIED, '\Foo']),
  108. new Token(';'),
  109. ],
  110. ];
  111. yield 'name qualified 1' => [
  112. [
  113. new Token([T_OPEN_TAG, "<?php\n"]),
  114. new Token([T_STRING, 'Foo']),
  115. new Token([T_NS_SEPARATOR, '\\']),
  116. new Token([T_STRING, 'Bar']),
  117. new Token(';'),
  118. ],
  119. [
  120. new Token([T_OPEN_TAG, "<?php\n"]),
  121. new Token([T_NAME_QUALIFIED, 'Foo\Bar']),
  122. new Token(';'),
  123. ],
  124. ];
  125. yield 'name qualified 2' => [
  126. [
  127. new Token([T_OPEN_TAG, "<?php\n"]),
  128. new Token([T_NS_SEPARATOR, '\\']),
  129. new Token([T_STRING, 'Foo']),
  130. new Token([T_NS_SEPARATOR, '\\']),
  131. new Token([T_STRING, 'Bar']),
  132. new Token(';'),
  133. ],
  134. [
  135. new Token([T_OPEN_TAG, "<?php\n"]),
  136. new Token([T_NAME_QUALIFIED, '\Foo\Bar']),
  137. new Token(';'),
  138. ],
  139. ];
  140. }
  141. }
  142. /**
  143. * @param list<Token> $expected
  144. *
  145. * @dataProvider providePriorityCases
  146. */
  147. public function testPriority(array $expected, string $source): void
  148. {
  149. self::assertTokens(Tokens::fromArray($expected), Tokens::fromCode($source));
  150. }
  151. public static function providePriorityCases(): iterable
  152. {
  153. yield [
  154. [
  155. new Token([T_OPEN_TAG, '<?php ']),
  156. new Token([T_STRING, 'Foo']),
  157. new Token(';'),
  158. new Token([T_STRING, 'Bar']),
  159. new Token(';'),
  160. ],
  161. '<?php Foo;Bar;',
  162. ];
  163. yield [
  164. [
  165. new Token([T_OPEN_TAG, '<?php ']),
  166. new Token([T_STRING, 'Foo']),
  167. new Token([T_NS_SEPARATOR, '\\']),
  168. new Token([T_STRING, 'Bar1']),
  169. new Token(';'),
  170. new Token([T_NS_SEPARATOR, '\\']),
  171. new Token([T_STRING, 'Foo']),
  172. new Token([T_NS_SEPARATOR, '\\']),
  173. new Token([T_STRING, 'Bar2']),
  174. new Token(';'),
  175. new Token([T_STRING, 'Foo']),
  176. new Token([T_NS_SEPARATOR, '\\']),
  177. new Token([T_STRING, 'Bar3']),
  178. new Token(';'),
  179. ],
  180. '<?php Foo\Bar1;\Foo\Bar2;Foo\Bar3;',
  181. ];
  182. yield [
  183. [
  184. new Token([T_OPEN_TAG, '<?php ']),
  185. new Token([T_STRING, 'Foo']),
  186. new Token([T_NS_SEPARATOR, '\\']),
  187. new Token([T_STRING, 'Bar1']),
  188. new Token(';'),
  189. new Token([T_STRING, 'Foo']),
  190. new Token([T_NS_SEPARATOR, '\\']),
  191. new Token([T_STRING, 'Bar2']),
  192. new Token(';'),
  193. new Token([T_STRING, 'Foo']),
  194. new Token([T_NS_SEPARATOR, '\\']),
  195. new Token([T_STRING, 'Bar3']),
  196. new Token(';'),
  197. ],
  198. '<?php Foo\Bar1;Foo\Bar2;Foo\Bar3;',
  199. ];
  200. yield [
  201. [
  202. new Token([T_OPEN_TAG, '<?php ']),
  203. new Token([T_NS_SEPARATOR, '\\']),
  204. new Token([T_STRING, 'Foo']),
  205. new Token([T_NS_SEPARATOR, '\\']),
  206. new Token([T_STRING, 'Bar1']),
  207. new Token(';'),
  208. new Token([T_NS_SEPARATOR, '\\']),
  209. new Token([T_STRING, 'Foo']),
  210. new Token([T_NS_SEPARATOR, '\\']),
  211. new Token([T_STRING, 'Bar2']),
  212. new Token(';'),
  213. new Token([T_NS_SEPARATOR, '\\']),
  214. new Token([T_STRING, 'Foo']),
  215. new Token([T_NS_SEPARATOR, '\\']),
  216. new Token([T_STRING, 'Bar3']),
  217. new Token(';'),
  218. ],
  219. '<?php \Foo\Bar1;\Foo\Bar2;\Foo\Bar3;',
  220. ];
  221. yield [
  222. [
  223. new Token([T_OPEN_TAG, '<?php ']),
  224. new Token([CT::T_NAMESPACE_OPERATOR, 'namespace']),
  225. new Token([T_NS_SEPARATOR, '\\']),
  226. new Token([T_STRING, 'Transformer']),
  227. new Token(';'),
  228. ],
  229. '<?php namespace\Transformer;',
  230. ];
  231. yield [
  232. [
  233. new Token([T_OPEN_TAG, '<?php ']),
  234. new Token([T_NAMESPACE, 'namespace']),
  235. new Token([T_NS_SEPARATOR, '\\']),
  236. new Token([T_STRING, 'Foo']),
  237. new Token(';'),
  238. new Token([T_NAMESPACE, 'namespace']),
  239. new Token([T_NS_SEPARATOR, '\\']),
  240. new Token([T_STRING, 'Bar']),
  241. new Token(';'),
  242. ],
  243. '<?php namespace\Foo;namespace\Bar;',
  244. ];
  245. }
  246. }