LineTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $this->assertSame($content, $line->getContent());
  121. }
  122. /**
  123. * @param int $pos
  124. *
  125. * @dataProvider provideLinesCases
  126. */
  127. public function testStartOrEndPos($pos)
  128. {
  129. $doc = new DocBlock(self::$sample);
  130. $line = $doc->getLine($pos);
  131. $this->assertSame(0 === $pos, $line->isTheStart());
  132. $this->assertSame(14 === $pos, $line->isTheEnd());
  133. }
  134. public function provideLinesCases()
  135. {
  136. $cases = [];
  137. foreach (self::$content as $index => $content) {
  138. $cases[] = [$index, $content];
  139. }
  140. return $cases;
  141. }
  142. /**
  143. * @param int $pos
  144. * @param bool $useful
  145. *
  146. * @dataProvider provideLinesWithUsefulCases
  147. */
  148. public function testUseful($pos, $useful)
  149. {
  150. $doc = new DocBlock(self::$sample);
  151. $line = $doc->getLine($pos);
  152. $this->assertSame($useful, $line->containsUsefulContent());
  153. }
  154. public function provideLinesWithUsefulCases()
  155. {
  156. $cases = [];
  157. foreach (self::$useful as $index => $useful) {
  158. $cases[] = [$index, $useful];
  159. }
  160. return $cases;
  161. }
  162. /**
  163. * @param int $pos
  164. * @param bool $tag
  165. *
  166. * @dataProvider provideLinesWithTagCases
  167. */
  168. public function testTag($pos, $tag)
  169. {
  170. $doc = new DocBlock(self::$sample);
  171. $line = $doc->getLine($pos);
  172. $this->assertSame($tag, $line->containsATag());
  173. }
  174. public function provideLinesWithTagCases()
  175. {
  176. $cases = [];
  177. foreach (self::$tag as $index => $tag) {
  178. $cases[] = [$index, $tag];
  179. }
  180. return $cases;
  181. }
  182. public function testSetContent()
  183. {
  184. $line = new Line(" * @param \$foo Hi!\n");
  185. $this->assertSame(" * @param \$foo Hi!\n", $line->getContent());
  186. $line->addBlank();
  187. $this->assertSame(" * @param \$foo Hi!\n *\n", $line->getContent());
  188. $line->setContent("\t * test\r\n");
  189. $this->assertSame("\t * test\r\n", $line->getContent());
  190. $line->addBlank();
  191. $this->assertSame("\t * test\r\n\t *\r\n", $line->getContent());
  192. $line->remove();
  193. $this->assertSame('', $line->getContent());
  194. }
  195. }