ImportTransformerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Tokenizer\CT;
  15. /**
  16. * @author Gregor Harlan <gharlan@web.de>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Tokenizer\Transformer\ImportTransformer
  21. *
  22. * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
  23. */
  24. final class ImportTransformerTest extends AbstractTransformerTestCase
  25. {
  26. /**
  27. * @param _TransformerTestExpectedTokens $expectedTokens
  28. *
  29. * @dataProvider provideProcessCases
  30. */
  31. public function testProcess(string $source, array $expectedTokens = []): void
  32. {
  33. $this->doTest(
  34. $source,
  35. $expectedTokens,
  36. [
  37. T_CONST,
  38. CT::T_CONST_IMPORT,
  39. T_FUNCTION,
  40. CT::T_FUNCTION_IMPORT,
  41. ]
  42. );
  43. }
  44. public static function provideProcessCases(): iterable
  45. {
  46. yield [
  47. '<?php const FOO = 1;',
  48. [
  49. 1 => T_CONST,
  50. ],
  51. ];
  52. yield [
  53. '<?php use Foo; const FOO = 1;',
  54. [
  55. 6 => T_CONST,
  56. ],
  57. ];
  58. yield [
  59. '<?php class Foo { const BAR = 1; }',
  60. [
  61. 7 => T_CONST,
  62. ],
  63. ];
  64. yield [
  65. '<?php use const Foo\BAR;',
  66. [
  67. 3 => CT::T_CONST_IMPORT,
  68. ],
  69. ];
  70. yield [
  71. '<?php function foo() {}',
  72. [
  73. 1 => T_FUNCTION,
  74. ],
  75. ];
  76. yield [
  77. '<?php $a = function () {};',
  78. [
  79. 5 => T_FUNCTION,
  80. ],
  81. ];
  82. yield [
  83. '<?php class Foo { function foo() {} }',
  84. [
  85. 7 => T_FUNCTION,
  86. ],
  87. ];
  88. yield [
  89. '<?php function & foo() {}',
  90. [
  91. 1 => T_FUNCTION,
  92. ],
  93. ];
  94. yield [
  95. '<?php use function Foo\bar;',
  96. [
  97. 3 => CT::T_FUNCTION_IMPORT,
  98. ],
  99. ];
  100. yield [
  101. '<?php use Foo\ { function Bar };',
  102. [
  103. 8 => CT::T_FUNCTION_IMPORT,
  104. ],
  105. ];
  106. yield [
  107. '<?php use Foo\ {
  108. function F1,
  109. const Constants\C1,
  110. function Functions\F2,
  111. const C2,
  112. function F3,
  113. const C3,
  114. };',
  115. [
  116. 8 => CT::T_FUNCTION_IMPORT,
  117. 13 => CT::T_CONST_IMPORT,
  118. 20 => CT::T_FUNCTION_IMPORT,
  119. 27 => CT::T_CONST_IMPORT,
  120. 32 => CT::T_FUNCTION_IMPORT,
  121. 37 => CT::T_CONST_IMPORT,
  122. ],
  123. ];
  124. }
  125. }