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