DocBlockTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\DocBlock;
  12. use PhpCsFixer\DocBlock\DocBlock;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * @author Graham Campbell <graham@alt-three.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\DocBlock\DocBlock
  20. */
  21. final class DocBlockTest extends TestCase
  22. {
  23. /**
  24. * This represents the content an entire docblock.
  25. *
  26. * @var string
  27. */
  28. private static $sample = '/**
  29. * Test docblock.
  30. *
  31. * @param string $hello
  32. * @param bool $test Description
  33. * extends over many lines
  34. *
  35. * @param adkjbadjasbdand $asdnjkasd
  36. *
  37. * @throws \Exception asdnjkasd
  38. * asdasdasdasdasdasdasdasd
  39. * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
  40. *
  41. * @return void
  42. */';
  43. public function testContent()
  44. {
  45. $doc = new DocBlock(self::$sample);
  46. $this->assertSame(self::$sample, $doc->getContent());
  47. $this->assertSame(self::$sample, (string) $doc);
  48. }
  49. public function testEmptyContent()
  50. {
  51. $doc = new DocBlock('');
  52. $this->assertSame('', $doc->getContent());
  53. }
  54. public function testGetLines()
  55. {
  56. $doc = new DocBlock(self::$sample);
  57. $this->assertInternalType('array', $doc->getLines());
  58. $this->assertCount(15, $doc->getLines());
  59. foreach ($doc->getLines() as $index => $line) {
  60. $this->assertInstanceOf('PhpCsFixer\DocBlock\Line', $line);
  61. $this->assertSame($doc->getLine($index), $line);
  62. }
  63. $this->assertEmpty($doc->getLine(15));
  64. }
  65. public function testGetAnnotations()
  66. {
  67. $doc = new DocBlock(self::$sample);
  68. $this->assertInternalType('array', $doc->getAnnotations());
  69. $this->assertCount(5, $doc->getAnnotations());
  70. foreach ($doc->getAnnotations() as $index => $annotations) {
  71. $this->assertInstanceOf('PhpCsFixer\DocBlock\Annotation', $annotations);
  72. $this->assertSame($doc->getAnnotation($index), $annotations);
  73. }
  74. $this->assertEmpty($doc->getAnnotation(5));
  75. }
  76. public function testGetAnnotationsOfTypeParam()
  77. {
  78. $doc = new DocBlock(self::$sample);
  79. $annotations = $doc->getAnnotationsOfType('param');
  80. $this->assertInternalType('array', $annotations);
  81. $this->assertCount(3, $annotations);
  82. $first = ' * @param string $hello
  83. ';
  84. $second = ' * @param bool $test Description
  85. * extends over many lines
  86. ';
  87. $third = ' * @param adkjbadjasbdand $asdnjkasd
  88. ';
  89. $this->assertSame($first, $annotations[0]->getContent());
  90. $this->assertSame($second, $annotations[1]->getContent());
  91. $this->assertSame($third, $annotations[2]->getContent());
  92. }
  93. public function testGetAnnotationsOfTypeThrows()
  94. {
  95. $doc = new DocBlock(self::$sample);
  96. $annotations = $doc->getAnnotationsOfType('throws');
  97. $this->assertInternalType('array', $annotations);
  98. $this->assertCount(1, $annotations);
  99. $content = ' * @throws \Exception asdnjkasd
  100. * asdasdasdasdasdasdasdasd
  101. * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
  102. ';
  103. $this->assertSame($content, $annotations[0]->getContent());
  104. }
  105. public function testGetAnnotationsOfTypeReturn()
  106. {
  107. $doc = new DocBlock(self::$sample);
  108. $annotations = $doc->getAnnotationsOfType('return');
  109. $this->assertInternalType('array', $annotations);
  110. $this->assertCount(1, $annotations);
  111. $content = ' * @return void
  112. ';
  113. $this->assertSame($content, $annotations[0]->getContent());
  114. }
  115. public function testGetAnnotationsOfTypeFoo()
  116. {
  117. $doc = new DocBlock(self::$sample);
  118. $annotations = $doc->getAnnotationsOfType('foo');
  119. $this->assertInternalType('array', $annotations);
  120. $this->assertCount(0, $annotations);
  121. }
  122. }