forked from phpstan/phpdoc-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpDocToStringTest.php
450 lines (405 loc) · 14 KB
/
PhpDocToStringTest.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php declare(strict_types = 1);
namespace PHPStan\PhpDocParser\Ast\ToString;
use Generator;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagMethodValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagPropertyValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineAnnotation;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArgument;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArray;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineArrayItem;
use PHPStan\PhpDocParser\Ast\PhpDoc\Doctrine\DoctrineTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\InvalidTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueParameterNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireExtendsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\RequireImplementsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\SelfOutTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypelessParamTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\UsesTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\PhpDocParser\Printer\Printer;
use PHPUnit\Framework\TestCase;
class PhpDocToStringTest extends TestCase
{
/**
* @dataProvider provideFullPhpDocCases
*/
public function testFullPhpDocToString(string $expected, Node $node): void
{
$this->assertSame($expected, (string) $node);
}
/**
* @dataProvider provideFullPhpDocCases
*/
public function testFullPhpDocPrinter(string $expected, Node $node): void
{
$printer = new Printer();
$this->assertSame($expected, $printer->print($node));
}
/**
* @dataProvider provideOtherCases
* @dataProvider provideMethodCases
* @dataProvider provideClassCases
* @dataProvider provideAssertionCases
* @dataProvider provideDoctrineCases
*/
public function testTagValueNodeToString(string $expected, Node $node): void
{
$this->assertSame($expected, (string) $node);
}
/**
* @dataProvider provideOtherCases
* @dataProvider provideMethodCases
* @dataProvider provideClassCases
* @dataProvider provideAssertionCases
* @dataProvider provideDoctrineCases
*/
public function testTagValueNodePrinter(string $expected, Node $node): void
{
$printer = new Printer();
$this->assertSame($expected, $printer->print($node));
}
public static function provideFullPhpDocCases(): Generator
{
yield [
"/**\n *\n */",
new PhpDocNode([]),
];
yield [
"/**\n * It works\n */",
new PhpDocNode([
new PhpDocTextNode('It works'),
]),
];
yield [
"/**\n * It works\n *\n * with empty lines\n */",
new PhpDocNode([
new PhpDocTextNode('It works'),
new PhpDocTextNode(''),
new PhpDocTextNode('with empty lines'),
]),
];
yield [
"/**\n * Foo\n *\n * @deprecated Because of reasons.\n */",
new PhpDocNode([
new PhpDocTextNode('Foo'),
new PhpDocTextNode(''),
new PhpDocTagNode('@deprecated', new DeprecatedTagValueNode('Because of reasons.')),
]),
];
}
public static function provideOtherCases(): Generator
{
$string = new IdentifierTypeNode('string');
yield from [
['', new GenericTagValueNode('')],
['Foo bar', new GenericTagValueNode('Foo bar')],
];
yield [
'#desc',
new InvalidTagValueNode(
'#desc',
new ParserException('#desc', Lexer::TOKEN_OTHER, 11, Lexer::TOKEN_IDENTIFIER)
),
];
yield from [
['', new DeprecatedTagValueNode('')],
['Because of reasons.', new DeprecatedTagValueNode('Because of reasons.')],
];
yield from [
['string $foo', new VarTagValueNode($string, '$foo', '')],
['string $foo Description.', new VarTagValueNode($string, '$foo', 'Description.')],
];
$bar = new IdentifierTypeNode('Foo\\Bar');
$baz = new IdentifierTypeNode('Foo\\Baz');
yield from [
['TValue', new TemplateTagValueNode('TValue', null, '')],
['TValue of Foo\\Bar', new TemplateTagValueNode('TValue', $bar, '')],
['TValue super Foo\\Bar', new TemplateTagValueNode('TValue', null, '', null, $bar)],
['TValue = Foo\\Bar', new TemplateTagValueNode('TValue', null, '', $bar)],
['TValue of Foo\\Bar = Foo\\Baz', new TemplateTagValueNode('TValue', $bar, '', $baz)],
['TValue Description.', new TemplateTagValueNode('TValue', null, 'Description.')],
['TValue of Foo\\Bar = Foo\\Baz Description.', new TemplateTagValueNode('TValue', $bar, 'Description.', $baz)],
['TValue super Foo\\Bar = Foo\\Baz Description.', new TemplateTagValueNode('TValue', null, 'Description.', $baz, $bar)],
['TValue of Foo\\Bar super Foo\\Baz Description.', new TemplateTagValueNode('TValue', $bar, 'Description.', null, $baz)],
];
}
public static function provideMethodCases(): Generator
{
$string = new IdentifierTypeNode('string');
$foo = new IdentifierTypeNode('Foo\\Foo');
yield from [
['string $foo', new ParamOutTagValueNode($string, '$foo', '')],
['string $foo Description.', new ParamOutTagValueNode($string, '$foo', 'Description.')],
];
yield from [
['Foo\\Foo', new ReturnTagValueNode($foo, '')],
['string Description.', new ReturnTagValueNode($string, 'Description.')],
];
yield from [
['string', new SelfOutTagValueNode($string, '')],
['string Description.', new SelfOutTagValueNode($string, 'Description.')],
];
yield from [
['Foo\\Foo', new ThrowsTagValueNode($foo, '')],
['Foo\\Foo Description.', new ThrowsTagValueNode($foo, 'Description.')],
];
yield from [
['string $foo', new ParamTagValueNode($string, false, '$foo', '', false)],
['string &$foo', new ParamTagValueNode($string, false, '$foo', '', true)],
['string ...$foo', new ParamTagValueNode($string, true, '$foo', '', false)],
['string &...$foo', new ParamTagValueNode($string, true, '$foo', '', true)],
['string $foo Description.', new ParamTagValueNode($string, false, '$foo', 'Description.', false)],
['string &...$foo Description.', new ParamTagValueNode($string, true, '$foo', 'Description.', true)],
['$foo', new TypelessParamTagValueNode(false, '$foo', '', false)],
['&$foo', new TypelessParamTagValueNode(false, '$foo', '', true)],
['&...$foo', new TypelessParamTagValueNode(true, '$foo', '', true)],
['$foo Description.', new TypelessParamTagValueNode(false, '$foo', 'Description.', false)],
['&...$foo Description.', new TypelessParamTagValueNode(true, '$foo', 'Description.', true)],
];
}
public static function provideClassCases(): Generator
{
$string = new IdentifierTypeNode('string');
$bar = new IdentifierTypeNode('Foo\\Bar');
$arrayOfStrings = new GenericTypeNode(new IdentifierTypeNode('array'), [$string]);
yield from [
['PHPUnit\\TestCase', new MixinTagValueNode(new IdentifierTypeNode('PHPUnit\\TestCase'), '')],
['Foo\\Bar Baz', new MixinTagValueNode(new IdentifierTypeNode('Foo\\Bar'), 'Baz')],
];
yield from [
['PHPUnit\\TestCase', new RequireExtendsTagValueNode(new IdentifierTypeNode('PHPUnit\\TestCase'), '')],
['Foo\\Bar Baz', new RequireExtendsTagValueNode(new IdentifierTypeNode('Foo\\Bar'), 'Baz')],
];
yield from [
['PHPUnit\\TestCase', new RequireImplementsTagValueNode(new IdentifierTypeNode('PHPUnit\\TestCase'), '')],
['Foo\\Bar Baz', new RequireImplementsTagValueNode(new IdentifierTypeNode('Foo\\Bar'), 'Baz')],
];
yield from [
['Foo array<string>', new TypeAliasTagValueNode('Foo', $arrayOfStrings)],
['Test from Foo\Bar', new TypeAliasImportTagValueNode('Test', $bar, null)],
['Test from Foo\Bar as Foo', new TypeAliasImportTagValueNode('Test', $bar, 'Foo')],
];
yield from [
[
'array<string>',
new ExtendsTagValueNode($arrayOfStrings, ''),
],
[
'array<string> How did we manage to extend an array?',
new ExtendsTagValueNode($arrayOfStrings, 'How did we manage to extend an array?'),
],
[
'array<string>',
new ImplementsTagValueNode($arrayOfStrings, ''),
],
[
'array<string> How did we manage to implement an array?',
new ImplementsTagValueNode($arrayOfStrings, 'How did we manage to implement an array?'),
],
[
'array<string>',
new UsesTagValueNode($arrayOfStrings, ''),
],
[
'array<string> How did we manage to use an array?',
new UsesTagValueNode($arrayOfStrings, 'How did we manage to use an array?'),
],
];
yield from [
['string $foo', new PropertyTagValueNode($string, '$foo', '')],
['string $foo Description.', new PropertyTagValueNode($string, '$foo', 'Description.')],
];
yield from [
[
'foo',
new MethodTagValueParameterNode(null, false, false, 'foo', null),
],
[
'string foo',
new MethodTagValueParameterNode($string, false, false, 'foo', null),
],
[
'&foo',
new MethodTagValueParameterNode(null, true, false, 'foo', null),
],
[
'string &foo',
new MethodTagValueParameterNode($string, true, false, 'foo', null),
],
[
'string &foo = bar',
new MethodTagValueParameterNode($string, true, false, 'foo', new ConstExprStringNode('bar')),
],
[
'&...foo',
new MethodTagValueParameterNode(null, true, true, 'foo', null),
],
[
'string ...foo',
new MethodTagValueParameterNode($string, false, true, 'foo', null),
],
[
'string foo()',
new MethodTagValueNode(false, $string, 'foo', [], '', []),
],
[
'static string bar() Description',
new MethodTagValueNode(true, $string, 'bar', [], 'Description', []),
],
[
'baz(string &foo, string ...foo)',
new MethodTagValueNode(false, null, 'baz', [
new MethodTagValueParameterNode($string, true, false, 'foo', null),
new MethodTagValueParameterNode($string, false, true, 'foo', null),
], '', []),
],
];
}
public static function provideAssertionCases(): Generator
{
$string = new IdentifierTypeNode('string');
yield from [
[
'string $foo->bar() description',
new AssertTagMethodValueNode($string, '$foo', 'bar', false, 'description', false),
],
[
'=string $foo->bar()',
new AssertTagMethodValueNode($string, '$foo', 'bar', false, '', true),
],
[
'!string $foo->bar() foobar',
new AssertTagMethodValueNode($string, '$foo', 'bar', true, 'foobar', false),
],
[
'!=string $foo->bar()',
new AssertTagMethodValueNode($string, '$foo', 'bar', true, '', true),
],
[
'string $foo->bar description',
new AssertTagPropertyValueNode($string, '$foo', 'bar', false, 'description', false),
],
[
'=string $foo->bar',
new AssertTagPropertyValueNode($string, '$foo', 'bar', false, '', true),
],
[
'!string $foo->bar foobar',
new AssertTagPropertyValueNode($string, '$foo', 'bar', true, 'foobar', false),
],
[
'!=string $foo->bar',
new AssertTagPropertyValueNode($string, '$foo', 'bar', true, '', true),
],
[
'string $foo description',
new AssertTagValueNode($string, '$foo', false, 'description', false),
],
[
'=string $foo',
new AssertTagValueNode($string, '$foo', false, '', true),
],
[
'!string $foo foobar',
new AssertTagValueNode($string, '$foo', true, 'foobar', false),
],
[
'!=string $foo',
new AssertTagValueNode($string, '$foo', true, '', true),
],
];
yield from [
['string $foo', new ParamOutTagValueNode($string, '$foo', '')],
['string $foo Description.', new ParamOutTagValueNode($string, '$foo', 'Description.')],
];
}
/**
* @return iterable<array{string, Node}>
*/
public static function provideDoctrineCases(): iterable
{
yield [
'@ORM\Entity()',
new PhpDocTagNode('@ORM\Entity', new DoctrineTagValueNode(
new DoctrineAnnotation('@ORM\Entity', []),
''
)),
];
yield [
'@ORM\Entity() test',
new PhpDocTagNode('@ORM\Entity', new DoctrineTagValueNode(
new DoctrineAnnotation('@ORM\Entity', []),
'test'
)),
];
yield [
'@ORM\Entity(1, b=2)',
new DoctrineTagValueNode(
new DoctrineAnnotation('@ORM\Entity', [
new DoctrineArgument(null, new ConstExprIntegerNode('1')),
new DoctrineArgument(new IdentifierTypeNode('b'), new ConstExprIntegerNode('2')),
]),
''
),
];
yield [
'{}',
new DoctrineArray([]),
];
yield [
'{1, a=2}',
new DoctrineArray([
new DoctrineArrayItem(null, new ConstExprIntegerNode('1')),
new DoctrineArrayItem(new ConstExprStringNode('a'), new ConstExprIntegerNode('2')),
]),
];
yield [
'1',
new DoctrineArrayItem(null, new ConstExprIntegerNode('1')),
];
yield [
'a=2',
new DoctrineArrayItem(new ConstExprStringNode('a'), new ConstExprIntegerNode('2')),
];
yield [
'@ORM\Entity(1, b=2)',
new DoctrineAnnotation('@ORM\Entity', [
new DoctrineArgument(null, new ConstExprIntegerNode('1')),
new DoctrineArgument(new IdentifierTypeNode('b'), new ConstExprIntegerNode('2')),
]),
];
yield [
'1',
new DoctrineArgument(null, new ConstExprIntegerNode('1')),
];
yield [
'b=2',
new DoctrineArgument(new IdentifierTypeNode('b'), new ConstExprIntegerNode('2')),
];
}
}