LineTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 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 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 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. public static function provideLinesCases(): iterable
  132. {
  133. foreach (self::$content as $index => $content) {
  134. yield [$index, $content];
  135. }
  136. }
  137. /**
  138. * @dataProvider provideUsefulCases
  139. */
  140. public function testUseful(int $pos, bool $useful): void
  141. {
  142. $doc = new DocBlock(self::$sample);
  143. $line = $doc->getLine($pos);
  144. self::assertSame($useful, $line->containsUsefulContent());
  145. }
  146. public static function provideUsefulCases(): iterable
  147. {
  148. foreach (self::$useful as $index => $useful) {
  149. yield [$index, $useful];
  150. }
  151. }
  152. /**
  153. * @dataProvider provideTagCases
  154. */
  155. public function testTag(int $pos, bool $tag): void
  156. {
  157. $doc = new DocBlock(self::$sample);
  158. $line = $doc->getLine($pos);
  159. self::assertSame($tag, $line->containsATag());
  160. }
  161. public static function provideTagCases(): iterable
  162. {
  163. foreach (self::$tag as $index => $tag) {
  164. yield [$index, $tag];
  165. }
  166. }
  167. public function testSetContent(): void
  168. {
  169. $line = new Line(" * @param \$foo Hi!\n");
  170. self::assertSame(" * @param \$foo Hi!\n", $line->getContent());
  171. $line->addBlank();
  172. self::assertSame(" * @param \$foo Hi!\n *\n", $line->getContent());
  173. $line->setContent("\t * test\r\n");
  174. self::assertSame("\t * test\r\n", $line->getContent());
  175. $line->addBlank();
  176. self::assertSame("\t * test\r\n\t *\r\n", $line->getContent());
  177. $line->remove();
  178. self::assertSame('', $line->getContent());
  179. }
  180. }