DocBlockTest.php 6.7 KB

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