DocBlockTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * This file is part of phpDocumentor.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
  9. * @license http://www.opensource.org/licenses/mit-license.php MIT
  10. * @link http://phpdoc.org
  11. */
  12. namespace phpDocumentor\Reflection;
  13. use Mockery as m;
  14. use phpDocumentor\Reflection\Types\Context;
  15. /**
  16. * @coversDefaultClass phpDocumentor\Reflection\DocBlock
  17. * @covers ::<private>
  18. * @uses \Webmozart\Assert\Assert
  19. */
  20. class DocBlockTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @covers ::__construct
  24. * @covers ::getSummary
  25. *
  26. * @uses \phpDocumentor\Reflection\DocBlock\Description
  27. */
  28. public function testDocBlockCanHaveASummary()
  29. {
  30. $summary = 'This is a summary';
  31. $fixture = new DocBlock($summary);
  32. $this->assertSame($summary, $fixture->getSummary());
  33. }
  34. /**
  35. * @covers ::__construct
  36. *
  37. * @expectedException \InvalidArgumentException
  38. */
  39. public function testExceptionIsThrownIfSummaryIsNotAString()
  40. {
  41. new DocBlock([]);
  42. }
  43. /**
  44. * @covers ::__construct
  45. *
  46. * @expectedException \InvalidArgumentException
  47. */
  48. public function testExceptionIsThrownIfTemplateStartIsNotABoolean()
  49. {
  50. new DocBlock('', null, [], null, null, ['is not boolean']);
  51. }
  52. /**
  53. * @covers ::__construct
  54. *
  55. * @expectedException \InvalidArgumentException
  56. */
  57. public function testExceptionIsThrownIfTemplateEndIsNotABoolean()
  58. {
  59. new DocBlock('', null, [], null, null, false, ['is not boolean']);
  60. }
  61. /**
  62. * @covers ::__construct
  63. * @covers ::getDescription
  64. *
  65. * @uses \phpDocumentor\Reflection\DocBlock\Description
  66. */
  67. public function testDocBlockCanHaveADescription()
  68. {
  69. $description = new DocBlock\Description('');
  70. $fixture = new DocBlock('', $description);
  71. $this->assertSame($description, $fixture->getDescription());
  72. }
  73. /**
  74. * @covers ::__construct
  75. * @covers ::getTags
  76. *
  77. * @uses \phpDocumentor\Reflection\DocBlock\Description
  78. * @uses \phpDocumentor\Reflection\DocBlock\Tag
  79. */
  80. public function testDocBlockCanHaveTags()
  81. {
  82. $tags = [
  83. m::mock(DocBlock\Tag::class)
  84. ];
  85. $fixture = new DocBlock('', null, $tags);
  86. $this->assertSame($tags, $fixture->getTags());
  87. }
  88. /**
  89. * @covers ::__construct
  90. * @covers ::getTags
  91. *
  92. * @uses \phpDocumentor\Reflection\DocBlock\Description
  93. * @uses \phpDocumentor\Reflection\DocBlock\Tag
  94. *
  95. * @expectedException \InvalidArgumentException
  96. */
  97. public function testDocBlockAllowsOnlyTags()
  98. {
  99. $tags = [
  100. null
  101. ];
  102. $fixture = new DocBlock('', null, $tags);
  103. }
  104. /**
  105. * @covers ::__construct
  106. * @covers ::getTagsByName
  107. *
  108. * @uses \phpDocumentor\Reflection\DocBlock::getTags
  109. * @uses \phpDocumentor\Reflection\DocBlock\Description
  110. * @uses \phpDocumentor\Reflection\DocBlock\Tag
  111. */
  112. public function testFindTagsInDocBlockByName()
  113. {
  114. $tag1 = m::mock(DocBlock\Tag::class);
  115. $tag2 = m::mock(DocBlock\Tag::class);
  116. $tag3 = m::mock(DocBlock\Tag::class);
  117. $tags = [$tag1, $tag2, $tag3];
  118. $tag1->shouldReceive('getName')->andReturn('abc');
  119. $tag2->shouldReceive('getName')->andReturn('abcd');
  120. $tag3->shouldReceive('getName')->andReturn('ab');
  121. $fixture = new DocBlock('', null, $tags);
  122. $this->assertSame([$tag2], $fixture->getTagsByName('abcd'));
  123. $this->assertSame([], $fixture->getTagsByName('Ebcd'));
  124. }
  125. /**
  126. * @covers ::__construct
  127. * @covers ::getTagsByName
  128. * @uses \phpDocumentor\Reflection\DocBlock\Description
  129. * @expectedException \InvalidArgumentException
  130. */
  131. public function testExceptionIsThrownIfNameForTagsIsNotString()
  132. {
  133. $fixture = new DocBlock();
  134. $fixture->getTagsByName([]);
  135. }
  136. /**
  137. * @covers ::__construct
  138. * @covers ::hasTag
  139. *
  140. * @uses \phpDocumentor\Reflection\DocBlock::getTags
  141. * @uses \phpDocumentor\Reflection\DocBlock\Description
  142. * @uses \phpDocumentor\Reflection\DocBlock\Tag
  143. */
  144. public function testCheckIfThereAreTagsWithAGivenName()
  145. {
  146. $tag1 = m::mock(DocBlock\Tag::class);
  147. $tag2 = m::mock(DocBlock\Tag::class);
  148. $tag3 = m::mock(DocBlock\Tag::class);
  149. $tags = [$tag1, $tag2, $tag3];
  150. $tag1->shouldReceive('getName')->twice()->andReturn('abc');
  151. $tag2->shouldReceive('getName')->twice()->andReturn('abcd');
  152. $tag3->shouldReceive('getName')->once();
  153. $fixture = new DocBlock('', null, $tags);
  154. $this->assertTrue($fixture->hasTag('abcd'));
  155. $this->assertFalse($fixture->hasTag('Ebcd'));
  156. }
  157. /**
  158. * @covers ::__construct
  159. * @covers ::hasTag
  160. * @uses \phpDocumentor\Reflection\DocBlock\Description
  161. * @expectedException \InvalidArgumentException
  162. */
  163. public function testExceptionIsThrownIfNameForCheckingTagsIsNotString()
  164. {
  165. $fixture = new DocBlock();
  166. $fixture->hasTag([]);
  167. }
  168. /**
  169. * @covers ::__construct
  170. * @covers ::getContext
  171. *
  172. * @uses \phpDocumentor\Reflection\DocBlock\Description
  173. * @uses \phpDocumentor\Reflection\Types\Context
  174. */
  175. public function testDocBlockKnowsInWhichNamespaceItIsAndWhichAliasesThereAre()
  176. {
  177. $context = new Context('');
  178. $fixture = new DocBlock('', null, [], $context);
  179. $this->assertSame($context, $fixture->getContext());
  180. }
  181. /**
  182. * @covers ::__construct
  183. * @covers ::getLocation
  184. *
  185. * @uses \phpDocumentor\Reflection\DocBlock\Description
  186. * @uses \phpDocumentor\Reflection\Location
  187. */
  188. public function testDocBlockKnowsAtWhichLineItIs()
  189. {
  190. $location = new Location(10);
  191. $fixture = new DocBlock('', null, [], null, $location);
  192. $this->assertSame($location, $fixture->getLocation());
  193. }
  194. /**
  195. * @covers ::__construct
  196. * @covers ::isTemplateStart
  197. *
  198. * @uses \phpDocumentor\Reflection\DocBlock\Description
  199. */
  200. public function testDocBlockKnowsIfItIsTheStartOfADocBlockTemplate()
  201. {
  202. $fixture = new DocBlock('', null, [], null, null, true);
  203. $this->assertTrue($fixture->isTemplateStart());
  204. }
  205. /**
  206. * @covers ::__construct
  207. * @covers ::isTemplateEnd
  208. *
  209. * @uses \phpDocumentor\Reflection\DocBlock\Description
  210. */
  211. public function testDocBlockKnowsIfItIsTheEndOfADocBlockTemplate()
  212. {
  213. $fixture = new DocBlock('', null, [], null, null, false, true);
  214. $this->assertTrue($fixture->isTemplateEnd());
  215. }
  216. }