|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Doctrine\DocBlockReader; |
|
8
|
|
|
use PHPUnit\Framework\Attributes\DataProvider; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use ReflectionMethod; |
|
11
|
|
|
use ReflectionParameter; |
|
12
|
|
|
|
|
13
|
|
|
class DocBlockReaderTest extends TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private const EMPTY_COMMENT = ' |
|
16
|
|
|
/** |
|
17
|
|
|
*/'; |
|
18
|
|
|
|
|
19
|
|
|
private const COMMENT = ' |
|
20
|
|
|
/** |
|
21
|
|
|
* Get interesting data |
|
22
|
|
|
* |
|
23
|
|
|
* Long description for the method |
|
24
|
|
|
* spanning lines |
|
25
|
|
|
* |
|
26
|
|
|
* @param null|string $foo Some param description |
|
27
|
|
|
* @param User $bar |
|
28
|
|
|
* @param \DateTimeImmutable $bazAbsolute |
|
29
|
|
|
* @param DateTimeImmutable $bazRelative |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/'; |
|
32
|
|
|
|
|
33
|
|
|
private const COMMENT_GENERIC = ' |
|
34
|
|
|
/** |
|
35
|
|
|
* @return Collection<int, Foo> |
|
36
|
|
|
*/'; |
|
37
|
|
|
|
|
38
|
|
|
#[DataProvider('providerGetMethodDescription')] |
|
39
|
|
|
public function testGetMethodDescription(string|false $comment, ?string $expected): void |
|
40
|
|
|
{ |
|
41
|
|
|
$reader = $this->create($comment); |
|
42
|
|
|
$actual = $reader->getMethodDescription(); |
|
43
|
|
|
self::assertSame($expected, $actual); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public static function providerGetMethodDescription(): iterable |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
[false, null], |
|
50
|
|
|
[self::EMPTY_COMMENT, null], |
|
51
|
|
|
[self::COMMENT, 'Interesting data |
|
52
|
|
|
|
|
53
|
|
|
Long description for the method |
|
54
|
|
|
spanning lines'], |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
#[DataProvider('providerGetParameterDescription')] |
|
59
|
|
|
public function testGetParameterDescription(string|false $comment, string $parameterName, ?string $expected): void |
|
60
|
|
|
{ |
|
61
|
|
|
$reader = $this->create($comment); |
|
62
|
|
|
$parameter = $this->createParameter($parameterName); |
|
63
|
|
|
$actual = $reader->getParameterDescription($parameter); |
|
64
|
|
|
self::assertSame($expected, $actual); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public static function providerGetParameterDescription(): iterable |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
[false, 'foo', null], |
|
71
|
|
|
[self::EMPTY_COMMENT, 'foo', null], |
|
72
|
|
|
[self::COMMENT, 'foo', 'Some param description'], |
|
73
|
|
|
'non-existing param' => [self::COMMENT, 'fo', null], |
|
74
|
|
|
[self::COMMENT, 'bar', null], |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
#[DataProvider('providerGetParameterType')] |
|
79
|
|
|
public function testGetParameterType(string|false $comment, string $parameterName, ?string $expected): void |
|
80
|
|
|
{ |
|
81
|
|
|
$reader = $this->create($comment); |
|
82
|
|
|
$parameter = $this->createParameter($parameterName); |
|
83
|
|
|
$actual = $reader->getParameterType($parameter); |
|
84
|
|
|
self::assertSame($expected, $actual); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public static function providerGetParameterType(): iterable |
|
88
|
|
|
{ |
|
89
|
|
|
return [ |
|
90
|
|
|
[false, 'foo', null], |
|
91
|
|
|
[self::EMPTY_COMMENT, 'foo', null], |
|
92
|
|
|
[self::COMMENT, 'foo', 'null|string'], |
|
93
|
|
|
'non-existing param' => [self::COMMENT, 'fo', null], |
|
94
|
|
|
[self::COMMENT, 'bar', 'User'], |
|
95
|
|
|
'do not make assumption on absolute types' => [self::COMMENT, 'bazAbsolute', '\DateTimeImmutable'], |
|
96
|
|
|
'do not make assumption on relative types' => [self::COMMENT, 'bazRelative', 'DateTimeImmutable'], |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
#[DataProvider('providerGetReturnType')] |
|
101
|
|
|
public function testGetReturnType(string|false $comment, ?string $expected): void |
|
102
|
|
|
{ |
|
103
|
|
|
$reader = $this->create($comment); |
|
104
|
|
|
$actual = $reader->getReturnType(); |
|
105
|
|
|
self::assertSame($expected, $actual); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public static function providerGetReturnType(): iterable |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
[false, null], |
|
112
|
|
|
[self::EMPTY_COMMENT, null], |
|
113
|
|
|
[self::COMMENT, 'bool'], |
|
114
|
|
|
[self::COMMENT_GENERIC, 'Collection<int, Foo>'], |
|
115
|
|
|
]; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function create(string|false $comment): DocBlockReader |
|
119
|
|
|
{ |
|
120
|
|
|
$method = new class($comment) extends ReflectionMethod { |
|
121
|
|
|
public function __construct( |
|
122
|
|
|
private readonly string|false $comment, |
|
123
|
|
|
) {} |
|
124
|
|
|
|
|
125
|
|
|
public function getDocComment(): string|false |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->comment; |
|
128
|
|
|
} |
|
129
|
|
|
}; |
|
130
|
|
|
|
|
131
|
|
|
return new DocBlockReader($method); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function createParameter(string $name): ReflectionParameter |
|
135
|
|
|
{ |
|
136
|
|
|
return new class($name) extends ReflectionParameter { |
|
137
|
|
|
public function __construct( |
|
138
|
|
|
public readonly string $mockedName, |
|
139
|
|
|
) {} |
|
140
|
|
|
|
|
141
|
|
|
public function getName(): string |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->mockedName; |
|
144
|
|
|
} |
|
145
|
|
|
}; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|