NamespacesAnalyzerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\Analyzer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
  15. use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. /**
  18. * @author VeeWee <toonverwerft@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer
  23. */
  24. final class NamespacesAnalyzerTest extends TestCase
  25. {
  26. /**
  27. * @param list<NamespaceAnalysis> $expected
  28. *
  29. * @dataProvider provideNamespacesCases
  30. */
  31. public function testNamespaces(string $code, array $expected): void
  32. {
  33. $tokens = Tokens::fromCode($code);
  34. $analyzer = new NamespacesAnalyzer();
  35. self::assertSame(
  36. serialize($expected),
  37. serialize($analyzer->getDeclarations($tokens))
  38. );
  39. }
  40. public static function provideNamespacesCases(): iterable
  41. {
  42. yield ['<?php // no namespaces', [
  43. new NamespaceAnalysis(
  44. '',
  45. '',
  46. 0,
  47. 0,
  48. 0,
  49. 1
  50. ),
  51. ]];
  52. yield ['<?php namespace Foo\Bar;', [
  53. new NamespaceAnalysis(
  54. 'Foo\Bar',
  55. 'Bar',
  56. 1,
  57. 6,
  58. 1,
  59. 6
  60. ),
  61. ]];
  62. yield ['<?php namespace Foo\Bar{}; namespace Foo\Baz {};', [
  63. new NamespaceAnalysis(
  64. 'Foo\Bar',
  65. 'Bar',
  66. 1,
  67. 6,
  68. 1,
  69. 7
  70. ),
  71. new NamespaceAnalysis(
  72. 'Foo\Baz',
  73. 'Baz',
  74. 10,
  75. 16,
  76. 10,
  77. 17
  78. ),
  79. ]];
  80. yield [
  81. <<<'PHP'
  82. #!/usr/bin/php
  83. <?php
  84. return true;
  85. PHP,
  86. [
  87. new NamespaceAnalysis(
  88. '',
  89. '',
  90. 1,
  91. 1,
  92. 1,
  93. 5
  94. ),
  95. ],
  96. ];
  97. yield [
  98. 'there is no namespace if there is no PHP code',
  99. [],
  100. ];
  101. }
  102. /**
  103. * @dataProvider provideGetNamespaceAtCases
  104. */
  105. public function testGetNamespaceAt(string $code, int $index, NamespaceAnalysis $expected): void
  106. {
  107. $tokens = Tokens::fromCode($code);
  108. $analyzer = new NamespacesAnalyzer();
  109. self::assertSame(
  110. serialize($expected),
  111. serialize($analyzer->getNamespaceAt($tokens, $index))
  112. );
  113. }
  114. /**
  115. * @return iterable<array{string, int, NamespaceAnalysis}>
  116. */
  117. public static function provideGetNamespaceAtCases(): iterable
  118. {
  119. yield [
  120. '<?php // no namespaces',
  121. 1,
  122. new NamespaceAnalysis(
  123. '',
  124. '',
  125. 0,
  126. 0,
  127. 0,
  128. 1
  129. ),
  130. ];
  131. yield [
  132. '<?php namespace Foo\Bar;',
  133. 5,
  134. new NamespaceAnalysis(
  135. 'Foo\Bar',
  136. 'Bar',
  137. 1,
  138. 6,
  139. 1,
  140. 6
  141. ),
  142. ];
  143. yield [
  144. '<?php namespace Foo\Bar{}; namespace Foo\Baz {};',
  145. 5,
  146. new NamespaceAnalysis(
  147. 'Foo\Bar',
  148. 'Bar',
  149. 1,
  150. 6,
  151. 1,
  152. 7
  153. ),
  154. ];
  155. yield [
  156. '<?php namespace Foo\Bar{}; namespace Foo\Baz {};',
  157. 13,
  158. new NamespaceAnalysis(
  159. 'Foo\Baz',
  160. 'Baz',
  161. 10,
  162. 16,
  163. 10,
  164. 17
  165. ),
  166. ];
  167. }
  168. public function testInvalidIndex(): void
  169. {
  170. $this->expectException(\InvalidArgumentException::class);
  171. $this->expectExceptionMessage('Token index 666 does not exist.');
  172. $analyzer = new NamespacesAnalyzer();
  173. $analyzer->getNamespaceAt(new Tokens(), 666);
  174. }
  175. }