Skip to content

Commit 6f58148

Browse files
committed
Add simple description parser tests
1 parent 9c124a7 commit 6f58148

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed

Diff for: tests/Unit/DescriptionParserTest.php

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PHPDoc\Tests\Unit;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use TypeLang\PHPDoc\Parser\Description\DescriptionParserInterface;
9+
use TypeLang\PHPDoc\Parser\Description\SprintfDescriptionParser;
10+
use TypeLang\PHPDoc\Parser\Tag\RegexTagParser;
11+
use TypeLang\PHPDoc\Tag\Content;
12+
use TypeLang\PHPDoc\Tag\Description;
13+
use TypeLang\PHPDoc\Tag\Factory\FactoryInterface;
14+
use TypeLang\PHPDoc\Tag\Factory\TagFactory;
15+
use TypeLang\PHPDoc\Tag\InvalidTag;
16+
use TypeLang\PHPDoc\Tag\Tag;
17+
use TypeLang\PHPDoc\Tag\TagInterface;
18+
19+
final class DescriptionParserTest extends TestCase
20+
{
21+
public static function parserDataProvider(): iterable
22+
{
23+
$tags = new TagFactory([
24+
'error' => new class implements FactoryInterface {
25+
public function create(string $name, Content $content, DescriptionParserInterface $descriptions): TagInterface
26+
{
27+
throw new \LogicException('Error tag ' . $name);
28+
}
29+
},
30+
]);
31+
32+
yield SprintfDescriptionParser::class => [new SprintfDescriptionParser(
33+
tags: new RegexTagParser($tags),
34+
)];
35+
}
36+
37+
#[DataProvider('parserDataProvider')]
38+
public function testSimpleDescription(DescriptionParserInterface $parser): void
39+
{
40+
self::assertEquals(
41+
expected: new Description('Hello World!'),
42+
actual: $parser->parse('Hello World!'),
43+
);
44+
}
45+
46+
#[DataProvider('parserDataProvider')]
47+
public function testDescriptionWithInlineTag(DescriptionParserInterface $parser): void
48+
{
49+
self::assertEquals(
50+
expected: new Description('Hello {%1$s} World!', [
51+
new Tag('tag'),
52+
]),
53+
actual: $parser->parse('Hello {@tag} World!'),
54+
);
55+
}
56+
57+
#[DataProvider('parserDataProvider')]
58+
public function testDescriptionWithDescribedInlineTag(DescriptionParserInterface $parser): void
59+
{
60+
self::assertEquals(
61+
expected: new Description('Hello {%1$s} World!', [
62+
new Tag('tag', 'description'),
63+
]),
64+
actual: $parser->parse('Hello {@tag description} World!'),
65+
);
66+
}
67+
68+
#[DataProvider('parserDataProvider')]
69+
public function testDescriptionWithMultipleInlineTags(DescriptionParserInterface $parser): void
70+
{
71+
self::assertEquals(
72+
expected: new Description('Hello {%1$s} {%2$s} World!', [
73+
new Tag('tag1'),
74+
new Tag('tag2', '#desc'),
75+
]),
76+
actual: $parser->parse('Hello {@tag1} {@tag2#desc} World!'),
77+
);
78+
}
79+
80+
#[DataProvider('parserDataProvider')]
81+
public function testDescriptionWithSprintfSyntax(DescriptionParserInterface $parser): void
82+
{
83+
self::assertEquals(
84+
expected: new Description('Hello %%s World!'),
85+
actual: $parser->parse('Hello %s World!'),
86+
);
87+
}
88+
89+
#[DataProvider('parserDataProvider')]
90+
public function testDescriptionWithSprintfSyntaxInsideInlineTag(DescriptionParserInterface $parser): void
91+
{
92+
self::assertEquals(
93+
expected: new Description('Hello {%1$s} World!', [
94+
new Tag('test-some', 'Desc %%s 42')
95+
]),
96+
actual: $parser->parse('Hello {@test-some Desc %s 42} World!'),
97+
);
98+
}
99+
100+
#[DataProvider('parserDataProvider')]
101+
public function testDescriptionWithNonNamedTag(DescriptionParserInterface $parser): void
102+
{
103+
self::assertEquals(
104+
expected: new Description('Hello {@} World!'),
105+
actual: $parser->parse('Hello {@} World!'),
106+
);
107+
}
108+
109+
#[DataProvider('parserDataProvider')]
110+
public function testDescriptionWithBadTagName(DescriptionParserInterface $parser): void
111+
{
112+
$description = $parser->parse('Hello {@@} World!');
113+
114+
self::assertCount(1, $description);
115+
self::assertInstanceOf(InvalidTag::class, $description[0]);
116+
117+
$reason = $description[0]->getReason();
118+
119+
self::assertSame('Tag name cannot be empty', $reason->getMessage());
120+
self::assertSame(InvalidTag::DEFAULT_UNKNOWN_TAG_NAME, $description[0]->getName());
121+
self::assertEquals(new Description('{@}'), $description[0]->getDescription());
122+
}
123+
124+
#[DataProvider('parserDataProvider')]
125+
public function testErrorWhileParsingInline(DescriptionParserInterface $parser): void
126+
{
127+
$description = $parser->parse('Hello {@error description} World!');
128+
129+
self::assertCount(1, $description);
130+
self::assertInstanceOf(InvalidTag::class, $description[0]);
131+
132+
$reason = $description[0]->getReason();
133+
134+
self::assertSame('Error while parsing tag @error', $reason->getMessage());
135+
self::assertSame('error', $description[0]->getName());
136+
self::assertEquals(new Description('description'), $description[0]->getDescription());
137+
}
138+
}

0 commit comments

Comments
 (0)