Skip to content

Commit d3c20c4

Browse files
committed
Add more tag tests
1 parent 99298b7 commit d3c20c4

3 files changed

Lines changed: 139 additions & 3 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDoc\Tests\DocBlock\Tag;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use TypeLang\PhpDoc\DocBlock\Grammar\AuthorNameGrammarRule;
9+
use TypeLang\PhpDoc\DocBlock\Grammar\EmailGrammarRule;
10+
use TypeLang\PhpDoc\DocBlock\Tag\AuthorTag\AuthorTag;
11+
use TypeLang\PhpDoc\DocBlock\Tag\AuthorTag\AuthorTagDefinition;
12+
use TypeLang\PhpDoc\DocBlockParser;
13+
use TypeLang\PhpDoc\TagFactory;
14+
use TypeLang\PhpDoc\Tests\TestCase;
15+
16+
final class AuthorTagTest extends TestCase
17+
{
18+
#[Test]
19+
public function parsesNameAndEmail(): void
20+
{
21+
$tag = self::factory()->create('author', 'John Doe <john@example.com>');
22+
23+
self::assertInstanceOf(AuthorTag::class, $tag);
24+
self::assertSame('author', $tag->name);
25+
self::assertSame('John Doe', $tag->author);
26+
self::assertSame('john@example.com', $tag->email);
27+
self::assertSame('@author John Doe <john@example.com>', (string) $tag);
28+
}
29+
30+
#[Test]
31+
public function parsesNameWithoutEmail(): void
32+
{
33+
$tag = self::factory()->create('author', 'Jane Roe');
34+
35+
self::assertInstanceOf(AuthorTag::class, $tag);
36+
self::assertSame('Jane Roe', $tag->author);
37+
self::assertNull($tag->email);
38+
self::assertSame('@author Jane Roe', (string) $tag);
39+
}
40+
41+
#[Test]
42+
public function resolvesThroughTheRealParser(): void
43+
{
44+
$block = new DocBlockParser()->parse('/** @author Kirill <k@example.com> */');
45+
46+
self::assertCount(1, $block->tags);
47+
self::assertInstanceOf(AuthorTag::class, $block->tags[0]);
48+
self::assertSame('k@example.com', $block->tags[0]->email);
49+
}
50+
51+
private static function factory(): TagFactory
52+
{
53+
return new TagFactory(
54+
definitions: [
55+
AuthorTagDefinition::NAME => new AuthorTagDefinition(),
56+
],
57+
rules: [
58+
AuthorNameGrammarRule::NAME => new AuthorNameGrammarRule(),
59+
EmailGrammarRule::NAME => new EmailGrammarRule(),
60+
],
61+
);
62+
}
63+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDoc\Tests\DocBlock\Tag;
6+
7+
use PHPUnit\Framework\Attributes\Test;
8+
use TypeLang\PhpDoc\DocBlock\Grammar\DescriptionGrammarRule;
9+
use TypeLang\PhpDoc\DocBlock\Grammar\ReferenceGrammarRule;
10+
use TypeLang\PhpDoc\DocBlock\Reference\ClassMethodReference;
11+
use TypeLang\PhpDoc\DocBlock\Tag\InvalidTag;
12+
use TypeLang\PhpDoc\DocBlock\Tag\ReferencedTagInterface;
13+
use TypeLang\PhpDoc\DocBlock\Tag\UsedByTag\UsedByTag;
14+
use TypeLang\PhpDoc\DocBlock\Tag\UsedByTag\UsedByTagDefinition;
15+
use TypeLang\PhpDoc\DocBlock\Tag\UsesTag\UsesTag;
16+
use TypeLang\PhpDoc\DocBlock\Tag\UsesTag\UsesTagDefinition;
17+
use TypeLang\PhpDoc\DocBlockParser;
18+
use TypeLang\PhpDoc\Exception\MalformedTagException;
19+
use TypeLang\PhpDoc\TagFactory;
20+
use TypeLang\PhpDoc\Tests\TestCase;
21+
22+
final class ReferenceTagTest extends TestCase
23+
{
24+
#[Test]
25+
public function parsesCodeReferenceWithDescription(): void
26+
{
27+
$tag = self::factory()->create('uses', 'Mailer::send() The underlying transport.');
28+
29+
self::assertInstanceOf(UsesTag::class, $tag);
30+
self::assertInstanceOf(ReferencedTagInterface::class, $tag);
31+
self::assertSame('uses', $tag->name);
32+
self::assertInstanceOf(ClassMethodReference::class, $tag->reference);
33+
self::assertSame('The underlying transport.', (string) $tag->description);
34+
self::assertSame('@uses Mailer::send() The underlying transport.', (string) $tag);
35+
}
36+
37+
#[Test]
38+
public function usedByResolvesThroughTheRealParser(): void
39+
{
40+
$block = new DocBlockParser()->parse('/** @used-by Service::run() */');
41+
42+
self::assertCount(1, $block->tags);
43+
self::assertInstanceOf(UsedByTag::class, $block->tags[0]);
44+
self::assertSame('used-by', $block->tags[0]->name);
45+
}
46+
47+
/**
48+
* Unlike "@see", the "@uses" tag references a code element only, so a URI
49+
* is not a valid target.
50+
*/
51+
#[Test]
52+
public function uriIsNotAccepted(): void
53+
{
54+
$tag = self::factory()->create('uses', 'https://example.com');
55+
56+
self::assertInstanceOf(InvalidTag::class, $tag);
57+
self::assertInstanceOf(MalformedTagException::class, $tag->reason);
58+
}
59+
60+
private static function factory(): TagFactory
61+
{
62+
return new TagFactory(
63+
definitions: [
64+
UsesTagDefinition::NAME => new UsesTagDefinition(),
65+
UsedByTagDefinition::NAME => new UsedByTagDefinition(),
66+
],
67+
rules: [
68+
ReferenceGrammarRule::NAME => new ReferenceGrammarRule(),
69+
DescriptionGrammarRule::NAME => new DescriptionGrammarRule(self::createDescriptionParser()),
70+
],
71+
);
72+
}
73+
}

libs/phpdoc/tests/DocBlock/Tag/TypedTagTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
use TypeLang\Parser\TypeParser;
1010
use TypeLang\PhpDoc\DocBlock\Grammar\DescriptionGrammarRule;
1111
use TypeLang\PhpDoc\DocBlock\Grammar\TypeGrammarRule;
12+
use TypeLang\PhpDoc\DocBlock\Reference\TypeReference;
1213
use TypeLang\PhpDoc\DocBlock\Tag\InheritanceTag\ExtendsTag;
1314
use TypeLang\PhpDoc\DocBlock\Tag\InheritanceTag\ExtendsTagDefinition;
1415
use TypeLang\PhpDoc\DocBlock\Tag\InheritanceTag\InheritanceTag;
16+
use TypeLang\PhpDoc\DocBlock\Tag\InvalidTag;
1517
use TypeLang\PhpDoc\DocBlock\Tag\MixinTag\MixinTag;
1618
use TypeLang\PhpDoc\DocBlock\Tag\MixinTag\MixinTagDefinition;
1719
use TypeLang\PhpDoc\DocBlock\Tag\ReturnTag\ReturnTag;
1820
use TypeLang\PhpDoc\DocBlock\Tag\ReturnTag\ReturnTagDefinition;
1921
use TypeLang\PhpDoc\DocBlock\Tag\ThrowsTag\ThrowsTag;
20-
use TypeLang\PhpDoc\DocBlock\Tag\InvalidTag;
2122
use TypeLang\PhpDoc\DocBlock\Tag\ThrowsTag\ThrowsTagDefinition;
2223
use TypeLang\PhpDoc\DocBlock\Tag\TypedTagInterface;
23-
use TypeLang\PhpDoc\DocBlock\Type\TypeStatement;
2424
use TypeLang\PhpDoc\Exception\MalformedTagException;
2525
use TypeLang\PhpDoc\Parser\Grammar\MatchedResult;
2626
use TypeLang\PhpDoc\TagFactory;
@@ -82,7 +82,7 @@ public function missingRequiredTypeProducesInvalidTag(): void
8282
#[Test]
8383
public function nameIsIntrinsicToTheTag(): void
8484
{
85-
$statement = new TypeStatement(new TypeParser()->parse('bool'), 'bool');
85+
$statement = new TypeReference(new TypeParser()->parse('bool'), 'bool');
8686

8787
$tag = new ReturnTagDefinition()
8888
->create('whatever-was-written', new MatchedResult(['type' => [$statement]]));

0 commit comments

Comments
 (0)