1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
<?php
declare(strict_types=1);
/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/
namespace phpDocumentor\Reflection\DocBlock;
use Exception;
use Mockery as m;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use phpDocumentor\Reflection\DocBlock\Tags\Link as LinkTag;
use phpDocumentor\Reflection\Types\Context;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use function str_replace;
use const PHP_EOL;
/**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @covers ::<private>
*/
class DescriptionFactoryTest extends TestCase
{
/**
* Call Mockery::close after each test.
*/
public function tearDown(): void
{
m::close();
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
*
* @covers ::__construct
* @covers ::create
*/
#[DataProvider('provideSimpleExampleDescriptions')]
public function testDescriptionCanParseASimpleString(string $contents): void
{
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')->never();
$factory = new DescriptionFactory($tagFactory);
$description = $factory->create($contents, new Context(''));
$this->assertSame($contents, $description->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
*
* @covers ::__construct
* @covers ::create
*/
#[DataProvider('provideEscapeSequences')]
public function testEscapeSequences(string $contents, string $expected): void
{
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')->never();
$factory = new DescriptionFactory($tagFactory);
$description = $factory->create($contents, new Context(''));
$this->assertSame($expected, $description->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Link
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::__construct
* @covers ::create
*/
public function testDescriptionCanParseAStringWithInlineTag(): void
{
$contents = 'This is text for a {@link http://phpdoc.org/ description} that uses an inline tag.';
$context = new Context('');
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')
->once()
->with('@link http://phpdoc.org/ description', $context)
->andReturn(new LinkTag('http://phpdoc.org/', new Description('description')));
$factory = new DescriptionFactory($tagFactory);
$description = $factory->create($contents, $context);
$this->assertSame($contents, $description->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Link
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::__construct
* @covers ::create
*/
public function testDescriptionCanParseAStringStartingWithInlineTag(): void
{
$contents = '{@link http://phpdoc.org/ This} is text for a description that starts with an inline tag.';
$context = new Context('');
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')
->once()
->with('@link http://phpdoc.org/ This', $context)
->andReturn(new LinkTag('http://phpdoc.org/', new Description('This')));
$factory = new DescriptionFactory($tagFactory);
$description = $factory->create($contents, $context);
$this->assertSame($contents, $description->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Link
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::__construct
* @covers ::create
*/
public function testDescriptionCanParseAStringContainingMultipleTags(): void
{
$contents = 'This description has a {@link http://phpdoc.org/ This} another {@link http://phpdoc.org/ This2}';
$context = new Context('');
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')
->twice()
->andReturnValues(
[
new LinkTag('http://phpdoc.org/', new Description('This')),
new LinkTag('http://phpdoc.org/', new Description('This2')),
]
);
$factory = new DescriptionFactory($tagFactory);
$description = $factory->create($contents, $context);
$this->assertSame($contents, $description->render());
$this->assertSame('This description has a %1$s another %2$s', $description->getBodyTemplate());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
*
* @covers ::__construct
* @covers ::create
*/
public function testIfSuperfluousStartingSpacesAreRemoved(): void
{
$factory = new DescriptionFactory(m::mock(TagFactory::class));
$descriptionText = <<<DESCRIPTION
This is a multiline
description that you commonly
see with tags.
It does have a multiline code sample
that should align, no matter what
All spaces superfluous spaces on the
second and later lines should be
removed but the code sample should
still be indented.
DESCRIPTION;
$expectedDescription = <<<DESCRIPTION
This is a multiline
description that you commonly
see with tags.
It does have a multiline code sample
that should align, no matter what
All spaces superfluous spaces on the
second and later lines should be
removed but the code sample should
still be indented.
DESCRIPTION;
$description = $factory->create($descriptionText, new Context(''));
$this->assertSame(str_replace(PHP_EOL, "\n", $expectedDescription), $description->render());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Description
* @uses \phpDocumentor\Reflection\DocBlock\Tags\InvalidTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
* @uses \phpDocumentor\Reflection\Types\Context
*
* @covers ::__construct
* @covers ::create
*/
public function testDescriptionWithBrokenInlineTags(): void
{
$contents = 'This {@see $name} is a broken use case, but used in real life.';
$context = new Context('');
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')
->once()
->with('@see $name', $context)
->andReturn(InvalidTag::create('$name', 'see', new Exception()));
$factory = new DescriptionFactory($tagFactory);
$description = $factory->create($contents, $context);
$this->assertSame($contents, $description->render());
}
/**
* Provides a series of example strings that the parser should correctly interpret and return.
*
* @return string[][]
*/
public static function provideSimpleExampleDescriptions(): array
{
return [
['This is text for a description.'],
['This is text for a description containing { that is literal.'],
['This is text for a description containing } that is literal.'],
['This is text for a description with {just a text} that is not a tag.'],
];
}
/**
* @return string[][]
*/
public static function provideEscapeSequences(): array
{
return [
['This is text for a description with a {@}.', 'This is text for a description with a @.'],
['This is text for a description with a {}.', 'This is text for a description with a }.'],
];
}
}
|