No Description

DocBlockTest.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * phpDocumentor DocBlock Test
  4. *
  5. * PHP Version 5.3
  6. *
  7. * @author Mike van Riel <mike.vanriel@naenius.com>
  8. * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com)
  9. * @license http://www.opensource.org/licenses/mit-license.php MIT
  10. * @link http://phpdoc.org
  11. */
  12. namespace phpDocumentor\Reflection;
  13. use phpDocumentor\Reflection\DocBlock\Context;
  14. use phpDocumentor\Reflection\DocBlock\Location;
  15. use phpDocumentor\Reflection\DocBlock\Tag\ReturnTag;
  16. /**
  17. * Test class for phpDocumentor\Reflection\DocBlock
  18. *
  19. * @author Mike van Riel <mike.vanriel@naenius.com>
  20. * @copyright 2010-2011 Mike van Riel / Naenius. (http://www.naenius.com)
  21. * @license http://www.opensource.org/licenses/mit-license.php MIT
  22. * @link http://phpdoc.org
  23. */
  24. class DocBlockTest extends \PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @covers \phpDocumentor\Reflection\DocBlock
  28. *
  29. * @return void
  30. */
  31. public function testConstruct()
  32. {
  33. $fixture = <<<DOCBLOCK
  34. /**
  35. * This is a short description
  36. *
  37. * This is a long description
  38. *
  39. * @see \MyClass
  40. * @return void
  41. */
  42. DOCBLOCK;
  43. $object = new DocBlock(
  44. $fixture,
  45. new Context('\MyNamespace', array('PHPDoc' => '\phpDocumentor')),
  46. new Location(2)
  47. );
  48. $this->assertEquals(
  49. 'This is a short description',
  50. $object->getShortDescription()
  51. );
  52. $this->assertEquals(
  53. 'This is a long description',
  54. $object->getLongDescription()->getContents()
  55. );
  56. $this->assertCount(2, $object->getTags());
  57. $this->assertTrue($object->hasTag('see'));
  58. $this->assertTrue($object->hasTag('return'));
  59. $this->assertFalse($object->hasTag('category'));
  60. $this->assertSame('MyNamespace', $object->getContext()->getNamespace());
  61. $this->assertSame(
  62. array('PHPDoc' => '\phpDocumentor'),
  63. $object->getContext()->getNamespaceAliases()
  64. );
  65. $this->assertSame(2, $object->getLocation()->getLineNumber());
  66. }
  67. /**
  68. * @covers \phpDocumentor\Reflection\DocBlock::splitDocBlock
  69. *
  70. * @return void
  71. */
  72. public function testConstructWithTagsOnly()
  73. {
  74. $fixture = <<<DOCBLOCK
  75. /**
  76. * @see \MyClass
  77. * @return void
  78. */
  79. DOCBLOCK;
  80. $object = new DocBlock($fixture);
  81. $this->assertEquals('', $object->getShortDescription());
  82. $this->assertEquals('', $object->getLongDescription()->getContents());
  83. $this->assertCount(2, $object->getTags());
  84. $this->assertTrue($object->hasTag('see'));
  85. $this->assertTrue($object->hasTag('return'));
  86. $this->assertFalse($object->hasTag('category'));
  87. }
  88. /**
  89. * @covers \phpDocumentor\Reflection\DocBlock::isTemplateStart
  90. */
  91. public function testIfStartOfTemplateIsDiscovered()
  92. {
  93. $fixture = <<<DOCBLOCK
  94. /**#@+
  95. * @see \MyClass
  96. * @return void
  97. */
  98. DOCBLOCK;
  99. $object = new DocBlock($fixture);
  100. $this->assertEquals('', $object->getShortDescription());
  101. $this->assertEquals('', $object->getLongDescription()->getContents());
  102. $this->assertCount(2, $object->getTags());
  103. $this->assertTrue($object->hasTag('see'));
  104. $this->assertTrue($object->hasTag('return'));
  105. $this->assertFalse($object->hasTag('category'));
  106. $this->assertTrue($object->isTemplateStart());
  107. }
  108. /**
  109. * @covers \phpDocumentor\Reflection\DocBlock::isTemplateEnd
  110. */
  111. public function testIfEndOfTemplateIsDiscovered()
  112. {
  113. $fixture = <<<DOCBLOCK
  114. /**#@-*/
  115. DOCBLOCK;
  116. $object = new DocBlock($fixture);
  117. $this->assertEquals('', $object->getShortDescription());
  118. $this->assertEquals('', $object->getLongDescription()->getContents());
  119. $this->assertTrue($object->isTemplateEnd());
  120. }
  121. /**
  122. * @covers \phpDocumentor\Reflection\DocBlock::cleanInput
  123. *
  124. * @return void
  125. */
  126. public function testConstructOneLiner()
  127. {
  128. $fixture = '/** Short description and nothing more. */';
  129. $object = new DocBlock($fixture);
  130. $this->assertEquals(
  131. 'Short description and nothing more.',
  132. $object->getShortDescription()
  133. );
  134. $this->assertEquals('', $object->getLongDescription()->getContents());
  135. $this->assertCount(0, $object->getTags());
  136. }
  137. /**
  138. * @covers \phpDocumentor\Reflection\DocBlock::__construct
  139. *
  140. * @return void
  141. */
  142. public function testConstructFromReflector()
  143. {
  144. $object = new DocBlock(new \ReflectionClass($this));
  145. $this->assertEquals(
  146. 'Test class for phpDocumentor\Reflection\DocBlock',
  147. $object->getShortDescription()
  148. );
  149. $this->assertEquals('', $object->getLongDescription()->getContents());
  150. $this->assertCount(4, $object->getTags());
  151. $this->assertTrue($object->hasTag('author'));
  152. $this->assertTrue($object->hasTag('copyright'));
  153. $this->assertTrue($object->hasTag('license'));
  154. $this->assertTrue($object->hasTag('link'));
  155. $this->assertFalse($object->hasTag('category'));
  156. }
  157. /**
  158. * @expectedException \InvalidArgumentException
  159. *
  160. * @return void
  161. */
  162. public function testExceptionOnInvalidObject()
  163. {
  164. new DocBlock($this);
  165. }
  166. public function testDotSeperation()
  167. {
  168. $fixture = <<<DOCBLOCK
  169. /**
  170. * This is a short description.
  171. * This is a long description.
  172. * This is a continuation of the long description.
  173. */
  174. DOCBLOCK;
  175. $object = new DocBlock($fixture);
  176. $this->assertEquals(
  177. 'This is a short description.',
  178. $object->getShortDescription()
  179. );
  180. $this->assertEquals(
  181. "This is a long description.\nThis is a continuation of the long "
  182. ."description.",
  183. $object->getLongDescription()->getContents()
  184. );
  185. }
  186. /**
  187. * @covers \phpDocumentor\Reflection\DocBlock::parseTags
  188. * @expectedException \LogicException
  189. *
  190. * @return void
  191. */
  192. public function testInvalidTagBlock()
  193. {
  194. if (0 == ini_get('allow_url_include')) {
  195. $this->markTestSkipped('"data" URIs for includes are required.');
  196. }
  197. include 'data:text/plain;base64,'. base64_encode(
  198. <<<DOCBLOCK_EXTENSION
  199. <?php
  200. class MyReflectionDocBlock extends \phpDocumentor\Reflection\DocBlock {
  201. protected function splitDocBlock(\$comment) {
  202. return array('', '', 'Invalid tag block');
  203. }
  204. }
  205. DOCBLOCK_EXTENSION
  206. );
  207. new \MyReflectionDocBlock('');
  208. }
  209. public function testTagCaseSensitivity()
  210. {
  211. $fixture = <<<DOCBLOCK
  212. /**
  213. * This is a short description.
  214. *
  215. * This is a long description.
  216. *
  217. * @method null something()
  218. * @Method({"GET", "POST"})
  219. */
  220. DOCBLOCK;
  221. $object = new DocBlock($fixture);
  222. $this->assertEquals(
  223. 'This is a short description.',
  224. $object->getShortDescription()
  225. );
  226. $this->assertEquals(
  227. 'This is a long description.',
  228. $object->getLongDescription()->getContents()
  229. );
  230. $tags = $object->getTags();
  231. $this->assertCount(2, $tags);
  232. $this->assertTrue($object->hasTag('method'));
  233. $this->assertTrue($object->hasTag('Method'));
  234. $this->assertInstanceOf(
  235. __NAMESPACE__ . '\DocBlock\Tag\MethodTag',
  236. $tags[0]
  237. );
  238. $this->assertInstanceOf(
  239. __NAMESPACE__ . '\DocBlock\Tag',
  240. $tags[1]
  241. );
  242. $this->assertNotInstanceOf(
  243. __NAMESPACE__ . '\DocBlock\Tag\MethodTag',
  244. $tags[1]
  245. );
  246. }
  247. /**
  248. * @depends testConstructFromReflector
  249. * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName
  250. *
  251. * @return void
  252. */
  253. public function testGetTagsByNameZeroAndOneMatch()
  254. {
  255. $object = new DocBlock(new \ReflectionClass($this));
  256. $this->assertEmpty($object->getTagsByName('category'));
  257. $this->assertCount(1, $object->getTagsByName('author'));
  258. }
  259. /**
  260. * @depends testConstructWithTagsOnly
  261. * @covers \phpDocumentor\Reflection\DocBlock::parseTags
  262. *
  263. * @return void
  264. */
  265. public function testParseMultilineTag()
  266. {
  267. $fixture = <<<DOCBLOCK
  268. /**
  269. * @return void Content on
  270. * multiple lines.
  271. */
  272. DOCBLOCK;
  273. $object = new DocBlock($fixture);
  274. $this->assertCount(1, $object->getTags());
  275. }
  276. /**
  277. * @depends testConstructWithTagsOnly
  278. * @covers \phpDocumentor\Reflection\DocBlock::parseTags
  279. *
  280. * @return void
  281. */
  282. public function testParseMultilineTagWithLineBreaks()
  283. {
  284. $fixture = <<<DOCBLOCK
  285. /**
  286. * @return void Content on
  287. * multiple lines.
  288. *
  289. * One more, after the break.
  290. */
  291. DOCBLOCK;
  292. $object = new DocBlock($fixture);
  293. $this->assertCount(1, $tags = $object->getTags());
  294. /** @var ReturnTag $tag */
  295. $tag = reset($tags);
  296. $this->assertEquals("Content on\n multiple lines.\n\n One more, after the break.", $tag->getDescription());
  297. }
  298. /**
  299. * @depends testConstructWithTagsOnly
  300. * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName
  301. *
  302. * @return void
  303. */
  304. public function testGetTagsByNameMultipleMatch()
  305. {
  306. $fixture = <<<DOCBLOCK
  307. /**
  308. * @param string
  309. * @param int
  310. * @return void
  311. */
  312. DOCBLOCK;
  313. $object = new DocBlock($fixture);
  314. $this->assertEmpty($object->getTagsByName('category'));
  315. $this->assertCount(1, $object->getTagsByName('return'));
  316. $this->assertCount(2, $object->getTagsByName('param'));
  317. }
  318. }