AnnotationTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Tests\DocBlock;
  11. use Symfony\CS\DocBlock\Annotation;
  12. use Symfony\CS\DocBlock\DocBlock;
  13. use Symfony\CS\DocBlock\Line;
  14. /**
  15. * @author Graham Campbell <graham@mineuk.com>
  16. *
  17. * @internal
  18. */
  19. final class AnnotationTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /**
  22. * This represents the content an entire docblock.
  23. *
  24. * @var string
  25. */
  26. private static $sample = '/**
  27. * Test docblock.
  28. *
  29. * @param string $hello
  30. * @param bool $test Description
  31. * extends over many lines
  32. *
  33. * @param adkjbadjasbdand $asdnjkasd
  34. *
  35. * @throws \Exception asdnjkasd
  36. *
  37. * asdasdasdasdasdasdasdasd
  38. * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb
  39. *
  40. * @return void
  41. */';
  42. /**
  43. * This represents the content of each annotation.
  44. *
  45. * @var string[]
  46. */
  47. private static $content = array(
  48. " * @param string \$hello\n",
  49. " * @param bool \$test Description\n * extends over many lines\n",
  50. " * @param adkjbadjasbdand \$asdnjkasd\n",
  51. " * @throws \Exception asdnjkasd\n *\n * asdasdasdasdasdasdasdasd\n * kasdkasdkbasdasdasdjhbasdhbasjdbjasbdjhb\n",
  52. " * @return void\n",
  53. );
  54. /**
  55. * This represents the start indexes of each annotation.
  56. *
  57. * @var int[]
  58. */
  59. private static $start = array(3, 4, 7, 9, 14);
  60. /**
  61. * This represents the start indexes of each annotation.
  62. *
  63. * @var int[]
  64. */
  65. private static $end = array(3, 5, 7, 12, 14);
  66. /**
  67. * This represents the tag type of each annotation.
  68. *
  69. * @var int[]
  70. */
  71. private static $tags = array('param', 'param', 'param', 'throws', 'return');
  72. /**
  73. * @dataProvider provideContent
  74. */
  75. public function testGetContent($index, $content)
  76. {
  77. $doc = new DocBlock(self::$sample);
  78. $annotation = $doc->getAnnotation($index);
  79. $this->assertSame($content, $annotation->getContent());
  80. $this->assertSame($content, (string) $annotation);
  81. }
  82. public function provideContent()
  83. {
  84. $cases = array();
  85. foreach (self::$content as $index => $content) {
  86. $cases[] = array($index, $content);
  87. }
  88. return $cases;
  89. }
  90. /**
  91. * @dataProvider provideStartCases
  92. */
  93. public function testStart($index, $start)
  94. {
  95. $doc = new DocBlock(self::$sample);
  96. $annotation = $doc->getAnnotation($index);
  97. $this->assertSame($start, $annotation->getStart());
  98. }
  99. public function provideStartCases()
  100. {
  101. $cases = array();
  102. foreach (self::$start as $index => $start) {
  103. $cases[] = array($index, $start);
  104. }
  105. return $cases;
  106. }
  107. /**
  108. * @dataProvider provideEndCases
  109. */
  110. public function testEnd($index, $end)
  111. {
  112. $doc = new DocBlock(self::$sample);
  113. $annotation = $doc->getAnnotation($index);
  114. $this->assertSame($end, $annotation->getEnd());
  115. }
  116. public function provideEndCases()
  117. {
  118. $cases = array();
  119. foreach (self::$end as $index => $end) {
  120. $cases[] = array($index, $end);
  121. }
  122. return $cases;
  123. }
  124. /**
  125. * @dataProvider provideTags
  126. */
  127. public function testGetTag($index, $tag)
  128. {
  129. $doc = new DocBlock(self::$sample);
  130. $annotation = $doc->getAnnotation($index);
  131. $this->assertSame($tag, $annotation->getTag()->getName());
  132. }
  133. public function provideTags()
  134. {
  135. $cases = array();
  136. foreach (self::$tags as $index => $tag) {
  137. $cases[] = array($index, $tag);
  138. }
  139. return $cases;
  140. }
  141. /**
  142. * @dataProvider provideRemoveCases
  143. */
  144. public function testRemove($index, $start, $end)
  145. {
  146. $doc = new DocBlock(self::$sample);
  147. $annotation = $doc->getAnnotation($index);
  148. $annotation->remove();
  149. $this->assertSame('', $annotation->getContent());
  150. $this->assertSame('', $doc->getLine($start)->getContent());
  151. $this->assertSame('', $doc->getLine($end)->getContent());
  152. }
  153. public function provideRemoveCases()
  154. {
  155. $cases = array();
  156. foreach (self::$start as $index => $start) {
  157. $cases[] = array($index, $start, self::$end[$index]);
  158. }
  159. return $cases;
  160. }
  161. /**
  162. * @dataProvider provideTypesCases
  163. */
  164. public function testTypes($expected, $new, $input, $output)
  165. {
  166. $line = new Line($input);
  167. $tag = new Annotation(array($line));
  168. $this->assertSame($expected, $tag->getTypes());
  169. $tag->setTypes($new);
  170. $this->assertSame($new, $tag->getTypes());
  171. $this->assertSame($output, $line->getContent());
  172. }
  173. public function provideTypesCases()
  174. {
  175. return array(
  176. array(array('Foo', 'null'), array('Bar[]'), ' * @param Foo|null $foo', ' * @param Bar[] $foo'),
  177. array(array('false'), array('bool'), '* @return false', '* @return bool'),
  178. array(array('RUNTIMEEEEeXCEPTION'), array('Throwable'), "\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"),
  179. array(array('string'), array('string', 'null'), ' * @method string getString()', ' * @method string|null getString()'),
  180. );
  181. }
  182. /**
  183. * @expectedException \RuntimeException
  184. * @expectedExceptionMessage This tag does not support types
  185. */
  186. public function testGetTypesOnBadTag()
  187. {
  188. $tag = new Annotation(array(new Line(' * @deprecated since 1.2')));
  189. $tag->getTypes();
  190. }
  191. /**
  192. * @expectedException \RuntimeException
  193. * @expectedExceptionMessage This tag does not support types
  194. */
  195. public function testSetTypesOnBadTag()
  196. {
  197. $tag = new Annotation(array(new Line(' * @author Chuck Norris')));
  198. $tag->setTypes(array('string'));
  199. }
  200. public function testGetTagsWithTypes()
  201. {
  202. $tags = Annotation::getTagsWithTypes();
  203. $this->assertInternalType('array', $tags);
  204. foreach ($tags as $tag) {
  205. $this->assertInternalType('string', $tag);
  206. $this->assertNotEmpty($tag);
  207. }
  208. }
  209. }