1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\PHPSA\Defintion; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node\Const_; |
6
|
|
|
use PhpParser\Node\Scalar\String_; |
7
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
8
|
|
|
use PHPSA\Definition\ClassDefinition; |
9
|
|
|
use Tests\PHPSA\TestCase; |
10
|
|
|
|
11
|
|
|
class ClassDefintionTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return ClassDefinition |
15
|
|
|
*/ |
16
|
|
|
protected function getSimpleInstance() |
17
|
|
|
{ |
18
|
|
|
return new ClassDefinition('MyTestClass', 0); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testSimpleInstance() |
22
|
|
|
{ |
23
|
|
|
$classDefinition = $this->getSimpleInstance(); |
24
|
|
|
$this->assertSame('MyTestClass', $classDefinition->getName()); |
25
|
|
|
$this->assertFalse($classDefinition->isCompiled()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testScopePointer() |
29
|
|
|
{ |
30
|
|
|
$classDefinition = $this->getSimpleInstance(); |
31
|
|
|
|
32
|
|
|
$pointer = $classDefinition->getPointer(); |
33
|
|
|
$this->assertInstanceOf('PHPSA\ScopePointer', $pointer); |
34
|
|
|
$this->assertEquals($classDefinition, $pointer->getObject()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testSetGetHasForClassProperty() |
38
|
|
|
{ |
39
|
|
|
$classDefinition = $this->getSimpleInstance(); |
40
|
|
|
$this->assertFalse($classDefinition->hasProperty('test1')); |
41
|
|
|
$this->assertFalse($classDefinition->hasProperty('test2')); |
42
|
|
|
|
43
|
|
|
$property = new \PhpParser\Node\Stmt\Property( |
44
|
|
|
0, |
45
|
|
|
array( |
46
|
|
|
new \PhpParser\Node\Stmt\PropertyProperty( |
47
|
|
|
'test1', |
48
|
|
|
new \PhpParser\Node\Scalar\String_( |
49
|
|
|
'test string' |
50
|
|
|
) |
51
|
|
|
) |
52
|
|
|
) |
53
|
|
|
); |
54
|
|
|
$classDefinition->addProperty($property); |
55
|
|
|
|
56
|
|
|
$this->assertTrue($classDefinition->hasProperty('test1')); |
57
|
|
|
$this->assertFalse($classDefinition->hasProperty('test2')); |
58
|
|
|
|
59
|
|
|
$property = new \PhpParser\Node\Stmt\Property( |
60
|
|
|
0, |
61
|
|
|
array( |
62
|
|
|
new \PhpParser\Node\Stmt\PropertyProperty( |
63
|
|
|
'test2', |
64
|
|
|
new \PhpParser\Node\Scalar\String_( |
65
|
|
|
'test string' |
66
|
|
|
) |
67
|
|
|
) |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
$classDefinition->addProperty($property); |
71
|
|
|
|
72
|
|
|
$this->assertTrue($classDefinition->hasProperty('test1')); |
73
|
|
|
$this->assertTrue($classDefinition->hasProperty('test2')); |
74
|
|
|
|
75
|
|
|
$property = new \PhpParser\Node\Stmt\Property(0, [ |
76
|
|
|
new \PhpParser\Node\Stmt\PropertyProperty( |
77
|
|
|
'foo', |
78
|
|
|
new \PhpParser\Node\Scalar\String_('test string') |
79
|
|
|
), |
80
|
|
|
new \PhpParser\Node\Stmt\PropertyProperty( |
81
|
|
|
'bar', |
82
|
|
|
new \PhpParser\Node\Scalar\String_('test string') |
83
|
|
|
) |
84
|
|
|
]); |
85
|
|
|
$classDefinition->addProperty($property); |
86
|
|
|
|
87
|
|
|
$this->assertTrue($classDefinition->hasProperty('foo')); |
88
|
|
|
$this->assertTrue($classDefinition->hasProperty('bar')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testMethodSetGet() |
92
|
|
|
{ |
93
|
|
|
$classDefinition = $this->getSimpleInstance(); |
94
|
|
|
$methodName = 'method1'; |
95
|
|
|
$nonExistsMethodName = 'method2'; |
96
|
|
|
|
97
|
|
|
$this->assertFalse($classDefinition->hasMethod($methodName)); |
98
|
|
|
$this->assertFalse($classDefinition->hasMethod($nonExistsMethodName)); |
99
|
|
|
|
100
|
|
|
$classDefinition->addMethod( |
101
|
|
|
new \PHPSA\Definition\ClassMethod( |
102
|
|
|
$methodName, |
103
|
|
|
new \PhpParser\Node\Stmt\ClassMethod( |
104
|
|
|
$methodName |
105
|
|
|
), |
106
|
|
|
0 |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
$this->assertTrue($classDefinition->hasMethod($methodName)); |
111
|
|
|
$this->assertFalse($classDefinition->hasMethod($nonExistsMethodName)); |
112
|
|
|
|
113
|
|
|
$method = $classDefinition->getMethod($methodName); |
114
|
|
|
$this->assertInstanceOf('PHPSA\Definition\ClassMethod', $method); |
115
|
|
|
$this->assertSame($methodName, $method->getName()); |
116
|
|
|
|
117
|
|
|
return $classDefinition; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testHasConstWithNoParent() |
121
|
|
|
{ |
122
|
|
|
$classDefinition = $this->getSimpleInstance(); |
123
|
|
|
$classDefinition->addConst(new ClassConst([ |
124
|
|
|
new Const_('FOO', new String_('bar')) |
125
|
|
|
])); |
126
|
|
|
|
127
|
|
|
$this->assertTrue($classDefinition->hasConst('FOO')); |
128
|
|
|
$this->assertFalse($classDefinition->hasConst('BAR')); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testHasConstWithParent() |
132
|
|
|
{ |
133
|
|
|
$parentClassDefinition = $this->getSimpleInstance(); |
134
|
|
|
$classDefinition = $this->getSimpleInstance(); |
135
|
|
|
|
136
|
|
|
$parentClassDefinition->addConst(new ClassConst([ |
137
|
|
|
new Const_('BAR', new String_('baz')) |
138
|
|
|
])); |
139
|
|
|
|
140
|
|
|
$classDefinition->setExtendsClassDefinition($parentClassDefinition); |
141
|
|
|
$classDefinition->addConst(new ClassConst([ |
142
|
|
|
new Const_('FOO', new String_('bar')) |
143
|
|
|
])); |
144
|
|
|
|
145
|
|
|
$this->assertTrue($classDefinition->hasConst('FOO')); |
146
|
|
|
$this->assertFalse($classDefinition->hasConst('BAR')); |
147
|
|
|
$this->assertTrue($classDefinition->hasConst('BAR', true)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|