Repository navigation
Expand file tree
/
Copy pathPhpParserTest.php
More file actions
145 lines (119 loc) · 5.44 KB
/
Copy pathPhpParserTest.php
File metadata and controls
145 lines (119 loc) · 5.44 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace HyperfTest\CodeParser;
use Hyperf\CodeParser\PhpParser;
use HyperfTest\CodeParser\Stub\Bar;
use HyperfTest\CodeParser\Stub\FooEnumStruct;
use HyperfTest\CodeParser\Stub\UnionTypeFoo;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter\Standard;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
/**
* @internal
* @coversNothing
*/
#[CoversNothing]
class PhpParserTest extends TestCase
{
public function testGetAstFromReflectionParameter()
{
$parserFactory = new ParserFactory();
$parser7 = $parserFactory->create(ParserFactory::ONLY_PHP7);
$stmts = $parser7->parse(file_get_contents(__DIR__ . '/Stub/Bar.php'));
/** @var ClassMethod $classMethod */
$classMethod = $stmts[1]->stmts[0]->stmts[0];
$name = $classMethod->getParams()[0];
$foo = $classMethod->getParams()[1];
$extra = $classMethod->getParams()[2];
$bar = new ReflectionClass(Bar::class);
$parameters = $bar->getMethod('__construct')->getParameters();
$parser = new PhpParser();
$this->assertNodeParam($name, $parser->getNodeFromReflectionParameter($parameters[0]));
$this->assertNodeParam($foo, $foo2 = $parser->getNodeFromReflectionParameter($parameters[1]));
$this->assertSame(['', 'HyperfTest', 'CodeParser', 'Stub', 'Foo'], $foo2->type->parts);
$this->assertNodeParam($extra, $parser->getNodeFromReflectionParameter($parameters[2]));
$stmts = $parser7->parse(file_get_contents(__DIR__ . '/Stub/UnionTypeFoo.php'));
/** @var ClassMethod $classMethod */
$classMethod = $stmts[1]->stmts[0]->stmts[0];
$name = $classMethod->getParams()[0];
$foo = new ReflectionClass(UnionTypeFoo::class);
$parameters = $foo->getMethod('__construct')->getParameters();
$this->assertNodeParam($name, $parser->getNodeFromReflectionParameter($parameters[0]));
}
public function testGetExprFromEnum()
{
$parser = new PhpParser();
$printer = new Standard();
$bar = new ReflectionClass(FooEnumStruct::class);
$parameters = $bar->getMethod('__construct')->getParameters();
$stmts = $parser->getNodeFromReflectionParameter($parameters[0]);
$code = $printer->prettyPrint([$stmts]);
$this->assertSame('\HyperfTest\CodeParser\Stub\FooEnum $enum = \HyperfTest\CodeParser\Stub\FooEnum::DEFAULT', $code);
$parameters = $bar->getMethod('stdClass')->getParameters();
$stmts = $parser->getNodeFromReflectionParameter($parameters[0]);
$code = $printer->prettyPrint([$stmts]);
$this->assertSame('object $id = new \stdClass()', $code);
$parameters = $bar->getMethod('class')->getParameters();
$stmts = $parser->getNodeFromReflectionParameter($parameters[0]);
$code = $printer->prettyPrint([$stmts]);
$this->assertSame('\HyperfTest\Di\Stub\Ignore $ignore = new \HyperfTest\Di\Stub\Ignore()', $code);
}
public function testGetExprFromArray()
{
$parser = new PhpParser();
$printer = new Standard();
$stmts = $parser->getExprFromValue([]);
$this->assertInstanceOf(Node\Expr\Array_::class, $stmts);
$this->assertSame([], $stmts->items);
$res = $printer->prettyPrint([$stmts]);
$this->assertSame('[]', $res);
$stmts = $parser->getExprFromValue([1, 2, 3]);
$res = $printer->prettyPrint([$stmts]);
$this->assertSame('[1, 2, 3]', $res);
$stmts = $parser->getExprFromValue(['a' => 1, 2, 3]);
$res = $printer->prettyPrint([$stmts]);
$this->assertSame("['a' => 1, 0 => 2, 1 => 3]", $res);
$stmts = $parser->getExprFromValue(['a' => 1, 'b' => 2]);
$res = $printer->prettyPrint([$stmts]);
$this->assertSame("['a' => 1, 'b' => 2]", $res);
}
protected function assertNodeParam(Node\Param $param, Node\Param $param2)
{
if ($param->type) {
$this->assertSame(get_class($param->type), get_class($param2->type));
if ($param->type instanceof Node\Name) {
$this->assertSame(get_class($param->type), get_class($param2->type));
} elseif ($param->type instanceof Node\Identifier) {
$this->assertSame($param->type->name, $param2->type->name);
}
} else {
$this->assertSame($param->type, $param2->type);
}
$this->assertSame($param2->byRef, $param->byRef);
$this->assertSame($param->variadic, $param2->variadic);
$this->assertSame($param->var->name, $param2->var->name);
$this->assertSame(get_class($param->var), get_class($param2->var));
if ($param->default) {
$this->assertSame(get_class($param->default), get_class($param2->default));
if ($param->default instanceof Node\Expr\Array_) {
$this->assertSame($param->default->items, $param2->default->items);
} elseif ($param->default instanceof Node\Scalar) {
$this->assertSame($param->default->value, $param2->default->value);
}
} else {
$this->assertSame($param->default, $param2->default);
}
}
}