1<?php declare(strict_types=1);
2
3namespace PhpParser\Builder;
4
5use PhpParser\Comment;
6use PhpParser\Modifiers;
7use PhpParser\Node;
8use PhpParser\Node\Arg;
9use PhpParser\Node\Attribute;
10use PhpParser\Node\AttributeGroup;
11use PhpParser\Node\Expr\Print_;
12use PhpParser\Node\Expr\Variable;
13use PhpParser\Node\Identifier;
14use PhpParser\Node\Name;
15use PhpParser\Node\Scalar\Int_;
16use PhpParser\Node\Scalar\String_;
17use PhpParser\Node\Stmt;
18
19class MethodTest extends \PHPUnit\Framework\TestCase {
20    public function createMethodBuilder($name) {
21        return new Method($name);
22    }
23
24    public function testModifiers(): void {
25        $node = $this->createMethodBuilder('test')
26            ->makePublic()
27            ->makeAbstract()
28            ->makeStatic()
29            ->getNode()
30        ;
31
32        $this->assertEquals(
33            new Stmt\ClassMethod('test', [
34                'flags' => Modifiers::PUBLIC | Modifiers::ABSTRACT | Modifiers::STATIC,
35                'stmts' => null,
36            ]),
37            $node
38        );
39
40        $node = $this->createMethodBuilder('test')
41            ->makeProtected()
42            ->makeFinal()
43            ->getNode()
44        ;
45
46        $this->assertEquals(
47            new Stmt\ClassMethod('test', [
48                'flags' => Modifiers::PROTECTED | Modifiers::FINAL
49            ]),
50            $node
51        );
52
53        $node = $this->createMethodBuilder('test')
54            ->makePrivate()
55            ->getNode()
56        ;
57
58        $this->assertEquals(
59            new Stmt\ClassMethod('test', [
60                'type' => Modifiers::PRIVATE
61            ]),
62            $node
63        );
64    }
65
66    public function testReturnByRef(): void {
67        $node = $this->createMethodBuilder('test')
68            ->makeReturnByRef()
69            ->getNode()
70        ;
71
72        $this->assertEquals(
73            new Stmt\ClassMethod('test', [
74                'byRef' => true
75            ]),
76            $node
77        );
78    }
79
80    public function testParams(): void {
81        $param1 = new Node\Param(new Variable('test1'));
82        $param2 = new Node\Param(new Variable('test2'));
83        $param3 = new Node\Param(new Variable('test3'));
84
85        $node = $this->createMethodBuilder('test')
86            ->addParam($param1)
87            ->addParams([$param2, $param3])
88            ->getNode()
89        ;
90
91        $this->assertEquals(
92            new Stmt\ClassMethod('test', [
93                'params' => [$param1, $param2, $param3]
94            ]),
95            $node
96        );
97    }
98
99    public function testStmts(): void {
100        $stmt1 = new Print_(new String_('test1'));
101        $stmt2 = new Print_(new String_('test2'));
102        $stmt3 = new Print_(new String_('test3'));
103
104        $node = $this->createMethodBuilder('test')
105            ->addStmt($stmt1)
106            ->addStmts([$stmt2, $stmt3])
107            ->getNode()
108        ;
109
110        $this->assertEquals(
111            new Stmt\ClassMethod('test', [
112                'stmts' => [
113                    new Stmt\Expression($stmt1),
114                    new Stmt\Expression($stmt2),
115                    new Stmt\Expression($stmt3),
116                ]
117            ]),
118            $node
119        );
120    }
121    public function testDocComment(): void {
122        $node = $this->createMethodBuilder('test')
123            ->setDocComment('/** Test */')
124            ->getNode();
125
126        $this->assertEquals(new Stmt\ClassMethod('test', [], [
127            'comments' => [new Comment\Doc('/** Test */')]
128        ]), $node);
129    }
130
131    public function testAddAttribute(): void {
132        $attribute = new Attribute(
133            new Name('Attr'),
134            [new Arg(new Int_(1), false, false, [], new Identifier('name'))]
135        );
136        $attributeGroup = new AttributeGroup([$attribute]);
137
138        $node = $this->createMethodBuilder('attributeGroup')
139            ->addAttribute($attributeGroup)
140            ->getNode();
141
142        $this->assertEquals(new Stmt\ClassMethod('attributeGroup', [
143            'attrGroups' => [$attributeGroup],
144        ], []), $node);
145    }
146
147    public function testReturnType(): void {
148        $node = $this->createMethodBuilder('test')
149            ->setReturnType('bool')
150            ->getNode();
151        $this->assertEquals(new Stmt\ClassMethod('test', [
152            'returnType' => new Identifier('bool'),
153        ], []), $node);
154    }
155
156    public function testAddStmtToAbstractMethodError(): void {
157        $this->expectException(\LogicException::class);
158        $this->expectExceptionMessage('Cannot add statements to an abstract method');
159        $this->createMethodBuilder('test')
160            ->makeAbstract()
161            ->addStmt(new Print_(new String_('test')))
162        ;
163    }
164
165    public function testMakeMethodWithStmtsAbstractError(): void {
166        $this->expectException(\LogicException::class);
167        $this->expectExceptionMessage('Cannot make method with statements abstract');
168        $this->createMethodBuilder('test')
169            ->addStmt(new Print_(new String_('test')))
170            ->makeAbstract()
171        ;
172    }
173
174    public function testInvalidParamError(): void {
175        $this->expectException(\LogicException::class);
176        $this->expectExceptionMessage('Expected parameter node, got "Name"');
177        $this->createMethodBuilder('test')
178            ->addParam(new Node\Name('foo'))
179        ;
180    }
181}
182