DescriptionFactoryTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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\DocBlock;
  13. use Mockery as m;
  14. use phpDocumentor\Reflection\DocBlock\Tags\Link;
  15. use phpDocumentor\Reflection\Types\Context;
  16. /**
  17. * @coversDefaultClass \phpDocumentor\Reflection\DocBlock\DescriptionFactory
  18. * @covers ::<private>
  19. */
  20. class DescriptionFactoryTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @covers ::__construct
  24. * @covers ::create
  25. * @uses phpDocumentor\Reflection\DocBlock\Description
  26. * @dataProvider provideSimpleExampleDescriptions
  27. */
  28. public function testDescriptionCanParseASimpleString($contents)
  29. {
  30. $tagFactory = m::mock(TagFactory::class);
  31. $tagFactory->shouldReceive('create')->never();
  32. $factory = new DescriptionFactory($tagFactory);
  33. $description = $factory->create($contents, new Context(''));
  34. $this->assertSame($contents, $description->render());
  35. }
  36. /**
  37. * @covers ::__construct
  38. * @covers ::create
  39. * @uses phpDocumentor\Reflection\DocBlock\Description
  40. * @dataProvider provideEscapeSequences
  41. */
  42. public function testEscapeSequences($contents, $expected)
  43. {
  44. $tagFactory = m::mock(TagFactory::class);
  45. $tagFactory->shouldReceive('create')->never();
  46. $factory = new DescriptionFactory($tagFactory);
  47. $description = $factory->create($contents, new Context(''));
  48. $this->assertSame($expected, $description->render());
  49. }
  50. /**
  51. * @covers ::__construct
  52. * @covers ::create
  53. * @uses phpDocumentor\Reflection\DocBlock\Description
  54. * @uses phpDocumentor\Reflection\DocBlock\Tags\Link
  55. * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
  56. * @uses phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
  57. * @uses phpDocumentor\Reflection\Types\Context
  58. */
  59. public function testDescriptionCanParseAStringWithInlineTag()
  60. {
  61. $contents = 'This is text for a {@link http://phpdoc.org/ description} that uses an inline tag.';
  62. $context = new Context('');
  63. $tagFactory = m::mock(TagFactory::class);
  64. $tagFactory->shouldReceive('create')
  65. ->once()
  66. ->with('@link http://phpdoc.org/ description', $context)
  67. ->andReturn(new Link('http://phpdoc.org/', new Description('description')))
  68. ;
  69. $factory = new DescriptionFactory($tagFactory);
  70. $description = $factory->create($contents, $context);
  71. $this->assertSame($contents, $description->render());
  72. }
  73. /**
  74. * @covers ::__construct
  75. * @covers ::create
  76. * @uses phpDocumentor\Reflection\DocBlock\Description
  77. * @uses phpDocumentor\Reflection\DocBlock\Tags\Link
  78. * @uses phpDocumentor\Reflection\DocBlock\Tags\BaseTag
  79. * @uses phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
  80. * @uses phpDocumentor\Reflection\Types\Context
  81. */
  82. public function testDescriptionCanParseAStringStartingWithInlineTag()
  83. {
  84. $contents = '{@link http://phpdoc.org/ This} is text for a description that starts with an inline tag.';
  85. $context = new Context('');
  86. $tagFactory = m::mock(TagFactory::class);
  87. $tagFactory->shouldReceive('create')
  88. ->once()
  89. ->with('@link http://phpdoc.org/ This', $context)
  90. ->andReturn(new Link('http://phpdoc.org/', new Description('This')))
  91. ;
  92. $factory = new DescriptionFactory($tagFactory);
  93. $description = $factory->create($contents, $context);
  94. $this->assertSame($contents, $description->render());
  95. }
  96. /**
  97. * @covers ::__construct
  98. * @covers ::create
  99. * @uses phpDocumentor\Reflection\DocBlock\Description
  100. */
  101. public function testIfSuperfluousStartingSpacesAreRemoved()
  102. {
  103. $factory = new DescriptionFactory(m::mock(TagFactory::class));
  104. $descriptionText = <<<DESCRIPTION
  105. This is a multiline
  106. description that you commonly
  107. see with tags.
  108. It does have a multiline code sample
  109. that should align, no matter what
  110. All spaces superfluous spaces on the
  111. second and later lines should be
  112. removed but the code sample should
  113. still be indented.
  114. DESCRIPTION;
  115. $expectedDescription = <<<DESCRIPTION
  116. This is a multiline
  117. description that you commonly
  118. see with tags.
  119. It does have a multiline code sample
  120. that should align, no matter what
  121. All spaces superfluous spaces on the
  122. second and later lines should be
  123. removed but the code sample should
  124. still be indented.
  125. DESCRIPTION;
  126. $description = $factory->create($descriptionText, new Context(''));
  127. $this->assertSame($expectedDescription, $description->render());
  128. }
  129. /**
  130. * Provides a series of example strings that the parser should correctly interpret and return.
  131. *
  132. * @return string[][]
  133. */
  134. public function provideSimpleExampleDescriptions()
  135. {
  136. return [
  137. ['This is text for a description.'],
  138. ['This is text for a description containing { that is literal.'],
  139. ['This is text for a description containing } that is literal.'],
  140. ['This is text for a description with {just a text} that is not a tag.'],
  141. ];
  142. }
  143. public function provideEscapeSequences()
  144. {
  145. return [
  146. ['This is text for a description with a {@}.', 'This is text for a description with a @.'],
  147. ['This is text for a description with a {}.', 'This is text for a description with a }.'],
  148. ];
  149. }
  150. }