3 namespace PhpParser\Builder;
7 use PhpParser\Node\Expr\Print_;
8 use PhpParser\Node\Scalar\String_;
9 use PhpParser\Node\Stmt;
11 class MethodTest extends \PHPUnit_Framework_TestCase
13 public function createMethodBuilder($name) {
14 return new Method($name);
17 public function testModifiers() {
18 $node = $this->createMethodBuilder('test')
26 new Stmt\ClassMethod('test', array(
27 'flags' => Stmt\Class_::MODIFIER_PUBLIC
28 | Stmt\Class_::MODIFIER_ABSTRACT
29 | Stmt\Class_::MODIFIER_STATIC,
35 $node = $this->createMethodBuilder('test')
42 new Stmt\ClassMethod('test', array(
43 'flags' => Stmt\Class_::MODIFIER_PROTECTED
44 | Stmt\Class_::MODIFIER_FINAL
49 $node = $this->createMethodBuilder('test')
55 new Stmt\ClassMethod('test', array(
56 'type' => Stmt\Class_::MODIFIER_PRIVATE
62 public function testReturnByRef() {
63 $node = $this->createMethodBuilder('test')
69 new Stmt\ClassMethod('test', array(
76 public function testParams() {
77 $param1 = new Node\Param('test1');
78 $param2 = new Node\Param('test2');
79 $param3 = new Node\Param('test3');
81 $node = $this->createMethodBuilder('test')
83 ->addParams(array($param2, $param3))
88 new Stmt\ClassMethod('test', array(
89 'params' => array($param1, $param2, $param3)
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'));
100 $node = $this->createMethodBuilder('test')
102 ->addStmts(array($stmt2, $stmt3))
107 new Stmt\ClassMethod('test', array(
108 'stmts' => array($stmt1, $stmt2, $stmt3)
113 public function testDocComment() {
114 $node = $this->createMethodBuilder('test')
115 ->setDocComment('/** Test */')
118 $this->assertEquals(new Stmt\ClassMethod('test', array(), array(
119 'comments' => array(new Comment\Doc('/** Test */'))
123 public function testReturnType() {
124 $node = $this->createMethodBuilder('test')
125 ->setReturnType('bool')
127 $this->assertEquals(new Stmt\ClassMethod('test', array(
128 'returnType' => 'bool'
133 * @expectedException \LogicException
134 * @expectedExceptionMessage Cannot add statements to an abstract method
136 public function testAddStmtToAbstractMethodError() {
137 $this->createMethodBuilder('test')
139 ->addStmt(new Print_(new String_('test')))
144 * @expectedException \LogicException
145 * @expectedExceptionMessage Cannot make method with statements abstract
147 public function testMakeMethodWithStmtsAbstractError() {
148 $this->createMethodBuilder('test')
149 ->addStmt(new Print_(new String_('test')))
155 * @expectedException \LogicException
156 * @expectedExceptionMessage Expected parameter node, got "Name"
158 public function testInvalidParamError() {
159 $this->createMethodBuilder('test')
160 ->addParam(new Node\Name('foo'))