mirror of
https://github.com/barryvdh/ReflectionDocBlock.git
synced 2026-08-18 01:57:13 +00:00
add more unit tests and normalize the "__toString" methods v2
This commit is contained in:
@@ -49,7 +49,7 @@ final class Covers extends BaseTag implements Factory\StaticMethod
|
||||
?FqsenResolver $resolver = null,
|
||||
?TypeContext $context = null
|
||||
) : self {
|
||||
Assert::notEmpty($body);
|
||||
Assert::stringNotEmpty($body);
|
||||
Assert::notNull($descriptionFactory);
|
||||
Assert::notNull($resolver);
|
||||
|
||||
|
||||
@@ -45,9 +45,14 @@ final class Example implements Tag, Factory\StaticMethod
|
||||
/** @var string|null */
|
||||
private $content;
|
||||
|
||||
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content)
|
||||
{
|
||||
Assert::notEmpty($filePath);
|
||||
public function __construct(
|
||||
string $filePath,
|
||||
bool $isURI,
|
||||
int $startingLine,
|
||||
int $lineCount,
|
||||
?string $content
|
||||
) {
|
||||
Assert::stringNotEmpty($filePath);
|
||||
Assert::greaterThanEq($startingLine, 1);
|
||||
Assert::greaterThanEq($lineCount, 0);
|
||||
|
||||
@@ -64,7 +69,7 @@ final class Example implements Tag, Factory\StaticMethod
|
||||
public function getContent() : string
|
||||
{
|
||||
if ($this->content === null || $this->content === '') {
|
||||
$filePath = '"' . $this->filePath . '"';
|
||||
$filePath = $this->filePath;
|
||||
if ($this->isURI) {
|
||||
$filePath = $this->isUriRelative($this->filePath)
|
||||
? str_replace('%2F', '/', rawurlencode($this->filePath))
|
||||
@@ -85,7 +90,7 @@ final class Example implements Tag, Factory\StaticMethod
|
||||
public static function create(string $body) : ?Tag
|
||||
{
|
||||
// File component: File path in quotes or File URI / Source information
|
||||
if (!preg_match('/^(?:\"([^\"]+)\"|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) {
|
||||
if (!preg_match('/^\s*(?:(\"[^\"]+\")|(\S+))(?:\s+(.*))?$/sux', $body, $matches)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -134,7 +139,7 @@ final class Example implements Tag, Factory\StaticMethod
|
||||
*/
|
||||
public function getFilePath() : string
|
||||
{
|
||||
return $this->filePath;
|
||||
return trim($this->filePath, '"');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,9 +148,15 @@ final class Example implements Tag, Factory\StaticMethod
|
||||
public function __toString() : string
|
||||
{
|
||||
$filePath = (string) $this->filePath;
|
||||
$isDefaultLine = $this->startingLine === 1 && $this->lineCount === 0;
|
||||
$startingLine = !$isDefaultLine ? (string) $this->startingLine : '';
|
||||
$lineCount = !$isDefaultLine ? (string) $this->lineCount : '';
|
||||
$content = (string) $this->content;
|
||||
|
||||
return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : '');
|
||||
return $filePath
|
||||
. ($startingLine !== '' ? ($filePath !== '' ? ' ' : '') . $startingLine : '')
|
||||
. ($lineCount !== '' ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount : '')
|
||||
. ($content !== '' ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content : '');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -93,6 +93,52 @@ class ExampleTest extends TestCase
|
||||
$this->assertEquals(5, $tag->getLineCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::create
|
||||
* @covers ::__toString
|
||||
*/
|
||||
public function testStringRepresentationIsReturned() : void
|
||||
{
|
||||
$tag = Example::create('"example1.php" 10 5 test text');
|
||||
|
||||
$this->assertSame('"example1.php" 10 5 test text', (string) $tag);
|
||||
|
||||
// ---
|
||||
|
||||
$tag = Example::create('file://example1.php');
|
||||
|
||||
$this->assertSame('file://example1.php', (string) $tag);
|
||||
|
||||
// ---
|
||||
|
||||
$tag = Example::create('0 foo bar');
|
||||
|
||||
$this->assertSame('0 foo bar', (string) $tag);
|
||||
|
||||
// ---
|
||||
|
||||
$tag = Example::create('$redisCluster->pttl(\'key\');');
|
||||
|
||||
$this->assertSame('$redisCluster->pttl(\'key\');', (string) $tag);
|
||||
|
||||
// ---
|
||||
|
||||
$tag = Example::create(' "example1.php" 10 5 test text ');
|
||||
|
||||
$this->assertSame('"example1.php" 10 5 test text', (string) $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::create
|
||||
* @covers ::__toString
|
||||
*/
|
||||
public function testStringRepresentationIsReturnedWithoutDescription() : void
|
||||
{
|
||||
$tag = Example::create('');
|
||||
|
||||
$this->assertSame('', (string) $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user