comparison vendor/nikic/php-parser/test/PhpParser/Builder/MethodTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 5fb285c0d0e3
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace PhpParser\Builder;
4
5 use PhpParser\Comment;
6 use PhpParser\Node;
7 use PhpParser\Node\Expr\Print_;
8 use PhpParser\Node\Scalar\String_;
9 use PhpParser\Node\Stmt;
10
11 class MethodTest extends \PHPUnit_Framework_TestCase
12 {
13 public function createMethodBuilder($name) {
14 return new Method($name);
15 }
16
17 public function testModifiers() {
18 $node = $this->createMethodBuilder('test')
19 ->makePublic()
20 ->makeAbstract()
21 ->makeStatic()
22 ->getNode()
23 ;
24
25 $this->assertEquals(
26 new Stmt\ClassMethod('test', array(
27 'flags' => Stmt\Class_::MODIFIER_PUBLIC
28 | Stmt\Class_::MODIFIER_ABSTRACT
29 | Stmt\Class_::MODIFIER_STATIC,
30 'stmts' => null,
31 )),
32 $node
33 );
34
35 $node = $this->createMethodBuilder('test')
36 ->makeProtected()
37 ->makeFinal()
38 ->getNode()
39 ;
40
41 $this->assertEquals(
42 new Stmt\ClassMethod('test', array(
43 'flags' => Stmt\Class_::MODIFIER_PROTECTED
44 | Stmt\Class_::MODIFIER_FINAL
45 )),
46 $node
47 );
48
49 $node = $this->createMethodBuilder('test')
50 ->makePrivate()
51 ->getNode()
52 ;
53
54 $this->assertEquals(
55 new Stmt\ClassMethod('test', array(
56 'type' => Stmt\Class_::MODIFIER_PRIVATE
57 )),
58 $node
59 );
60 }
61
62 public function testReturnByRef() {
63 $node = $this->createMethodBuilder('test')
64 ->makeReturnByRef()
65 ->getNode()
66 ;
67
68 $this->assertEquals(
69 new Stmt\ClassMethod('test', array(
70 'byRef' => true
71 )),
72 $node
73 );
74 }
75
76 public function testParams() {
77 $param1 = new Node\Param('test1');
78 $param2 = new Node\Param('test2');
79 $param3 = new Node\Param('test3');
80
81 $node = $this->createMethodBuilder('test')
82 ->addParam($param1)
83 ->addParams(array($param2, $param3))
84 ->getNode()
85 ;
86
87 $this->assertEquals(
88 new Stmt\ClassMethod('test', array(
89 'params' => array($param1, $param2, $param3)
90 )),
91 $node
92 );
93 }
94
95 public function testStmts() {
96 $stmt1 = new Print_(new String_('test1'));
97 $stmt2 = new Print_(new String_('test2'));
98 $stmt3 = new Print_(new String_('test3'));
99
100 $node = $this->createMethodBuilder('test')
101 ->addStmt($stmt1)
102 ->addStmts(array($stmt2, $stmt3))
103 ->getNode()
104 ;
105
106 $this->assertEquals(
107 new Stmt\ClassMethod('test', array(
108 'stmts' => array($stmt1, $stmt2, $stmt3)
109 )),
110 $node
111 );
112 }
113 public function testDocComment() {
114 $node = $this->createMethodBuilder('test')
115 ->setDocComment('/** Test */')
116 ->getNode();
117
118 $this->assertEquals(new Stmt\ClassMethod('test', array(), array(
119 'comments' => array(new Comment\Doc('/** Test */'))
120 )), $node);
121 }
122
123 public function testReturnType() {
124 $node = $this->createMethodBuilder('test')
125 ->setReturnType('bool')
126 ->getNode();
127 $this->assertEquals(new Stmt\ClassMethod('test', array(
128 'returnType' => 'bool'
129 ), array()), $node);
130 }
131
132 /**
133 * @expectedException \LogicException
134 * @expectedExceptionMessage Cannot add statements to an abstract method
135 */
136 public function testAddStmtToAbstractMethodError() {
137 $this->createMethodBuilder('test')
138 ->makeAbstract()
139 ->addStmt(new Print_(new String_('test')))
140 ;
141 }
142
143 /**
144 * @expectedException \LogicException
145 * @expectedExceptionMessage Cannot make method with statements abstract
146 */
147 public function testMakeMethodWithStmtsAbstractError() {
148 $this->createMethodBuilder('test')
149 ->addStmt(new Print_(new String_('test')))
150 ->makeAbstract()
151 ;
152 }
153
154 /**
155 * @expectedException \LogicException
156 * @expectedExceptionMessage Expected parameter node, got "Name"
157 */
158 public function testInvalidParamError() {
159 $this->createMethodBuilder('test')
160 ->addParam(new Node\Name('foo'))
161 ;
162 }
163 }