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