|
| 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\Combinator\DescriptionCombinator; |
| 9 | +use TypeLang\PhpDoc\DocBlock\Combinator\IntegerCombinator; |
| 10 | +use TypeLang\PhpDoc\DocBlock\Combinator\UriCombinator; |
| 11 | +use TypeLang\PhpDoc\DocBlock\Combinator\UrlCombinator; |
| 12 | +use TypeLang\PhpDoc\DocBlock\Tag\ExampleTag\ExampleTag; |
| 13 | +use TypeLang\PhpDoc\DocBlock\Tag\ExampleTag\ExampleTagDefinition; |
| 14 | +use TypeLang\PhpDoc\DocBlock\Tag\InvalidTag; |
| 15 | +use TypeLang\PhpDoc\DocBlockParser; |
| 16 | +use TypeLang\PhpDoc\TagFactory; |
| 17 | +use TypeLang\PhpDoc\Tests\TestCase; |
| 18 | + |
| 19 | +final class ExampleTagTest extends TestCase |
| 20 | +{ |
| 21 | + #[Test] |
| 22 | + public function parsesLocationWithStartCountAndDescription(): void |
| 23 | + { |
| 24 | + $tag = self::factory()->create('example', 'https://example.com/demo.php 12 30 A relevant excerpt.'); |
| 25 | + |
| 26 | + self::assertInstanceOf(ExampleTag::class, $tag); |
| 27 | + self::assertNotNull($tag->location); |
| 28 | + self::assertSame('https://example.com/demo.php', (string) $tag->location); |
| 29 | + self::assertSame(12, $tag->start); |
| 30 | + self::assertSame(30, $tag->count); |
| 31 | + self::assertSame('A relevant excerpt.', (string) $tag->description); |
| 32 | + self::assertSame('@example https://example.com/demo.php 12 30 A relevant excerpt.', (string) $tag); |
| 33 | + } |
| 34 | + |
| 35 | + #[Test] |
| 36 | + public function parsesLocationWithStartOnly(): void |
| 37 | + { |
| 38 | + $tag = self::factory()->create('example', 'demo.php 7'); |
| 39 | + |
| 40 | + self::assertInstanceOf(ExampleTag::class, $tag); |
| 41 | + self::assertNotNull($tag->location); |
| 42 | + self::assertSame('demo.php', (string) $tag->location); |
| 43 | + self::assertSame(7, $tag->start); |
| 44 | + self::assertNull($tag->count); |
| 45 | + self::assertNull($tag->description); |
| 46 | + self::assertSame('@example demo.php 7', (string) $tag); |
| 47 | + } |
| 48 | + |
| 49 | + #[Test] |
| 50 | + public function parsesLocationOnly(): void |
| 51 | + { |
| 52 | + $tag = self::factory()->create('example', 'demo.php'); |
| 53 | + |
| 54 | + self::assertInstanceOf(ExampleTag::class, $tag); |
| 55 | + self::assertNotNull($tag->location); |
| 56 | + self::assertSame('demo.php', (string) $tag->location); |
| 57 | + self::assertNull($tag->start); |
| 58 | + self::assertNull($tag->count); |
| 59 | + self::assertNull($tag->description); |
| 60 | + self::assertSame('@example demo.php', (string) $tag); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Any ordinary word is a valid relative URI, so a leading word is captured |
| 65 | + * as the location and the remainder becomes the description. |
| 66 | + */ |
| 67 | + #[Test] |
| 68 | + public function treatsLeadingWordAsLocation(): void |
| 69 | + { |
| 70 | + $tag = self::factory()->create('example', 'demo.php the bundled snippet below.'); |
| 71 | + |
| 72 | + self::assertInstanceOf(ExampleTag::class, $tag); |
| 73 | + self::assertNotNull($tag->location); |
| 74 | + self::assertSame('demo.php', (string) $tag->location); |
| 75 | + self::assertSame('the bundled snippet below.', (string) $tag->description); |
| 76 | + self::assertSame('@example demo.php the bundled snippet below.', (string) $tag); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * A location is mandatory, so an input whose first word cannot be a URI is |
| 81 | + * malformed. |
| 82 | + */ |
| 83 | + #[Test] |
| 84 | + public function rejectsMissingLocation(): void |
| 85 | + { |
| 86 | + $tag = self::factory()->create('example', '// inline example'); |
| 87 | + |
| 88 | + self::assertInstanceOf(InvalidTag::class, $tag); |
| 89 | + } |
| 90 | + |
| 91 | + #[Test] |
| 92 | + public function resolvesThroughTheRealParser(): void |
| 93 | + { |
| 94 | + $block = new DocBlockParser()->parse('/** @example demo.php 3 5 */'); |
| 95 | + |
| 96 | + self::assertInstanceOf(ExampleTag::class, $block->tags[0]); |
| 97 | + self::assertSame('demo.php', (string) $block->tags[0]->location); |
| 98 | + self::assertSame(3, $block->tags[0]->start); |
| 99 | + self::assertSame(5, $block->tags[0]->count); |
| 100 | + } |
| 101 | + |
| 102 | + private static function factory(): TagFactory |
| 103 | + { |
| 104 | + return new TagFactory( |
| 105 | + definitions: [ |
| 106 | + ExampleTagDefinition::NAME => new ExampleTagDefinition(), |
| 107 | + ], |
| 108 | + combinators: [ |
| 109 | + UrlCombinator::NAME => new UrlCombinator(), |
| 110 | + UriCombinator::NAME => new UriCombinator(), |
| 111 | + IntegerCombinator::NAME => new IntegerCombinator(), |
| 112 | + DescriptionCombinator::NAME => new DescriptionCombinator(self::createDescriptionParser()), |
| 113 | + ], |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments