Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser\Builder;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\Node;
|
Chris@0
|
6 use PhpParser\Node\Expr;
|
Chris@0
|
7 use PhpParser\Node\Scalar;
|
Chris@0
|
8
|
Chris@17
|
9 class ParamTest extends \PHPUnit\Framework\TestCase
|
Chris@0
|
10 {
|
Chris@0
|
11 public function createParamBuilder($name) {
|
Chris@0
|
12 return new Param($name);
|
Chris@0
|
13 }
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * @dataProvider provideTestDefaultValues
|
Chris@0
|
17 */
|
Chris@0
|
18 public function testDefaultValues($value, $expectedValueNode) {
|
Chris@0
|
19 $node = $this->createParamBuilder('test')
|
Chris@0
|
20 ->setDefault($value)
|
Chris@0
|
21 ->getNode()
|
Chris@0
|
22 ;
|
Chris@0
|
23
|
Chris@0
|
24 $this->assertEquals($expectedValueNode, $node->default);
|
Chris@0
|
25 }
|
Chris@0
|
26
|
Chris@0
|
27 public function provideTestDefaultValues() {
|
Chris@13
|
28 return [
|
Chris@13
|
29 [
|
Chris@0
|
30 null,
|
Chris@0
|
31 new Expr\ConstFetch(new Node\Name('null'))
|
Chris@13
|
32 ],
|
Chris@13
|
33 [
|
Chris@0
|
34 true,
|
Chris@0
|
35 new Expr\ConstFetch(new Node\Name('true'))
|
Chris@13
|
36 ],
|
Chris@13
|
37 [
|
Chris@0
|
38 false,
|
Chris@0
|
39 new Expr\ConstFetch(new Node\Name('false'))
|
Chris@13
|
40 ],
|
Chris@13
|
41 [
|
Chris@0
|
42 31415,
|
Chris@0
|
43 new Scalar\LNumber(31415)
|
Chris@13
|
44 ],
|
Chris@13
|
45 [
|
Chris@0
|
46 3.1415,
|
Chris@0
|
47 new Scalar\DNumber(3.1415)
|
Chris@13
|
48 ],
|
Chris@13
|
49 [
|
Chris@0
|
50 'Hallo World',
|
Chris@0
|
51 new Scalar\String_('Hallo World')
|
Chris@13
|
52 ],
|
Chris@13
|
53 [
|
Chris@13
|
54 [1, 2, 3],
|
Chris@13
|
55 new Expr\Array_([
|
Chris@0
|
56 new Expr\ArrayItem(new Scalar\LNumber(1)),
|
Chris@0
|
57 new Expr\ArrayItem(new Scalar\LNumber(2)),
|
Chris@0
|
58 new Expr\ArrayItem(new Scalar\LNumber(3)),
|
Chris@13
|
59 ])
|
Chris@13
|
60 ],
|
Chris@13
|
61 [
|
Chris@13
|
62 ['foo' => 'bar', 'bar' => 'foo'],
|
Chris@13
|
63 new Expr\Array_([
|
Chris@0
|
64 new Expr\ArrayItem(
|
Chris@0
|
65 new Scalar\String_('bar'),
|
Chris@0
|
66 new Scalar\String_('foo')
|
Chris@0
|
67 ),
|
Chris@0
|
68 new Expr\ArrayItem(
|
Chris@0
|
69 new Scalar\String_('foo'),
|
Chris@0
|
70 new Scalar\String_('bar')
|
Chris@0
|
71 ),
|
Chris@13
|
72 ])
|
Chris@13
|
73 ],
|
Chris@13
|
74 [
|
Chris@0
|
75 new Scalar\MagicConst\Dir,
|
Chris@0
|
76 new Scalar\MagicConst\Dir
|
Chris@13
|
77 ]
|
Chris@13
|
78 ];
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@17
|
82 * @dataProvider provideTestTypes
|
Chris@0
|
83 */
|
Chris@17
|
84 public function testTypes($typeHint, $expectedType) {
|
Chris@0
|
85 $node = $this->createParamBuilder('test')
|
Chris@0
|
86 ->setTypeHint($typeHint)
|
Chris@0
|
87 ->getNode()
|
Chris@0
|
88 ;
|
Chris@0
|
89 $type = $node->type;
|
Chris@0
|
90
|
Chris@0
|
91 /* Manually implement comparison to avoid __toString stupidity */
|
Chris@0
|
92 if ($expectedType instanceof Node\NullableType) {
|
Chris@0
|
93 $this->assertInstanceOf(get_class($expectedType), $type);
|
Chris@0
|
94 $expectedType = $expectedType->type;
|
Chris@0
|
95 $type = $type->type;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@13
|
98 $this->assertInstanceOf(get_class($expectedType), $type);
|
Chris@13
|
99 $this->assertEquals($expectedType, $type);
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@17
|
102 public function provideTestTypes() {
|
Chris@13
|
103 return [
|
Chris@13
|
104 ['array', new Node\Identifier('array')],
|
Chris@13
|
105 ['callable', new Node\Identifier('callable')],
|
Chris@13
|
106 ['bool', new Node\Identifier('bool')],
|
Chris@13
|
107 ['int', new Node\Identifier('int')],
|
Chris@13
|
108 ['float', new Node\Identifier('float')],
|
Chris@13
|
109 ['string', new Node\Identifier('string')],
|
Chris@13
|
110 ['iterable', new Node\Identifier('iterable')],
|
Chris@13
|
111 ['object', new Node\Identifier('object')],
|
Chris@13
|
112 ['Array', new Node\Identifier('array')],
|
Chris@13
|
113 ['CALLABLE', new Node\Identifier('callable')],
|
Chris@13
|
114 ['Some\Class', new Node\Name('Some\Class')],
|
Chris@13
|
115 ['\Foo', new Node\Name\FullyQualified('Foo')],
|
Chris@13
|
116 ['self', new Node\Name('self')],
|
Chris@13
|
117 ['?array', new Node\NullableType(new Node\Identifier('array'))],
|
Chris@13
|
118 ['?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))],
|
Chris@13
|
119 [new Node\Name('Some\Class'), new Node\Name('Some\Class')],
|
Chris@13
|
120 [
|
Chris@13
|
121 new Node\NullableType(new Node\Identifier('int')),
|
Chris@13
|
122 new Node\NullableType(new Node\Identifier('int'))
|
Chris@13
|
123 ],
|
Chris@13
|
124 [
|
Chris@0
|
125 new Node\NullableType(new Node\Name('Some\Class')),
|
Chris@0
|
126 new Node\NullableType(new Node\Name('Some\Class'))
|
Chris@13
|
127 ],
|
Chris@13
|
128 ];
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 public function testVoidTypeError() {
|
Chris@17
|
132 $this->expectException(\LogicException::class);
|
Chris@17
|
133 $this->expectExceptionMessage('Parameter type cannot be void');
|
Chris@17
|
134 $this->createParamBuilder('test')->setType('void');
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 public function testInvalidTypeError() {
|
Chris@17
|
138 $this->expectException(\LogicException::class);
|
Chris@17
|
139 $this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or NullableType');
|
Chris@17
|
140 $this->createParamBuilder('test')->setType(new \stdClass);
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 public function testByRef() {
|
Chris@0
|
144 $node = $this->createParamBuilder('test')
|
Chris@0
|
145 ->makeByRef()
|
Chris@0
|
146 ->getNode()
|
Chris@0
|
147 ;
|
Chris@0
|
148
|
Chris@0
|
149 $this->assertEquals(
|
Chris@13
|
150 new Node\Param(new Expr\Variable('test'), null, null, true),
|
Chris@0
|
151 $node
|
Chris@0
|
152 );
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 public function testVariadic() {
|
Chris@0
|
156 $node = $this->createParamBuilder('test')
|
Chris@0
|
157 ->makeVariadic()
|
Chris@0
|
158 ->getNode()
|
Chris@0
|
159 ;
|
Chris@0
|
160
|
Chris@0
|
161 $this->assertEquals(
|
Chris@13
|
162 new Node\Param(new Expr\Variable('test'), null, null, false, true),
|
Chris@0
|
163 $node
|
Chris@0
|
164 );
|
Chris@0
|
165 }
|
Chris@0
|
166 }
|