LineTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\DocBlock;
  12. use PhpCsFixer\DocBlock\DocBlock;
  13. use PhpCsFixer\DocBlock\Line;
  14. use PhpCsFixer\Tests\TestCase;
  15. /**
  16. * @author Graham Campbell <graham@alt-three.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\DocBlock\Line
  21. */
  22. final class LineTest 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. /**
  45. * This represents the content of each line.
  46. *
  47. * @var string[]
  48. */
  49. private static $content = [
  50. "/**\n",
  51. " * Test docblock.\n",
  52. " *\n",
  53. " * @param string \$hello\n",
  54. " * @param bool \$test Description\n",
  55. " * extends over many lines\n",
  56. " *\n",
  57. " * @param adkjbadjasbdand \$asdnjkasd\n",
  58. " *\n",
  59. " * @throws \\Exception asdnjkasd\n",
  60. " * asdasdasdasdasdasdasdasd\n",
  61. " * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb\n",
  62. " *\n",
  63. " * @return void\n",
  64. ' */',
  65. ];
  66. /**
  67. * This represents the if each line is "useful".
  68. *
  69. * @var bool[]
  70. */
  71. private static $useful = [
  72. false,
  73. true,
  74. false,
  75. true,
  76. true,
  77. true,
  78. false,
  79. true,
  80. false,
  81. true,
  82. true,
  83. true,
  84. false,
  85. true,
  86. false,
  87. ];
  88. /**
  89. * This represents the if each line "contains a tag".
  90. *
  91. * @var bool[]
  92. */
  93. private static $tag = [
  94. false,
  95. false,
  96. false,
  97. true,
  98. true,
  99. false,
  100. false,
  101. true,
  102. false,
  103. true,
  104. false,
  105. false,
  106. false,
  107. true,
  108. false,
  109. ];
  110. /**
  111. * @param int $pos
  112. * @param string $content
  113. *
  114. * @dataProvider provideLinesCases
  115. */
  116. public function testPosAndContent($pos, $content)
  117. {
  118. $doc = new DocBlock(self::$sample);
  119. $line = $doc->getLine($pos);
  120. static::assertSame($content, $line->getContent());
  121. static::assertSame($content, (string) $line);
  122. }
  123. /**
  124. * @param int $pos
  125. *
  126. * @dataProvider provideLinesCases
  127. */
  128. public function testStartOrEndPos($pos)
  129. {
  130. $doc = new DocBlock(self::$sample);
  131. $line = $doc->getLine($pos);
  132. static::assertSame(0 === $pos, $line->isTheStart());
  133. static::assertSame(14 === $pos, $line->isTheEnd());
  134. }
  135. public function provideLinesCases()
  136. {
  137. $cases = [];
  138. foreach (self::$content as $index => $content) {
  139. $cases[] = [$index, $content];
  140. }
  141. return $cases;
  142. }
  143. /**
  144. * @param int $pos
  145. * @param bool $useful
  146. *
  147. * @dataProvider provideLinesWithUsefulCases
  148. */
  149. public function testUseful($pos, $useful)
  150. {
  151. $doc = new DocBlock(self::$sample);
  152. $line = $doc->getLine($pos);
  153. static::assertSame($useful, $line->containsUsefulContent());
  154. }
  155. public function provideLinesWithUsefulCases()
  156. {
  157. $cases = [];
  158. foreach (self::$useful as $index => $useful) {
  159. $cases[] = [$index, $useful];
  160. }
  161. return $cases;
  162. }
  163. /**
  164. * @param int $pos
  165. * @param bool $tag
  166. *
  167. * @dataProvider provideLinesWithTagCases
  168. */
  169. public function testTag($pos, $tag)
  170. {
  171. $doc = new DocBlock(self::$sample);
  172. $line = $doc->getLine($pos);
  173. static::assertSame($tag, $line->containsATag());
  174. }
  175. public function provideLinesWithTagCases()
  176. {
  177. $cases = [];
  178. foreach (self::$tag as $index => $tag) {
  179. $cases[] = [$index, $tag];
  180. }
  181. return $cases;
  182. }
  183. public function testSetContent()
  184. {
  185. $line = new Line(" * @param \$foo Hi!\n");
  186. static::assertSame(" * @param \$foo Hi!\n", $line->getContent());
  187. $line->addBlank();
  188. static::assertSame(" * @param \$foo Hi!\n *\n", $line->getContent());
  189. $line->setContent("\t * test\r\n");
  190. static::assertSame("\t * test\r\n", $line->getContent());
  191. $line->addBlank();
  192. static::assertSame("\t * test\r\n\t *\r\n", $line->getContent());
  193. $line->remove();
  194. static::assertSame('', $line->getContent());
  195. }
  196. }