LineTest.php 5.2 KB

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