|
| 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 | +} |
0 commit comments