php-parser/test/PhpParser/Builder/MethodTest.php

164 lines
4.4 KiB
PHP
Raw Normal View History

2012-03-10 23:25:26 +01:00
<?php
namespace PhpParser\Builder;
use PhpParser\Node;
use PhpParser\Node\Expr\Print_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
use PhpParser\Comment;
class MethodTest extends \PHPUnit_Framework_TestCase
2012-03-10 23:25:26 +01:00
{
public function createMethodBuilder($name) {
return new Method($name);
2012-03-10 23:25:26 +01:00
}
public function testModifiers() {
$node = $this->createMethodBuilder('test')
->makePublic()
->makeAbstract()
->makeStatic()
->getNode()
;
$this->assertEquals(
new Stmt\ClassMethod('test', array(
'flags' => Stmt\Class_::MODIFIER_PUBLIC
| Stmt\Class_::MODIFIER_ABSTRACT
| Stmt\Class_::MODIFIER_STATIC,
2012-03-10 23:25:26 +01:00
'stmts' => null,
)),
$node
);
$node = $this->createMethodBuilder('test')
->makeProtected()
->makeFinal()
->getNode()
;
$this->assertEquals(
new Stmt\ClassMethod('test', array(
'flags' => Stmt\Class_::MODIFIER_PROTECTED
| Stmt\Class_::MODIFIER_FINAL
2012-03-10 23:25:26 +01:00
)),
$node
);
$node = $this->createMethodBuilder('test')
->makePrivate()
->getNode()
;
$this->assertEquals(
new Stmt\ClassMethod('test', array(
'type' => Stmt\Class_::MODIFIER_PRIVATE
2012-03-10 23:25:26 +01:00
)),
$node
);
}
public function testReturnByRef() {
$node = $this->createMethodBuilder('test')
->makeReturnByRef()
->getNode()
;
$this->assertEquals(
new Stmt\ClassMethod('test', array(
2012-03-10 23:25:26 +01:00
'byRef' => true
)),
$node
);
}
public function testParams() {
$param1 = new Node\Param('test1');
$param2 = new Node\Param('test2');
$param3 = new Node\Param('test3');
2012-03-10 23:25:26 +01:00
$node = $this->createMethodBuilder('test')
->addParam($param1)
->addParams(array($param2, $param3))
->getNode()
;
$this->assertEquals(
new Stmt\ClassMethod('test', array(
2012-03-10 23:25:26 +01:00
'params' => array($param1, $param2, $param3)
)),
$node
);
}
public function testStmts() {
$stmt1 = new Print_(new String_('test1'));
$stmt2 = new Print_(new String_('test2'));
$stmt3 = new Print_(new String_('test3'));
2012-03-10 23:25:26 +01:00
$node = $this->createMethodBuilder('test')
->addStmt($stmt1)
->addStmts(array($stmt2, $stmt3))
->getNode()
;
$this->assertEquals(
new Stmt\ClassMethod('test', array(
2012-03-10 23:25:26 +01:00
'stmts' => array($stmt1, $stmt2, $stmt3)
)),
$node
);
}
public function testDocComment() {
$node = $this->createMethodBuilder('test')
->setDocComment('/** Test */')
->getNode();
$this->assertEquals(new Stmt\ClassMethod('test', array(), array(
'comments' => array(new Comment\Doc('/** Test */'))
)), $node);
}
2012-03-10 23:25:26 +01:00
public function testReturnType() {
$node = $this->createMethodBuilder('test')
->setReturnType('bool')
->getNode();
$this->assertEquals(new Stmt\ClassMethod('test', array(
'returnType' => 'bool'
), array()), $node);
}
2012-03-10 23:25:26 +01:00
/**
* @expectedException \LogicException
2012-03-10 23:25:26 +01:00
* @expectedExceptionMessage Cannot add statements to an abstract method
*/
public function testAddStmtToAbstractMethodError() {
$this->createMethodBuilder('test')
->makeAbstract()
->addStmt(new Print_(new String_('test')))
2012-03-10 23:25:26 +01:00
;
}
/**
* @expectedException \LogicException
2012-03-10 23:25:26 +01:00
* @expectedExceptionMessage Cannot make method with statements abstract
*/
public function testMakeMethodWithStmtsAbstractError() {
$this->createMethodBuilder('test')
->addStmt(new Print_(new String_('test')))
2012-03-10 23:25:26 +01:00
->makeAbstract()
;
}
/**
* @expectedException \LogicException
2012-03-10 23:25:26 +01:00
* @expectedExceptionMessage Expected parameter node, got "Name"
*/
public function testInvalidParamError() {
$this->createMethodBuilder('test')
->addParam(new Node\Name('foo'))
2012-03-10 23:25:26 +01:00
;
}
}