DocBlockFactoryTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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\DocBlock\Description;
  15. use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
  16. use phpDocumentor\Reflection\DocBlock\Tag;
  17. use phpDocumentor\Reflection\DocBlock\TagFactory;
  18. use phpDocumentor\Reflection\DocBlock\Tags\Param;
  19. use phpDocumentor\Reflection\Types\Context;
  20. /**
  21. * @coversDefaultClass phpDocumentor\Reflection\DocBlockFactory
  22. * @covers ::<private>
  23. * @uses \Webmozart\Assert\Assert
  24. * @uses phpDocumentor\Reflection\DocBlock
  25. */
  26. class DocBlockFactoryTest extends \PHPUnit_Framework_TestCase
  27. {
  28. /**
  29. * @covers ::__construct
  30. * @covers ::createInstance
  31. * @uses \phpDocumentor\Reflection\DocBlock\StandardTagFactory
  32. * @uses \phpDocumentor\Reflection\DocBlock\DescriptionFactory
  33. */
  34. public function testCreateFactoryUsingFactoryMethod()
  35. {
  36. $fixture = DocBlockFactory::createInstance();
  37. $this->assertInstanceOf(DocBlockFactory::class, $fixture);
  38. }
  39. /**
  40. * @covers ::__construct
  41. * @covers ::create
  42. * @uses phpDocumentor\Reflection\DocBlock\Description
  43. */
  44. public function testCreateDocBlockFromReflection()
  45. {
  46. $fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), m::mock(TagFactory::class));
  47. $docBlock = '/** This is a DocBlock */';
  48. $classReflector = m::mock(\ReflectionClass::class);
  49. $classReflector->shouldReceive('getDocComment')->andReturn($docBlock);
  50. $docblock = $fixture->create($classReflector);
  51. $this->assertInstanceOf(DocBlock::class, $docblock);
  52. $this->assertSame('This is a DocBlock', $docblock->getSummary());
  53. $this->assertEquals(new Description(''), $docblock->getDescription());
  54. $this->assertSame([], $docblock->getTags());
  55. $this->assertEquals(new Context(''), $docblock->getContext());
  56. $this->assertNull($docblock->getLocation());
  57. }
  58. /**
  59. * @covers ::__construct
  60. * @covers ::create
  61. * @uses phpDocumentor\Reflection\DocBlock\Description
  62. */
  63. public function testCreateDocBlockFromStringWithDocComment()
  64. {
  65. $fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), m::mock(TagFactory::class));
  66. $docblock = $fixture->create('/** This is a DocBlock */');
  67. $this->assertInstanceOf(DocBlock::class, $docblock);
  68. $this->assertSame('This is a DocBlock', $docblock->getSummary());
  69. $this->assertEquals(new Description(''), $docblock->getDescription());
  70. $this->assertSame([], $docblock->getTags());
  71. $this->assertEquals(new Context(''), $docblock->getContext());
  72. $this->assertNull($docblock->getLocation());
  73. }
  74. /**
  75. * @covers ::create
  76. * @covers ::__construct
  77. * @uses phpDocumentor\Reflection\DocBlock\Description
  78. */
  79. public function testCreateDocBlockFromStringWithoutDocComment()
  80. {
  81. $fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), m::mock(TagFactory::class));
  82. $docblock = $fixture->create('This is a DocBlock');
  83. $this->assertInstanceOf(DocBlock::class, $docblock);
  84. $this->assertSame('This is a DocBlock', $docblock->getSummary());
  85. $this->assertEquals(new Description(''), $docblock->getDescription());
  86. $this->assertSame([], $docblock->getTags());
  87. $this->assertEquals(new Context(''), $docblock->getContext());
  88. $this->assertNull($docblock->getLocation());
  89. }
  90. /**
  91. * @covers ::__construct
  92. * @covers ::create
  93. * @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
  94. * @uses phpDocumentor\Reflection\DocBlock\Description
  95. * @dataProvider provideSummaryAndDescriptions
  96. */
  97. public function testSummaryAndDescriptionAreSeparated($given, $summary, $description)
  98. {
  99. $tagFactory = m::mock(TagFactory::class);
  100. $fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);
  101. $docblock = $fixture->create($given);
  102. $this->assertSame($summary, $docblock->getSummary());
  103. $this->assertEquals(new Description($description), $docblock->getDescription());
  104. }
  105. /**
  106. * @covers ::__construct
  107. * @covers ::create
  108. * @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
  109. * @uses phpDocumentor\Reflection\DocBlock\Description
  110. */
  111. public function testDescriptionsRetainFormatting()
  112. {
  113. $tagFactory = m::mock(TagFactory::class);
  114. $fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);
  115. $given = <<<DOCBLOCK
  116. /**
  117. * This is a summary.
  118. * This is a multiline Description
  119. * that contains a code block.
  120. *
  121. * See here: a CodeBlock
  122. */
  123. DOCBLOCK;
  124. $description = <<<DESCRIPTION
  125. This is a multiline Description
  126. that contains a code block.
  127. See here: a CodeBlock
  128. DESCRIPTION;
  129. $docblock = $fixture->create($given);
  130. $this->assertEquals(new Description($description), $docblock->getDescription());
  131. }
  132. /**
  133. * @covers ::__construct
  134. * @covers ::create
  135. * @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
  136. * @uses phpDocumentor\Reflection\DocBlock\Description
  137. */
  138. public function testTagsAreInterpretedUsingFactory()
  139. {
  140. $tagString = <<<TAG
  141. @author Mike van Riel <me@mikevanriel.com> This is with
  142. multiline description.
  143. TAG;
  144. $tag = m::mock(Tag::class);
  145. $tagFactory = m::mock(TagFactory::class);
  146. $tagFactory->shouldReceive('create')->with($tagString, m::type(Context::class))->andReturn($tag);
  147. $fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);
  148. $given = <<<DOCBLOCK
  149. /**
  150. * This is a summary.
  151. *
  152. * @author Mike van Riel <me@mikevanriel.com> This is with
  153. * multiline description.
  154. */
  155. DOCBLOCK;
  156. $docblock = $fixture->create($given, new Context(''));
  157. $this->assertEquals([$tag], $docblock->getTags());
  158. }
  159. public function provideSummaryAndDescriptions()
  160. {
  161. return [
  162. ['This is a DocBlock', 'This is a DocBlock', ''],
  163. [
  164. 'This is a DocBlock. This should still be summary.',
  165. 'This is a DocBlock. This should still be summary.',
  166. ''
  167. ],
  168. [
  169. <<<DOCBLOCK
  170. This is a DocBlock.
  171. This should be a Description.
  172. DOCBLOCK
  173. ,
  174. 'This is a DocBlock.',
  175. 'This should be a Description.'
  176. ],
  177. [
  178. <<<DOCBLOCK
  179. This is a
  180. multiline Summary.
  181. This should be a Description.
  182. DOCBLOCK
  183. ,
  184. "This is a\nmultiline Summary.",
  185. 'This should be a Description.'
  186. ],
  187. [
  188. <<<DOCBLOCK
  189. This is a Summary without dot but with a whiteline
  190. This should be a Description.
  191. DOCBLOCK
  192. ,
  193. 'This is a Summary without dot but with a whiteline',
  194. 'This should be a Description.'
  195. ],
  196. [
  197. <<<DOCBLOCK
  198. This is a Summary with dot and with a whiteline.
  199. This should be a Description.
  200. DOCBLOCK
  201. ,
  202. 'This is a Summary with dot and with a whiteline.',
  203. 'This should be a Description.'
  204. ],
  205. ];
  206. }
  207. /**
  208. * @covers ::__construct
  209. * @covers ::create
  210. *
  211. * @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
  212. * @uses phpDocumentor\Reflection\DocBlock\Description
  213. * @uses phpDocumentor\Reflection\Types\Context
  214. * @uses phpDocumentor\Reflection\DocBlock\Tags\Param
  215. */
  216. public function testTagsWithContextNamespace()
  217. {
  218. $tagFactoryMock = m::mock(TagFactory::class);
  219. $fixture = new DocBlockFactory(m::mock(DescriptionFactory::class), $tagFactoryMock);
  220. $context = new Context('MyNamespace');
  221. $tagFactoryMock->shouldReceive('create')->with(m::any(), $context)->andReturn(new Param('param'));
  222. $docblock = $fixture->create('/** @param MyType $param */', $context);
  223. }
  224. /**
  225. * @covers ::__construct
  226. * @covers ::create
  227. *
  228. * @uses phpDocumentor\Reflection\DocBlock\DescriptionFactory
  229. * @uses phpDocumentor\Reflection\DocBlock\Description
  230. */
  231. public function testTagsAreFilteredForNullValues()
  232. {
  233. $tagString = <<<TAG
  234. @author Mike van Riel <me@mikevanriel.com> This is with
  235. multiline description.
  236. TAG;
  237. $tagFactory = m::mock(TagFactory::class);
  238. $tagFactory->shouldReceive('create')->with($tagString, m::any())->andReturn(null);
  239. $fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);
  240. $given = <<<DOCBLOCK
  241. /**
  242. * This is a summary.
  243. *
  244. * @author Mike van Riel <me@mikevanriel.com> This is with
  245. * multiline description.
  246. */
  247. DOCBLOCK;
  248. $docblock = $fixture->create($given, new Context(''));
  249. $this->assertEquals([], $docblock->getTags());
  250. }
  251. }