NamespaceUseAnalysisTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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\Analysis;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis;
  15. /**
  16. * @author VeeWee <toonverwerft@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceUseAnalysis
  21. */
  22. final class NamespaceUseAnalysisTest extends TestCase
  23. {
  24. public function testFullName(): void
  25. {
  26. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, false, 11, 21);
  27. self::assertSame('Full\NamespaceName', $analysis->getFullName());
  28. }
  29. public function testAliased(): void
  30. {
  31. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, false, 12, 22);
  32. self::assertFalse($analysis->isAliased());
  33. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', true, false, 1, 2);
  34. self::assertTrue($analysis->isAliased());
  35. }
  36. public function testIsInMulti(): void
  37. {
  38. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, false, 12, 22);
  39. self::assertFalse($analysis->isInMulti());
  40. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, true, 1, 20, 5, 10);
  41. self::assertTrue($analysis->isInMulti());
  42. }
  43. public function testChunkStartIndexAndChunkEndIndexAreRequiredWhenIsInMulti(): void
  44. {
  45. $this->expectException(\LogicException::class);
  46. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, true, 1, 20);
  47. }
  48. public function testShortName(): void
  49. {
  50. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, false, 1, 2);
  51. self::assertSame('NamespaceName', $analysis->getShortName());
  52. }
  53. public function testStartIndex(): void
  54. {
  55. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, false, 1, 2);
  56. self::assertSame(1, $analysis->getStartIndex());
  57. }
  58. public function testEndIndex(): void
  59. {
  60. $analysis = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Full\NamespaceName', 'NamespaceName', false, false, 1, 72);
  61. self::assertSame(72, $analysis->getEndIndex());
  62. }
  63. public function testTypeCheck(): void
  64. {
  65. $class = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CLASS, 'Foo\Bar', 'Baz', false, false, 1, 2);
  66. $function = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_FUNCTION, 'Foo\Bar', 'Baz', false, false, 1, 2);
  67. $constant = new NamespaceUseAnalysis(NamespaceUseAnalysis::TYPE_CONSTANT, 'Foo\Bar', 'Baz', false, false, 1, 2);
  68. self::assertTrue($class->isClass());
  69. self::assertFalse($class->isFunction());
  70. self::assertFalse($class->isConstant());
  71. self::assertSame(NamespaceUseAnalysis::TYPE_CLASS, $class->getType());
  72. self::assertFalse($function->isClass());
  73. self::assertTrue($function->isFunction());
  74. self::assertFalse($function->isConstant());
  75. self::assertSame(NamespaceUseAnalysis::TYPE_FUNCTION, $function->getType());
  76. self::assertFalse($constant->isClass());
  77. self::assertFalse($constant->isFunction());
  78. self::assertTrue($constant->isConstant());
  79. self::assertSame(NamespaceUseAnalysis::TYPE_CONSTANT, $constant->getType());
  80. }
  81. }