AnnotationTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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\Annotation;
  13. use PhpCsFixer\DocBlock\DocBlock;
  14. use PhpCsFixer\DocBlock\Line;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author Graham Campbell <graham@alt-three.com>
  18. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\DocBlock\Annotation
  23. */
  24. final class AnnotationTest extends TestCase
  25. {
  26. /**
  27. * This represents the content an entire docblock.
  28. *
  29. * @var string
  30. */
  31. private static $sample = '/**
  32. * Test docblock.
  33. *
  34. * @param string $hello
  35. * @param bool $test Description
  36. * extends over many lines
  37. *
  38. * @param adkjbadjasbdand $asdnjkasd
  39. *
  40. * @throws \Exception asdnjkasd
  41. *
  42. * asdasdasdasdasdasdasdasd
  43. * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
  44. *
  45. * @return void
  46. */';
  47. /**
  48. * This represents the content of each annotation.
  49. *
  50. * @var string[]
  51. */
  52. private static $content = [
  53. " * @param string \$hello\n",
  54. " * @param bool \$test Description\n * extends over many lines\n",
  55. " * @param adkjbadjasbdand \$asdnjkasd\n",
  56. " * @throws \\Exception asdnjkasd\n *\n * asdasdasdasdasdasdasdasd\n * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb\n",
  57. " * @return void\n",
  58. ];
  59. /**
  60. * This represents the start indexes of each annotation.
  61. *
  62. * @var int[]
  63. */
  64. private static $start = [3, 4, 7, 9, 14];
  65. /**
  66. * This represents the start indexes of each annotation.
  67. *
  68. * @var int[]
  69. */
  70. private static $end = [3, 5, 7, 12, 14];
  71. /**
  72. * This represents the tag type of each annotation.
  73. *
  74. * @var string[]
  75. */
  76. private static $tags = ['param', 'param', 'param', 'throws', 'return'];
  77. /**
  78. * @param int $index
  79. * @param string $content
  80. *
  81. * @dataProvider provideGetContentCases
  82. */
  83. public function testGetContent($index, $content)
  84. {
  85. $doc = new DocBlock(self::$sample);
  86. $annotation = $doc->getAnnotation($index);
  87. static::assertSame($content, $annotation->getContent());
  88. static::assertSame($content, (string) $annotation);
  89. }
  90. public function provideGetContentCases()
  91. {
  92. $cases = [];
  93. foreach (self::$content as $index => $content) {
  94. $cases[] = [$index, $content];
  95. }
  96. return $cases;
  97. }
  98. /**
  99. * @param int $index
  100. * @param int $start
  101. *
  102. * @dataProvider provideStartCases
  103. */
  104. public function testStart($index, $start)
  105. {
  106. $doc = new DocBlock(self::$sample);
  107. $annotation = $doc->getAnnotation($index);
  108. static::assertSame($start, $annotation->getStart());
  109. }
  110. public function provideStartCases()
  111. {
  112. $cases = [];
  113. foreach (self::$start as $index => $start) {
  114. $cases[] = [$index, $start];
  115. }
  116. return $cases;
  117. }
  118. /**
  119. * @param int $index
  120. * @param int $end
  121. *
  122. * @dataProvider provideEndCases
  123. */
  124. public function testEnd($index, $end)
  125. {
  126. $doc = new DocBlock(self::$sample);
  127. $annotation = $doc->getAnnotation($index);
  128. static::assertSame($end, $annotation->getEnd());
  129. }
  130. public function provideEndCases()
  131. {
  132. $cases = [];
  133. foreach (self::$end as $index => $end) {
  134. $cases[] = [$index, $end];
  135. }
  136. return $cases;
  137. }
  138. /**
  139. * @param int $index
  140. * @param string $tag
  141. *
  142. * @dataProvider provideGetTagCases
  143. */
  144. public function testGetTag($index, $tag)
  145. {
  146. $doc = new DocBlock(self::$sample);
  147. $annotation = $doc->getAnnotation($index);
  148. static::assertSame($tag, $annotation->getTag()->getName());
  149. }
  150. public function provideGetTagCases()
  151. {
  152. $cases = [];
  153. foreach (self::$tags as $index => $tag) {
  154. $cases[] = [$index, $tag];
  155. }
  156. return $cases;
  157. }
  158. /**
  159. * @param int $index
  160. * @param int $start
  161. * @param int $end
  162. *
  163. * @dataProvider provideRemoveCases
  164. */
  165. public function testRemove($index, $start, $end)
  166. {
  167. $doc = new DocBlock(self::$sample);
  168. $annotation = $doc->getAnnotation($index);
  169. $annotation->remove();
  170. static::assertSame('', $annotation->getContent());
  171. static::assertSame('', $doc->getLine($start)->getContent());
  172. static::assertSame('', $doc->getLine($end)->getContent());
  173. }
  174. public function provideRemoveCases()
  175. {
  176. $cases = [];
  177. foreach (self::$start as $index => $start) {
  178. $cases[] = [$index, $start, self::$end[$index]];
  179. }
  180. return $cases;
  181. }
  182. /**
  183. * @param string $expected
  184. * @param string $input
  185. *
  186. * @dataProvider provideRemoveEdgeCasesCases
  187. */
  188. public function testRemoveEdgeCases($expected, $input)
  189. {
  190. $doc = new DocBlock($input);
  191. $annotation = $doc->getAnnotation(0);
  192. $annotation->remove();
  193. static::assertSame($expected, $doc->getContent());
  194. }
  195. public function provideRemoveEdgeCasesCases()
  196. {
  197. return [
  198. // Single line
  199. ['', '/** @return null*/'],
  200. ['', '/** @return null */'],
  201. ['', '/** @return null */'],
  202. // Multi line, annotation on start line
  203. [
  204. '/**
  205. */',
  206. '/** @return null
  207. */',
  208. ],
  209. [
  210. '/**
  211. */',
  212. '/** @return null '.'
  213. */',
  214. ],
  215. // Multi line, annotation on end line
  216. [
  217. '/**
  218. */',
  219. '/**
  220. * @return null*/',
  221. ],
  222. [
  223. '/**
  224. */',
  225. '/**
  226. * @return null */',
  227. ],
  228. ];
  229. }
  230. /**
  231. * @param string $input
  232. * @param string[] $expected
  233. *
  234. * @dataProvider provideTypeParsingCases
  235. */
  236. public function testTypeParsing($input, array $expected)
  237. {
  238. $tag = new Annotation([new Line($input)]);
  239. static::assertSame($expected, $tag->getTypes());
  240. }
  241. public function provideTypeParsingCases()
  242. {
  243. return [
  244. [
  245. ' * @method int method()',
  246. ['int'],
  247. ],
  248. [
  249. ' * @method Foo[][] method()',
  250. ['Foo[][]'],
  251. ],
  252. [
  253. ' * @method int[] method()',
  254. ['int[]'],
  255. ],
  256. [
  257. ' * @method int[]|null method()',
  258. ['int[]', 'null'],
  259. ],
  260. [
  261. ' * @method int[]|null|?int|array method()',
  262. ['int[]', 'null', '?int', 'array'],
  263. ],
  264. [
  265. ' * @method null|Foo\Bar|\Baz\Bax|int[] method()',
  266. ['null', 'Foo\Bar', '\Baz\Bax', 'int[]'],
  267. ],
  268. [
  269. ' * @method gen<int> method()',
  270. ['gen<int>'],
  271. ],
  272. [
  273. ' * @method int|gen<int> method()',
  274. ['int', 'gen<int>'],
  275. ],
  276. [
  277. ' * @method \int|\gen<\int, \bool> method()',
  278. ['\int', '\gen<\int, \bool>'],
  279. ],
  280. [
  281. ' * @method gen<int, int> method()',
  282. ['gen<int, int>'],
  283. ],
  284. [
  285. ' * @method gen<int, bool|string> method()',
  286. ['gen<int, bool|string>'],
  287. ],
  288. [
  289. ' * @method gen<int, string[]> method() <> a',
  290. ['gen<int, string[]>'],
  291. ],
  292. [
  293. ' * @method gen<int, gener<string, bool>> method() foo <a >',
  294. ['gen<int, gener<string, bool>>'],
  295. ],
  296. [
  297. ' * @method gen<int, gener<string, null|bool>> method()',
  298. ['gen<int, gener<string, null|bool>>'],
  299. ],
  300. [
  301. ' * @method null|gen<int, gener<string, bool>>|int|string[] method() foo <a >',
  302. ['null', 'gen<int, gener<string, bool>>', 'int', 'string[]'],
  303. ],
  304. [
  305. ' * @method null|gen<int, gener<string, bool>>|int|array<int, string>|string[] method() foo <a >',
  306. ['null', 'gen<int, gener<string, bool>>', 'int', 'array<int, string>', 'string[]'],
  307. ],
  308. [
  309. '/** @return this */',
  310. ['this'],
  311. ],
  312. [
  313. '/** @return @this */',
  314. ['@this'],
  315. ],
  316. [
  317. '/** @return $SELF|int */',
  318. ['$SELF', 'int'],
  319. ],
  320. [
  321. '/** @var array<string|int, string>',
  322. ['array<string|int, string>'],
  323. ],
  324. ];
  325. }
  326. /**
  327. * @param string[] $expected
  328. * @param string[] $new
  329. * @param string $input
  330. * @param string $output
  331. *
  332. * @dataProvider provideTypesCases
  333. */
  334. public function testTypes($expected, $new, $input, $output)
  335. {
  336. $line = new Line($input);
  337. $tag = new Annotation([$line]);
  338. static::assertSame($expected, $tag->getTypes());
  339. $tag->setTypes($new);
  340. static::assertSame($new, $tag->getTypes());
  341. static::assertSame($output, $line->getContent());
  342. }
  343. public function provideTypesCases()
  344. {
  345. return [
  346. [['Foo', 'null'], ['Bar[]'], ' * @param Foo|null $foo', ' * @param Bar[] $foo'],
  347. [['false'], ['bool'], '* @return false', '* @return bool'],
  348. [['RUNTIMEEEEeXCEPTION'], [\Throwable::class], "* \t@throws\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n", "* \t@throws\t \t Throwable\t\t\t\t\t\t\t\n\n\n"],
  349. [['RUNTIMEEEEeXCEPTION'], [\Throwable::class], "*\t@throws\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n", "*\t@throws\t \t Throwable\t\t\t\t\t\t\t\n\n\n"],
  350. [['RUNTIMEEEEeXCEPTION'], [\Throwable::class], "*@throws\t \t RUNTIMEEEEeXCEPTION\t\t\t\t\t\t\t\n\n\n", "*@throws\t \t Throwable\t\t\t\t\t\t\t\n\n\n"],
  351. [['string'], ['string', 'null'], ' * @method string getString()', ' * @method string|null getString()'],
  352. ];
  353. }
  354. /**
  355. * @param string[] $expected
  356. * @param string $input
  357. *
  358. * @dataProvider provideNormalizedTypesCases
  359. */
  360. public function testNormalizedTypes($expected, $input)
  361. {
  362. $line = new Line($input);
  363. $tag = new Annotation([$line]);
  364. static::assertSame($expected, $tag->getNormalizedTypes());
  365. }
  366. public function provideNormalizedTypesCases()
  367. {
  368. return [
  369. [['null', 'string'], '* @param StRiNg|NuLl $foo'],
  370. [['void'], '* @return Void'],
  371. [['bar', 'baz', 'foo', 'null', 'qux'], '* @return Foo|Bar|Baz|Qux|null'],
  372. ];
  373. }
  374. public function testGetTypesOnBadTag()
  375. {
  376. $this->expectException(\RuntimeException::class);
  377. $this->expectExceptionMessage('This tag does not support types');
  378. $tag = new Annotation([new Line(' * @deprecated since 1.2')]);
  379. $tag->getTypes();
  380. }
  381. public function testSetTypesOnBadTag()
  382. {
  383. $this->expectException(\RuntimeException::class);
  384. $this->expectExceptionMessage('This tag does not support types');
  385. $tag = new Annotation([new Line(' * @author Chuck Norris')]);
  386. $tag->setTypes(['string']);
  387. }
  388. public function testGetTagsWithTypes()
  389. {
  390. $tags = Annotation::getTagsWithTypes();
  391. static::assertInternalType('array', $tags);
  392. foreach ($tags as $tag) {
  393. static::assertInternalType('string', $tag);
  394. static::assertNotEmpty($tag);
  395. }
  396. }
  397. }