Completed
Pull Request — master (#88)
by Kévin
02:57
created

ClassDefintionTest::testHasConstWithNoParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 1
b 0
f 1
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
76
    public function testMethodSetGet()
77
    {
78
        $classDefinition = $this->getSimpleInstance();
79
        $methodName = 'method1';
80
        $nonExistsMethodName = 'method2';
81
82
        $this->assertFalse($classDefinition->hasMethod($methodName));
83
        $this->assertFalse($classDefinition->hasMethod($nonExistsMethodName));
84
85
        $classDefinition->addMethod(
86
            new \PHPSA\Definition\ClassMethod(
87
                $methodName,
88
                new \PhpParser\Node\Stmt\ClassMethod(
89
                    $methodName
90
                ),
91
                0
92
            )
93
        );
94
95
        $this->assertTrue($classDefinition->hasMethod($methodName));
96
        $this->assertFalse($classDefinition->hasMethod($nonExistsMethodName));
97
98
        $method = $classDefinition->getMethod($methodName);
99
        $this->assertInstanceOf('PHPSA\Definition\ClassMethod', $method);
100
        $this->assertSame($methodName, $method->getName());
101
102
        return $classDefinition;
103
    }
104
105
    public function testHasConstWithNoParent()
106
    {
107
        $classDefinition = $this->getSimpleInstance();
108
        $classDefinition->addConst(new ClassConst([
109
            new Const_('FOO', new String_('bar'))
110
        ]));
111
112
        $this->assertTrue($classDefinition->hasConst('FOO'));
113
        $this->assertFalse($classDefinition->hasConst('BAR'));
114
    }
115
116
    public function testHasConstWithParent()
117
    {
118
        $parentClassDefinition = $this->getSimpleInstance();
119
        $classDefinition = $this->getSimpleInstance();
120
121
        $parentClassDefinition->addConst(new ClassConst([
122
            new Const_('BAR', new String_('baz'))
123
        ]));
124
125
        $classDefinition->setExtendsClassDefinition($parentClassDefinition);
126
        $classDefinition->addConst(new ClassConst([
127
            new Const_('FOO', new String_('bar'))
128
        ]));
129
130
        $this->assertTrue($classDefinition->hasConst('FOO'));
131
        $this->assertFalse($classDefinition->hasConst('BAR'));
132
        $this->assertTrue($classDefinition->hasConst('BAR', true));
133
    }
134
}
135