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