DocBlockTest.php 6.7 KB

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