ImportProcessorTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Processor;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Processor\ImportProcessor;
  15. use PhpCsFixer\Tokenizer\Token;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. use PhpCsFixer\WhitespacesFixerConfig;
  18. /**
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Tokenizer\Processor\ImportProcessor
  22. */
  23. final class ImportProcessorTest extends TestCase
  24. {
  25. /**
  26. * @param class-string $symbol
  27. *
  28. * @dataProvider provideTokenizeNameCases
  29. */
  30. public function testTokenizeName(string $symbol): void
  31. {
  32. self::assertSame(
  33. $symbol,
  34. implode(
  35. '',
  36. array_map(
  37. static fn (Token $token): string => $token->getContent(),
  38. ImportProcessor::tokenizeName($symbol)
  39. )
  40. )
  41. );
  42. }
  43. /**
  44. * @return iterable<array{0: string}>
  45. */
  46. public static function provideTokenizeNameCases(): iterable
  47. {
  48. yield [__CLASS__];
  49. yield ['Foo\Bar'];
  50. yield ['\Foo\Bar'];
  51. yield ['FooBar'];
  52. yield ['\FooBar'];
  53. yield ['\Foo\Bar\Baz\Buzz'];
  54. yield ['\Foo1\Bar_\baz\buzz'];
  55. }
  56. /**
  57. * @param array{
  58. * const?: array<int|string, class-string>,
  59. * class?: array<int|string, class-string>,
  60. * function?: array<int|string, class-string>
  61. * } $imports
  62. *
  63. * @dataProvider provideInsertImportsCases
  64. */
  65. public function testInsertImports(string $expected, string $input, array $imports, int $atIndex): void
  66. {
  67. $processor = new ImportProcessor(new WhitespacesFixerConfig());
  68. $tokens = Tokens::fromCode($input);
  69. $processor->insertImports($tokens, $imports, $atIndex);
  70. self::assertSame($expected, $tokens->generateCode());
  71. }
  72. /**
  73. * @return iterable<string, array{0: string, 1: string, 2: array{class?: list<string>, const?: list<string>, function?: list<string>}, 3: int}>
  74. */
  75. public static function provideInsertImportsCases(): iterable
  76. {
  77. yield 'class import in single namespace' => [
  78. '<?php
  79. namespace Foo;
  80. use Other\A;
  81. use Other\B;
  82. ',
  83. '<?php
  84. namespace Foo;
  85. ',
  86. [
  87. 'class' => ['Other\A', 'Other\B'],
  88. ],
  89. 6,
  90. ];
  91. yield 'class import in single {} namespace' => [
  92. '<?php
  93. namespace Foo {
  94. use Other\A;
  95. use Other\B;
  96. }
  97. ',
  98. '<?php
  99. namespace Foo {
  100. }
  101. ',
  102. [
  103. 'class' => ['Other\A', 'Other\B'],
  104. ],
  105. 7,
  106. ];
  107. }
  108. }