forked from marc-mabe/php-enum-phpstan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumMethodReflectionTest.php
159 lines (124 loc) · 6.12 KB
/
EnumMethodReflectionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php declare(strict_types=1);
namespace MabeEnum\PHPStan\tests\unit;
use MabeEnumPHPStan\EnumMethodsClassReflectionExtension;
use MabeEnum\PHPStan\tests\assets\DeprecatedEnum;
use MabeEnum\PHPStan\tests\assets\DocCommentEnum;
use MabeEnum\PHPStan\tests\assets\VisibilityEnum;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\VerbosityLevel;
use PHPStan\Analyser\Scope;
class EnumMethodReflectionTest extends PHPStanTestCase
{
protected ReflectionProvider $reflectionProvider;
protected EnumMethodsClassReflectionExtension $reflectionExtension;
public function setUp(): void
{
$this->reflectionProvider = $this->createReflectionProvider();
$this->reflectionExtension = new EnumMethodsClassReflectionExtension();
}
public function testGetName(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertSame('STR', $methodReflection->getName());
}
public function testGetDeclaringClass(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertSame($classReflection, $methodReflection->getDeclaringClass());
}
public function testShouldBeStatic(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertTrue($methodReflection->isStatic());
}
public function testShouldNotBePrivate(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertFalse($methodReflection->isPrivate());
}
public function testShouldBePublic(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertTrue($methodReflection->isPublic());
}
public function testGetPrototype(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertSame($methodReflection, $methodReflection->getPrototype());
}
public function testGetVariants(): void
{
$classReflection = $this->reflectionProvider->getClass(VisibilityEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$scope = $this->createMock(Scope::class);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
[],
$methodReflection->getVariants()
);
$this->assertSame(
VisibilityEnum::class,
$parametersAcceptor->getReturnType()->describe(VerbosityLevel::value())
);
}
public function testGetDocComment(): void
{
$classReflection = $this->reflectionProvider->getClass(DocCommentEnum::class);
$docMethodRefl = $this->reflectionExtension->getMethod($classReflection, 'WITH_DOC_BLOCK');
$noDocMethodRefl = $this->reflectionExtension->getMethod($classReflection, 'WITHOUT_DOC_BLOCK');
// return null on no doc block
$this->assertSame(null, $noDocMethodRefl->getDocComment());
// return the correct doc block
$this->assertMatchesRegularExpression('/With doc block/', $docMethodRefl->getDocComment());
// remove @var declaration
$this->assertDoesNotMatchRegularExpression('/@var/', $docMethodRefl->getDocComment());
}
public function testIsDeprecated(): void
{
$classReflection = $this->reflectionProvider->getClass(DeprecatedEnum::class);
$deprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'DEPRECATED');
$notDeprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'NOT_DEPRECATED');
$this->assertTrue($deprecatedRefl->isDeprecated()->yes());
$this->assertTrue($notDeprecatedRefl->isDeprecated()->no());
}
public function testGetDeprecatedDescription(): void
{
$classReflection = $this->reflectionProvider->getClass(DeprecatedEnum::class);
$deprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'DEPRECATED');
$notDeprecatedRefl = $this->reflectionExtension->getMethod($classReflection, 'NOT_DEPRECATED');
$this->assertSame('Test deprecated reflection', $deprecatedRefl->getDeprecatedDescription());
$this->assertNull($notDeprecatedRefl->getDeprecatedDescription());
}
public function testIsFinal(): void
{
$classReflection = $this->reflectionProvider->getClass(DeprecatedEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertTrue($methodReflection->isFinal()->no());
}
public function testIsInternal(): void
{
$classReflection = $this->reflectionProvider->getClass(DeprecatedEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertTrue($methodReflection->isInternal()->no());
}
public function testGetThrowType(): void
{
$classReflection = $this->reflectionProvider->getClass(DeprecatedEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertNull($methodReflection->getThrowType());
}
public function testHasSideEffects(): void
{
$classReflection = $this->reflectionProvider->getClass(DeprecatedEnum::class);
$methodReflection = $this->reflectionExtension->getMethod($classReflection, 'STR');
$this->assertTrue($methodReflection->hasSideEffects()->no());
}
}