Skip to content

Commit f3f8ca9

Browse files
author
Kirill Nesmeyanov
committed
Add phpdoc unit tests
1 parent 6f7beba commit f3f8ca9

6 files changed

Lines changed: 257 additions & 6 deletions

File tree

tests/TestCase.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
use PHPUnit\Framework\TestCase as BaseTestCase;
88

9-
abstract class TestCase extends BaseTestCase
10-
{
11-
}
9+
abstract class TestCase extends BaseTestCase {}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\Tests\Unit\Extractor;
6+
7+
use PHPUnit\Framework\Attributes\Before;
8+
use TypeLang\PhpDocParser\DocBlock\Extractor\TagNameExtractor;
9+
use TypeLang\PhpDocParser\Exception\TagWithoutNameException;
10+
11+
final class TagNameExtractorTest extends TestCase
12+
{
13+
private TagNameExtractor $extractor;
14+
15+
#[Before]
16+
public function setUpTagNameExtractor(): void
17+
{
18+
$this->extractor = new TagNameExtractor();
19+
}
20+
21+
public function testExtraction(): void
22+
{
23+
[$name, $description] = $this->extractor->extract('@name descr');
24+
25+
self::assertSame('name', $name);
26+
self::assertSame('descr', $description);
27+
}
28+
29+
public function testByNonNamespaceDelimiter(): void
30+
{
31+
[$name, $description] = $this->extractor->extract('@phpcs:on test');
32+
33+
self::assertSame('phpcs:on', $name);
34+
self::assertSame('test', $description);
35+
}
36+
37+
public function testWithoutDescription(): void
38+
{
39+
[$name, $description] = $this->extractor->extract('@inheritDoc');
40+
41+
self::assertSame('inheritDoc', $name);
42+
self::assertNull($description);
43+
}
44+
45+
public function testWithoutName(): void
46+
{
47+
self::expectException(TagWithoutNameException::class);
48+
self::expectExceptionCode(TagWithoutNameException::CODE_NO_NAME);
49+
50+
$this->extractor->extract('@ Description');
51+
}
52+
53+
public function testWithoutNameAndDescription(): void
54+
{
55+
self::expectException(TagWithoutNameException::class);
56+
self::expectExceptionCode(TagWithoutNameException::CODE_NO_NAME);
57+
58+
$this->extractor->extract('@');
59+
}
60+
61+
public function testEmptyTag(): void
62+
{
63+
self::expectException(TagWithoutNameException::class);
64+
self::expectExceptionCode(TagWithoutNameException::CODE_EMPTY);
65+
66+
$this->extractor->extract('');
67+
}
68+
69+
public function testWithoutTag(): void
70+
{
71+
self::expectException(TagWithoutNameException::class);
72+
self::expectExceptionCode(TagWithoutNameException::CODE_NON_TAGGED);
73+
74+
$this->extractor->extract('description');
75+
}
76+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\Tests\Unit\Extractor;
6+
7+
use PHPUnit\Framework\Attributes\Before;
8+
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
9+
use TypeLang\Parser\Parser;
10+
use TypeLang\PhpDocParser\DocBlock\Extractor\TagTypeExtractor;
11+
use TypeLang\PhpDocParser\Exception\InvalidTagTypeException;
12+
13+
final class TagTypeExtractorTest extends TestCase
14+
{
15+
private TagTypeExtractor $extractor;
16+
17+
#[Before]
18+
public function setUpTagNameExtractor(): void
19+
{
20+
$this->extractor = new TagTypeExtractor(
21+
parser: new Parser(true),
22+
);
23+
}
24+
25+
public function testExtraction(): void
26+
{
27+
[$type, $description] = $this->extractor->extractTypeOrFail('type description');
28+
29+
self::assertInstanceOf(NamedTypeNode::class, $type);
30+
self::assertSame('description', $description);
31+
}
32+
33+
public function testWithoutDescription(): void
34+
{
35+
[$type, $description] = $this->extractor->extractTypeOrFail('type');
36+
37+
self::assertInstanceOf(NamedTypeNode::class, $type);
38+
self::assertSame('type', $type->name->toString());
39+
40+
self::assertNull($description);
41+
}
42+
43+
public function testWithoutType(): void
44+
{
45+
self::expectException(InvalidTagTypeException::class);
46+
self::expectExceptionCode(InvalidTagTypeException::CODE_WITHOUT_NAME);
47+
48+
$this->extractor->extractTypeOrFail(':test');
49+
}
50+
51+
public function testWithoutTypeAllowMixed(): void
52+
{
53+
[$type, $description] = $this->extractor->extractTypeOrMixed(':test');
54+
55+
self::assertInstanceOf(NamedTypeNode::class, $type);
56+
self::assertSame('mixed', $type->name->toString());
57+
58+
self::assertSame(':test', $description);
59+
}
60+
61+
public function testWithoutValue(): void
62+
{
63+
self::expectException(InvalidTagTypeException::class);
64+
self::expectExceptionCode(InvalidTagTypeException::CODE_WITHOUT_NAME);
65+
66+
$this->extractor->extractTypeOrFail('');
67+
}
68+
69+
public function testWithoutValueAllowMixed(): void
70+
{
71+
[$type, $description] = $this->extractor->extractTypeOrMixed('');
72+
73+
self::assertInstanceOf(NamedTypeNode::class, $type);
74+
self::assertSame('mixed', $type->name->toString());
75+
76+
self::assertNull($description);
77+
}
78+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\Tests\Unit\Extractor;
6+
7+
use PHPUnit\Framework\Attributes\Before;
8+
use TypeLang\PhpDocParser\DocBlock\Extractor\TagVariableExtractor;
9+
use TypeLang\PhpDocParser\Exception\InvalidTagVariableNameException;
10+
11+
final class TagVariableExtractorTest extends TestCase
12+
{
13+
private TagVariableExtractor $extractor;
14+
15+
#[Before]
16+
public function setUpTagNameExtractor(): void
17+
{
18+
$this->extractor = new TagVariableExtractor();
19+
}
20+
21+
public function testExtraction(): void
22+
{
23+
[$name, $description] = $this->extractor->extractOrFail('$name description');
24+
25+
self::assertSame('$name', $name);
26+
self::assertSame('description', $description);
27+
}
28+
29+
public function testWithoutDescription(): void
30+
{
31+
[$name, $description] = $this->extractor->extractOrFail('$name');
32+
33+
self::assertSame('$name', $name);
34+
self::assertNull($description);
35+
}
36+
37+
public function testWithInvalidName(): void
38+
{
39+
self::expectException(InvalidTagVariableNameException::class);
40+
self::expectExceptionCode(InvalidTagVariableNameException::CODE_WITHOUT_TYPE);
41+
42+
$this->extractor->extractOrFail('$0_name');
43+
}
44+
45+
public function testWithInvalidNameAllowNull(): void
46+
{
47+
[$var, $description] = $this->extractor->extractOrNull('$0_name');
48+
49+
self::assertNull($var);
50+
self::assertSame('$0_name', $description);
51+
}
52+
53+
public function testWithoutVariable(): void
54+
{
55+
self::expectException(InvalidTagVariableNameException::class);
56+
self::expectExceptionCode(InvalidTagVariableNameException::CODE_WITHOUT_TYPE);
57+
58+
$this->extractor->extractOrFail('test');
59+
}
60+
61+
public function testWithoutVariableAllowNull(): void
62+
{
63+
[$var, $description] = $this->extractor->extractOrNull('test');
64+
65+
self::assertNull($var);
66+
self::assertSame('test', $description);
67+
}
68+
69+
public function testWithEmptyValue(): void
70+
{
71+
self::expectException(InvalidTagVariableNameException::class);
72+
self::expectExceptionCode(InvalidTagVariableNameException::CODE_WITHOUT_TYPE);
73+
74+
$this->extractor->extractOrFail('');
75+
}
76+
77+
public function testWithEmptyValueAllowNull(): void
78+
{
79+
[$var, $description] = $this->extractor->extractOrNull('');
80+
81+
self::assertNull($var);
82+
self::assertNull($description);
83+
}
84+
85+
public function testVariadic(): void
86+
{
87+
[$var, $description] = $this->extractor->extractOrFail('...$name description');
88+
89+
self::assertSame('$name', $var);
90+
self::assertSame('description', $description);
91+
}
92+
}

tests/Unit/Extractor/TestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\PhpDocParser\Tests\Unit\Extractor;
6+
7+
use TypeLang\PhpDocParser\Tests\Unit\TestCase as BaseTestCase;
8+
9+
abstract class TestCase extends BaseTestCase {}

tests/Unit/TestCase.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
use TypeLang\PhpDocParser\Tests\TestCase as BaseTestCase;
88

9-
abstract class TestCase extends BaseTestCase
10-
{
11-
}
9+
abstract class TestCase extends BaseTestCase {}

0 commit comments

Comments
 (0)