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
|
<?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 Mockery as m;
use phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use PHPUnit\Framework\TestCase;
/**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Description
*/
class DescriptionTest extends TestCase
{
/**
* Call Mockery::close after each test.
*/
public function tearDown(): void
{
m::close();
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
*
* @covers ::__construct
* @covers ::render
*/
public function testDescriptionCanRenderUsingABodyWithPlaceholdersAndTags(): void
{
$body = 'This is a %1$s body.';
$expected = 'This is a {@internal significant} body.';
$tags = [new Generic('internal', new Description('significant '))];
$fixture = new Description($body, $tags);
// without formatter (thus the PassthroughFormatter by default)
$this->assertSame($expected, $fixture->render());
// with a custom formatter
$formatter = m::mock(PassthroughFormatter::class);
$formatter->shouldReceive('format')->with($tags[0])->andReturn('@internal significant');
$this->assertSame($expected, $fixture->render($formatter));
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
*
* @covers ::__construct
* @covers ::render
* @covers ::__toString
*/
public function testDescriptionCanBeCastToString(): void
{
$body = 'This is a %1$s body.';
$expected = 'This is a {@internal significant} body.';
$tags = [new Generic('internal', new Description('significant '))];
$fixture = new Description($body, $tags);
$this->assertSame($expected, (string) $fixture);
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
*
* @covers ::getTags
*/
public function testDescriptionTagsGetter(): void
{
$body = '@JoinTable(name="table", joinColumns=%1$s, inverseJoinColumns=%2$s)';
$tag1 = new Generic('JoinColumn', new Description('(name="column_id", referencedColumnName="id")'));
$tag2 = new Generic('JoinColumn', new Description('(name="column_id_2", referencedColumnName="id")'));
$tags = [
$tag1,
$tag2,
];
$fixture = new Description($body, $tags);
$this->assertCount(2, $fixture->getTags());
$actualTags = $fixture->getTags();
$this->assertSame($tags, $actualTags);
$this->assertSame($tag1, $actualTags[0]);
$this->assertSame($tag2, $actualTags[1]);
}
/**
* @covers ::getBodyTemplate
*/
public function testDescriptionBodyTemplateGetter(): void
{
$body = 'See https://github.com/phpDocumentor/ReflectionDocBlock/pull/171 for more information';
$fixture = new Description($body, []);
$this->assertSame($body, $fixture->getBodyTemplate());
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Generic
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
*
* @covers ::__construct
* @covers ::render
* @covers ::__toString
*/
public function testDescriptionMultipleTagsCanBeCastToString(): void
{
$body = '@JoinTable(name="table", joinColumns=%1$s, inverseJoinColumns=%2$s)';
$tag1 = new Generic('JoinColumn', new Description('(name="column_id", referencedColumnName="id")'));
$tag2 = new Generic('JoinColumn', new Description('(name="column_id_2", referencedColumnName="id")'));
$tags = [
$tag1,
$tag2,
];
$fixture = new Description($body, $tags);
$expected = '@JoinTable(name="table", joinColumns={@JoinColumn (name="column_id", referencedColumnName="id")}, '
. 'inverseJoinColumns={@JoinColumn (name="column_id_2", referencedColumnName="id")})';
$this->assertSame($expected, (string) $fixture);
}
/**
* @uses \phpDocumentor\Reflection\DocBlock\Tags\Formatter\PassthroughFormatter
*
* @covers ::__construct
* @covers ::render
* @covers ::__toString
*/
public function testDescriptionWithEscapedCharactersAndNoTagsCanBeCastToString(): void
{
//% chars are escaped in \phpDocumentor\Reflection\DocBlock\DescriptionFactory::create
$body = <<<'EOT'
{%% for user in users %%}
{{ user.name }}
{%% endfor %%}';
EOT;
$expected = <<<'EOT'
{% for user in users %}
{{ user.name }}
{% endfor %}';
EOT;
$fixture = new Description($body, []);
$this->assertSame($expected, (string) $fixture);
}
}
|