add more unit tests and normalize the "__toString" methods v2

This commit is contained in:
Lars Moelleken
2020-09-03 02:06:24 +02:00
parent 7b7c22ddc3
commit 4e6d7ecd03
3 changed files with 65 additions and 8 deletions
+1 -1
View File
@@ -49,7 +49,7 @@ final class Covers extends BaseTag implements Factory\StaticMethod
?FqsenResolver $resolver = null, ?FqsenResolver $resolver = null,
?TypeContext $context = null ?TypeContext $context = null
) : self { ) : self {
Assert::notEmpty($body); Assert::stringNotEmpty($body);
Assert::notNull($descriptionFactory); Assert::notNull($descriptionFactory);
Assert::notNull($resolver); Assert::notNull($resolver);
+18 -7
View File
@@ -45,9 +45,14 @@ final class Example implements Tag, Factory\StaticMethod
/** @var string|null */ /** @var string|null */
private $content; private $content;
public function __construct(string $filePath, bool $isURI, int $startingLine, int $lineCount, ?string $content) public function __construct(
{ string $filePath,
Assert::notEmpty($filePath); bool $isURI,
int $startingLine,
int $lineCount,
?string $content
) {
Assert::stringNotEmpty($filePath);
Assert::greaterThanEq($startingLine, 1); Assert::greaterThanEq($startingLine, 1);
Assert::greaterThanEq($lineCount, 0); Assert::greaterThanEq($lineCount, 0);
@@ -64,7 +69,7 @@ final class Example implements Tag, Factory\StaticMethod
public function getContent() : string public function getContent() : string
{ {
if ($this->content === null || $this->content === '') { if ($this->content === null || $this->content === '') {
$filePath = '"' . $this->filePath . '"'; $filePath = $this->filePath;
if ($this->isURI) { if ($this->isURI) {
$filePath = $this->isUriRelative($this->filePath) $filePath = $this->isUriRelative($this->filePath)
? str_replace('%2F', '/', rawurlencode($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 public static function create(string $body) : ?Tag
{ {
// File component: File path in quotes or File URI / Source information // 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; return null;
} }
@@ -134,7 +139,7 @@ final class Example implements Tag, Factory\StaticMethod
*/ */
public function getFilePath() : string 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 public function __toString() : string
{ {
$filePath = (string) $this->filePath; $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; $content = (string) $this->content;
return $filePath . ($content !== '' ? ($filePath !== '' ? ' ' : '') . $content : ''); return $filePath
. ($startingLine !== '' ? ($filePath !== '' ? ' ' : '') . $startingLine : '')
. ($lineCount !== '' ? ($filePath !== '' || $startingLine !== '' ? ' ' : '') . $lineCount : '')
. ($content !== '' ? ($filePath !== '' || $startingLine !== '' || $lineCount !== '' ? ' ' : '') . $content : '');
} }
/** /**
+46
View File
@@ -93,6 +93,52 @@ class ExampleTest extends TestCase
$this->assertEquals(5, $tag->getLineCount()); $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 * @uses \phpDocumentor\Reflection\DocBlock\Tags\BaseTag
* *