1<?php declare(strict_types=1);
2
3namespace PhpParser\Node\Stmt;
4
5use PhpParser\Modifiers;
6use PhpParser\Node\PropertyItem;
7use PhpParser\Node\Scalar\String_;
8
9class ClassTest extends \PHPUnit\Framework\TestCase {
10    public function testIsAbstract(): void {
11        $class = new Class_('Foo', ['type' => Modifiers::ABSTRACT]);
12        $this->assertTrue($class->isAbstract());
13
14        $class = new Class_('Foo');
15        $this->assertFalse($class->isAbstract());
16    }
17
18    public function testIsFinal(): void {
19        $class = new Class_('Foo', ['type' => Modifiers::FINAL]);
20        $this->assertTrue($class->isFinal());
21
22        $class = new Class_('Foo');
23        $this->assertFalse($class->isFinal());
24    }
25
26    public function testGetTraitUses(): void {
27        $traitUses = [
28            new TraitUse([new Trait_('foo')]),
29            new TraitUse([new Trait_('bar')]),
30        ];
31        $class = new Class_('Foo', [
32            'stmts' => [
33                $traitUses[0],
34                new ClassMethod('fooBar'),
35                $traitUses[1],
36            ]
37        ]);
38
39        $this->assertSame($traitUses, $class->getTraitUses());
40    }
41
42    public function testGetMethods(): void {
43        $methods = [
44            new ClassMethod('foo'),
45            new ClassMethod('bar'),
46            new ClassMethod('fooBar'),
47        ];
48        $class = new Class_('Foo', [
49            'stmts' => [
50                new TraitUse([]),
51                $methods[0],
52                new ClassConst([]),
53                $methods[1],
54                new Property(0, []),
55                $methods[2],
56            ]
57        ]);
58
59        $this->assertSame($methods, $class->getMethods());
60    }
61
62    public function testGetConstants(): void {
63        $constants = [
64            new ClassConst([new \PhpParser\Node\Const_('foo', new String_('foo_value'))]),
65            new ClassConst([new \PhpParser\Node\Const_('bar', new String_('bar_value'))]),
66        ];
67        $class = new Class_('Foo', [
68            'stmts' => [
69                new TraitUse([]),
70                $constants[0],
71                new ClassMethod('fooBar'),
72                $constants[1],
73            ]
74        ]);
75
76        $this->assertSame($constants, $class->getConstants());
77    }
78
79    public function testGetProperties(): void {
80        $properties = [
81            new Property(Modifiers::PUBLIC, [new PropertyItem('foo')]),
82            new Property(Modifiers::PUBLIC, [new PropertyItem('bar')]),
83        ];
84        $class = new Class_('Foo', [
85            'stmts' => [
86                new TraitUse([]),
87                $properties[0],
88                new ClassConst([]),
89                $properties[1],
90                new ClassMethod('fooBar'),
91            ]
92        ]);
93
94        $this->assertSame($properties, $class->getProperties());
95    }
96
97    public function testGetProperty(): void {
98        $properties = [
99            $fooProp = new Property(Modifiers::PUBLIC, [new PropertyItem('foo1')]),
100            $barProp = new Property(Modifiers::PUBLIC, [new PropertyItem('BAR1')]),
101            $fooBarProp = new Property(Modifiers::PUBLIC, [new PropertyItem('foo2'), new PropertyItem('bar2')]),
102        ];
103        $class = new Class_('Foo', [
104            'stmts' => [
105                new TraitUse([]),
106                $properties[0],
107                new ClassConst([]),
108                $properties[1],
109                new ClassMethod('fooBar'),
110                $properties[2],
111            ]
112        ]);
113
114        $this->assertSame($fooProp, $class->getProperty('foo1'));
115        $this->assertSame($barProp, $class->getProperty('BAR1'));
116        $this->assertSame($fooBarProp, $class->getProperty('foo2'));
117        $this->assertSame($fooBarProp, $class->getProperty('bar2'));
118        $this->assertNull($class->getProperty('bar1'));
119        $this->assertNull($class->getProperty('nonExisting'));
120    }
121
122    public function testGetMethod(): void {
123        $methodConstruct = new ClassMethod('__CONSTRUCT');
124        $methodTest = new ClassMethod('test');
125        $class = new Class_('Foo', [
126            'stmts' => [
127                new ClassConst([]),
128                $methodConstruct,
129                new Property(0, []),
130                $methodTest,
131            ]
132        ]);
133
134        $this->assertSame($methodConstruct, $class->getMethod('__construct'));
135        $this->assertSame($methodTest, $class->getMethod('test'));
136        $this->assertNull($class->getMethod('nonExisting'));
137    }
138}
139