LineTest.php 4.8 KB

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