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\Comment;
|
Chris@0
|
6 use PhpParser\Node\Expr;
|
Chris@0
|
7 use PhpParser\Node\Name;
|
Chris@0
|
8 use PhpParser\Node\Scalar;
|
Chris@0
|
9 use PhpParser\Node\Stmt;
|
Chris@0
|
10
|
Chris@17
|
11 class PropertyTest extends \PHPUnit\Framework\TestCase
|
Chris@0
|
12 {
|
Chris@0
|
13 public function createPropertyBuilder($name) {
|
Chris@0
|
14 return new Property($name);
|
Chris@0
|
15 }
|
Chris@0
|
16
|
Chris@0
|
17 public function testModifiers() {
|
Chris@0
|
18 $node = $this->createPropertyBuilder('test')
|
Chris@0
|
19 ->makePrivate()
|
Chris@0
|
20 ->makeStatic()
|
Chris@0
|
21 ->getNode()
|
Chris@0
|
22 ;
|
Chris@0
|
23
|
Chris@0
|
24 $this->assertEquals(
|
Chris@0
|
25 new Stmt\Property(
|
Chris@0
|
26 Stmt\Class_::MODIFIER_PRIVATE
|
Chris@0
|
27 | Stmt\Class_::MODIFIER_STATIC,
|
Chris@13
|
28 [
|
Chris@0
|
29 new Stmt\PropertyProperty('test')
|
Chris@13
|
30 ]
|
Chris@0
|
31 ),
|
Chris@0
|
32 $node
|
Chris@0
|
33 );
|
Chris@0
|
34
|
Chris@0
|
35 $node = $this->createPropertyBuilder('test')
|
Chris@0
|
36 ->makeProtected()
|
Chris@0
|
37 ->getNode()
|
Chris@0
|
38 ;
|
Chris@0
|
39
|
Chris@0
|
40 $this->assertEquals(
|
Chris@0
|
41 new Stmt\Property(
|
Chris@0
|
42 Stmt\Class_::MODIFIER_PROTECTED,
|
Chris@13
|
43 [
|
Chris@0
|
44 new Stmt\PropertyProperty('test')
|
Chris@13
|
45 ]
|
Chris@0
|
46 ),
|
Chris@0
|
47 $node
|
Chris@0
|
48 );
|
Chris@0
|
49
|
Chris@0
|
50 $node = $this->createPropertyBuilder('test')
|
Chris@0
|
51 ->makePublic()
|
Chris@0
|
52 ->getNode()
|
Chris@0
|
53 ;
|
Chris@0
|
54
|
Chris@0
|
55 $this->assertEquals(
|
Chris@0
|
56 new Stmt\Property(
|
Chris@0
|
57 Stmt\Class_::MODIFIER_PUBLIC,
|
Chris@13
|
58 [
|
Chris@0
|
59 new Stmt\PropertyProperty('test')
|
Chris@13
|
60 ]
|
Chris@0
|
61 ),
|
Chris@0
|
62 $node
|
Chris@0
|
63 );
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 public function testDocComment() {
|
Chris@0
|
67 $node = $this->createPropertyBuilder('test')
|
Chris@0
|
68 ->setDocComment('/** Test */')
|
Chris@0
|
69 ->getNode();
|
Chris@0
|
70
|
Chris@0
|
71 $this->assertEquals(new Stmt\Property(
|
Chris@0
|
72 Stmt\Class_::MODIFIER_PUBLIC,
|
Chris@13
|
73 [
|
Chris@0
|
74 new Stmt\PropertyProperty('test')
|
Chris@13
|
75 ],
|
Chris@13
|
76 [
|
Chris@13
|
77 'comments' => [new Comment\Doc('/** Test */')]
|
Chris@13
|
78 ]
|
Chris@0
|
79 ), $node);
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * @dataProvider provideTestDefaultValues
|
Chris@0
|
84 */
|
Chris@0
|
85 public function testDefaultValues($value, $expectedValueNode) {
|
Chris@0
|
86 $node = $this->createPropertyBuilder('test')
|
Chris@0
|
87 ->setDefault($value)
|
Chris@0
|
88 ->getNode()
|
Chris@0
|
89 ;
|
Chris@0
|
90
|
Chris@0
|
91 $this->assertEquals($expectedValueNode, $node->props[0]->default);
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 public function provideTestDefaultValues() {
|
Chris@13
|
95 return [
|
Chris@13
|
96 [
|
Chris@0
|
97 null,
|
Chris@0
|
98 new Expr\ConstFetch(new Name('null'))
|
Chris@13
|
99 ],
|
Chris@13
|
100 [
|
Chris@0
|
101 true,
|
Chris@0
|
102 new Expr\ConstFetch(new Name('true'))
|
Chris@13
|
103 ],
|
Chris@13
|
104 [
|
Chris@0
|
105 false,
|
Chris@0
|
106 new Expr\ConstFetch(new Name('false'))
|
Chris@13
|
107 ],
|
Chris@13
|
108 [
|
Chris@0
|
109 31415,
|
Chris@0
|
110 new Scalar\LNumber(31415)
|
Chris@13
|
111 ],
|
Chris@13
|
112 [
|
Chris@0
|
113 3.1415,
|
Chris@0
|
114 new Scalar\DNumber(3.1415)
|
Chris@13
|
115 ],
|
Chris@13
|
116 [
|
Chris@0
|
117 'Hallo World',
|
Chris@0
|
118 new Scalar\String_('Hallo World')
|
Chris@13
|
119 ],
|
Chris@13
|
120 [
|
Chris@13
|
121 [1, 2, 3],
|
Chris@13
|
122 new Expr\Array_([
|
Chris@0
|
123 new Expr\ArrayItem(new Scalar\LNumber(1)),
|
Chris@0
|
124 new Expr\ArrayItem(new Scalar\LNumber(2)),
|
Chris@0
|
125 new Expr\ArrayItem(new Scalar\LNumber(3)),
|
Chris@13
|
126 ])
|
Chris@13
|
127 ],
|
Chris@13
|
128 [
|
Chris@13
|
129 ['foo' => 'bar', 'bar' => 'foo'],
|
Chris@13
|
130 new Expr\Array_([
|
Chris@0
|
131 new Expr\ArrayItem(
|
Chris@0
|
132 new Scalar\String_('bar'),
|
Chris@0
|
133 new Scalar\String_('foo')
|
Chris@0
|
134 ),
|
Chris@0
|
135 new Expr\ArrayItem(
|
Chris@0
|
136 new Scalar\String_('foo'),
|
Chris@0
|
137 new Scalar\String_('bar')
|
Chris@0
|
138 ),
|
Chris@13
|
139 ])
|
Chris@13
|
140 ],
|
Chris@13
|
141 [
|
Chris@0
|
142 new Scalar\MagicConst\Dir,
|
Chris@0
|
143 new Scalar\MagicConst\Dir
|
Chris@13
|
144 ]
|
Chris@13
|
145 ];
|
Chris@0
|
146 }
|
Chris@0
|
147 }
|