DocBlockTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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\DocBlock;
  13. use PhpCsFixer\DocBlock\DocBlock;
  14. use PhpCsFixer\Tests\TestCase;
  15. /**
  16. * @author Graham Campbell <hello@gjcampbell.co.uk>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\DocBlock\DocBlock
  21. */
  22. final class DocBlockTest extends TestCase
  23. {
  24. /**
  25. * This represents the content an entire docblock.
  26. *
  27. * @var string
  28. */
  29. private static $sample = '/**
  30. * Test docblock.
  31. *
  32. * @param string $hello
  33. * @param bool $test Description
  34. * extends over many lines
  35. *
  36. * @param adkjbadjasbdand $asdnjkasd
  37. *
  38. * @throws \Exception asdnjkasd
  39. * asdasdasdasdasdasdasdasd
  40. * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
  41. *
  42. * @return void
  43. */';
  44. public function testContent(): void
  45. {
  46. $doc = new DocBlock(self::$sample);
  47. self::assertSame(self::$sample, $doc->getContent());
  48. self::assertSame(self::$sample, (string) $doc);
  49. }
  50. public function testEmptyContent(): void
  51. {
  52. $doc = new DocBlock('');
  53. self::assertSame('', $doc->getContent());
  54. }
  55. public function testGetLines(): void
  56. {
  57. $doc = new DocBlock(self::$sample);
  58. $lines = $doc->getLines();
  59. self::assertCount(15, $lines);
  60. foreach ($lines as $index => $line) {
  61. self::assertInstanceOf(\PhpCsFixer\DocBlock\Line::class, $line);
  62. self::assertSame($doc->getLine($index), $line);
  63. }
  64. self::assertEmpty($doc->getLine(15));
  65. }
  66. public function testGetAnnotations(): void
  67. {
  68. $doc = new DocBlock(self::$sample);
  69. $annotations = $doc->getAnnotations();
  70. self::assertCount(5, $annotations);
  71. foreach ($annotations as $index => $annotation) {
  72. self::assertInstanceOf(\PhpCsFixer\DocBlock\Annotation::class, $annotation);
  73. self::assertSame($doc->getAnnotation($index), $annotation);
  74. }
  75. self::assertEmpty($doc->getAnnotation(5));
  76. }
  77. public function testGetAnnotationsOfTypeParam(): void
  78. {
  79. $doc = new DocBlock(self::$sample);
  80. $annotations = $doc->getAnnotationsOfType('param');
  81. self::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. self::assertSame($first, $annotations[0]->getContent());
  90. self::assertSame($second, $annotations[1]->getContent());
  91. self::assertSame($third, $annotations[2]->getContent());
  92. }
  93. public function testGetAnnotationsOfTypeThrows(): void
  94. {
  95. $doc = new DocBlock(self::$sample);
  96. $annotations = $doc->getAnnotationsOfType('throws');
  97. self::assertCount(1, $annotations);
  98. $content = ' * @throws \Exception asdnjkasd
  99. * asdasdasdasdasdasdasdasd
  100. * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
  101. ';
  102. self::assertSame($content, $annotations[0]->getContent());
  103. }
  104. public function testGetAnnotationsOfTypeReturn(): void
  105. {
  106. $doc = new DocBlock(self::$sample);
  107. $annotations = $doc->getAnnotationsOfType('return');
  108. self::assertCount(1, $annotations);
  109. $content = ' * @return void
  110. ';
  111. self::assertSame($content, $annotations[0]->getContent());
  112. }
  113. public function testGetAnnotationsOfTypeFoo(): void
  114. {
  115. $doc = new DocBlock(self::$sample);
  116. $annotations = $doc->getAnnotationsOfType('foo');
  117. self::assertCount(0, $annotations);
  118. }
  119. public function testIsMultiLIne(): void
  120. {
  121. $doc = new DocBlock(self::$sample);
  122. self::assertTrue($doc->isMultiLine());
  123. }
  124. /**
  125. * @dataProvider provideMakeMultiLIneCases
  126. */
  127. public function testMakeMultiLIne(string $inputDocBlock, string $outputDocBlock = null, string $indent = '', string $newLine = "\n"): void
  128. {
  129. $doc = new DocBlock($inputDocBlock);
  130. $doc->makeMultiLine($indent, $newLine);
  131. if (null === $outputDocBlock) {
  132. $outputDocBlock = $inputDocBlock;
  133. }
  134. self::assertSame($outputDocBlock, $doc->getContent());
  135. }
  136. public static function provideMakeMultiLIneCases(): iterable
  137. {
  138. yield 'It keeps a multi line doc block as is' => [
  139. "/**\n * Hello\n */",
  140. ];
  141. yield 'It keeps a multi line doc block as is with multiple annotations' => [
  142. "/**\n * @foo\n *@bar\n */",
  143. ];
  144. yield 'It keeps a multi line doc block with indentation' => [
  145. "/**\n\t *@foo\n\t */",
  146. ];
  147. yield 'It Converts a single line to multi line with no indentation' => [
  148. '/** Hello */',
  149. "/**\n * Hello\n */",
  150. ];
  151. yield 'It Converts a single line to multi line with correct indentation' => [
  152. '/** Hello */',
  153. "/**\n * Hello\n */",
  154. ' ',
  155. ];
  156. yield 'It Converts a single line to multi line with correct indentation and Line ending' => [
  157. '/** Hello */',
  158. "/**\r\n * Hello\r\n */",
  159. ' ',
  160. "\r\n",
  161. ];
  162. }
  163. /**
  164. * @dataProvider provideMakeSingleLineCases
  165. */
  166. public function testMakeSingleLine(string $inputDocBlock, string $outputDocBlock = null): void
  167. {
  168. $doc = new DocBlock($inputDocBlock);
  169. $doc->makeSingleLine();
  170. if (null === $outputDocBlock) {
  171. $outputDocBlock = $inputDocBlock;
  172. }
  173. self::assertSame($outputDocBlock, $doc->getContent());
  174. }
  175. public static function provideMakeSingleLineCases(): iterable
  176. {
  177. yield 'It keeps a single line doc block as is' => [
  178. '/** Hello */',
  179. ];
  180. yield 'It converts a multi line doc block to a single line' => [
  181. "/**\n * Hello\n */",
  182. '/** Hello */',
  183. ];
  184. yield 'It converts a multi line doc block to a single line with annotation' => [
  185. "/**\n * @foo\n */",
  186. '/** @foo */',
  187. ];
  188. yield 'It converts a multi line doc block to a single line multiple empty lines' => [
  189. "/**\n * @foo\n *\n *\n *\n * */",
  190. '/** @foo */',
  191. ];
  192. yield 'It changes an empty doc block to single line' => [
  193. "/**\n *\n */",
  194. '/** */',
  195. ];
  196. yield 'It does not change a multi line doc block if it can\'t' => [
  197. self::$sample,
  198. ];
  199. }
  200. }