LineTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\DocBlock\Line;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author Graham Campbell <hello@gjcampbell.co.uk>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\DocBlock\Line
  22. */
  23. final class LineTest extends TestCase
  24. {
  25. /**
  26. * This represents the content an entire docblock.
  27. */
  28. private static string $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. /**
  44. * This represents the content of each line.
  45. *
  46. * @var list<string>
  47. */
  48. private static array $content = [
  49. "/**\n",
  50. " * Test docblock.\n",
  51. " *\n",
  52. " * @param string \$hello\n",
  53. " * @param bool \$test Description\n",
  54. " * extends over many lines\n",
  55. " *\n",
  56. " * @param adkjbadjasbdand \$asdnjkasd\n",
  57. " *\n",
  58. " * @throws \\Exception asdnjkasd\n",
  59. " * asdasdasdasdasdasdasdasd\n",
  60. " * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb\n",
  61. " *\n",
  62. " * @return void\n",
  63. ' */',
  64. ];
  65. /**
  66. * This represents the if each line is "useful".
  67. *
  68. * @var list<bool>
  69. */
  70. private static array $useful = [
  71. false,
  72. true,
  73. false,
  74. true,
  75. true,
  76. true,
  77. false,
  78. true,
  79. false,
  80. true,
  81. true,
  82. true,
  83. false,
  84. true,
  85. false,
  86. ];
  87. /**
  88. * This represents the if each line "contains a tag".
  89. *
  90. * @var list<bool>
  91. */
  92. private static array $tag = [
  93. false,
  94. false,
  95. false,
  96. true,
  97. true,
  98. false,
  99. false,
  100. true,
  101. false,
  102. true,
  103. false,
  104. false,
  105. false,
  106. true,
  107. false,
  108. ];
  109. /**
  110. * @dataProvider provideLinesCases
  111. */
  112. public function testPosAndContent(int $pos, string $content): void
  113. {
  114. $doc = new DocBlock(self::$sample);
  115. $line = $doc->getLine($pos);
  116. self::assertSame($content, $line->getContent());
  117. self::assertSame($content, (string) $line);
  118. }
  119. /**
  120. * @dataProvider provideLinesCases
  121. */
  122. public function testStartOrEndPos(int $pos): void
  123. {
  124. $doc = new DocBlock(self::$sample);
  125. $line = $doc->getLine($pos);
  126. self::assertSame(0 === $pos, $line->isTheStart());
  127. self::assertSame(14 === $pos, $line->isTheEnd());
  128. }
  129. /**
  130. * @return iterable<array{int, string}>
  131. */
  132. public static function provideLinesCases(): iterable
  133. {
  134. foreach (self::$content as $index => $content) {
  135. yield [$index, $content];
  136. }
  137. }
  138. /**
  139. * @dataProvider provideUsefulCases
  140. */
  141. public function testUseful(int $pos, bool $useful): void
  142. {
  143. $doc = new DocBlock(self::$sample);
  144. $line = $doc->getLine($pos);
  145. self::assertSame($useful, $line->containsUsefulContent());
  146. }
  147. /**
  148. * @return iterable<array{int, bool}>
  149. */
  150. public static function provideUsefulCases(): iterable
  151. {
  152. foreach (self::$useful as $index => $useful) {
  153. yield [$index, $useful];
  154. }
  155. }
  156. /**
  157. * @dataProvider provideTagCases
  158. */
  159. public function testTag(int $pos, bool $tag): void
  160. {
  161. $doc = new DocBlock(self::$sample);
  162. $line = $doc->getLine($pos);
  163. self::assertSame($tag, $line->containsATag());
  164. }
  165. /**
  166. * @return iterable<array{int, bool}>
  167. */
  168. public static function provideTagCases(): iterable
  169. {
  170. foreach (self::$tag as $index => $tag) {
  171. yield [$index, $tag];
  172. }
  173. }
  174. public function testSetContent(): void
  175. {
  176. $line = new Line(" * @param \$foo Hi!\n");
  177. self::assertSame(" * @param \$foo Hi!\n", $line->getContent());
  178. $line->addBlank();
  179. self::assertSame(" * @param \$foo Hi!\n *\n", $line->getContent());
  180. $line->setContent("\t * test\r\n");
  181. self::assertSame("\t * test\r\n", $line->getContent());
  182. $line->addBlank();
  183. self::assertSame("\t * test\r\n\t *\r\n", $line->getContent());
  184. $line->remove();
  185. self::assertSame('', $line->getContent());
  186. }
  187. }