1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-30 04:19:30 +01:00
PHP-Parser/test/PhpParser/Builder/MethodTest.php

163 lines
4.5 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2012-03-10 23:25:26 +01:00
namespace PhpParser\Builder;
2016-11-23 22:58:18 +01:00
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Expr\Print_;
2017-01-19 23:32:49 +01:00
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt;
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(
2017-08-13 14:35:03 +02:00
new Stmt\ClassMethod('test', [
'flags' => Stmt\Class_::MODIFIER_PUBLIC
| Stmt\Class_::MODIFIER_ABSTRACT
| Stmt\Class_::MODIFIER_STATIC,
2012-03-10 23:25:26 +01:00
'stmts' => null,
2017-08-13 14:35:03 +02:00
]),
2012-03-10 23:25:26 +01:00
$node
);
$node = $this->createMethodBuilder('test')
->makeProtected()
->makeFinal()
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\ClassMethod('test', [
'flags' => Stmt\Class_::MODIFIER_PROTECTED
| Stmt\Class_::MODIFIER_FINAL
2017-08-13 14:35:03 +02:00
]),
2012-03-10 23:25:26 +01:00
$node
);
$node = $this->createMethodBuilder('test')
->makePrivate()
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\ClassMethod('test', [
'type' => Stmt\Class_::MODIFIER_PRIVATE
2017-08-13 14:35:03 +02:00
]),
2012-03-10 23:25:26 +01:00
$node
);
}
public function testReturnByRef() {
$node = $this->createMethodBuilder('test')
->makeReturnByRef()
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\ClassMethod('test', [
2012-03-10 23:25:26 +01:00
'byRef' => true
2017-08-13 14:35:03 +02:00
]),
2012-03-10 23:25:26 +01:00
$node
);
}
public function testParams() {
2017-01-19 23:32:49 +01:00
$param1 = new Node\Param(new Variable('test1'));
$param2 = new Node\Param(new Variable('test2'));
$param3 = new Node\Param(new Variable('test3'));
2012-03-10 23:25:26 +01:00
$node = $this->createMethodBuilder('test')
->addParam($param1)
2017-08-13 14:35:03 +02:00
->addParams([$param2, $param3])
2012-03-10 23:25:26 +01:00
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\ClassMethod('test', [
'params' => [$param1, $param2, $param3]
]),
2012-03-10 23:25:26 +01:00
$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)
2017-08-13 14:35:03 +02:00
->addStmts([$stmt2, $stmt3])
2012-03-10 23:25:26 +01:00
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\ClassMethod('test', [
'stmts' => [
new Stmt\Expression($stmt1),
new Stmt\Expression($stmt2),
new Stmt\Expression($stmt3),
2017-08-13 14:35:03 +02:00
]
]),
2012-03-10 23:25:26 +01:00
$node
);
}
public function testDocComment() {
$node = $this->createMethodBuilder('test')
->setDocComment('/** Test */')
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\ClassMethod('test', [], [
'comments' => [new Comment\Doc('/** Test */')]
]), $node);
}
2012-03-10 23:25:26 +01:00
public function testReturnType() {
$node = $this->createMethodBuilder('test')
->setReturnType('bool')
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\ClassMethod('test', [
'returnType' => 'bool'
2017-08-13 14:35:03 +02:00
], []), $node);
}
2012-03-10 23:25:26 +01:00
public function testAddStmtToAbstractMethodError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot add statements to an abstract method');
2012-03-10 23:25:26 +01:00
$this->createMethodBuilder('test')
->makeAbstract()
->addStmt(new Print_(new String_('test')))
2012-03-10 23:25:26 +01:00
;
}
public function testMakeMethodWithStmtsAbstractError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot make method with statements abstract');
2012-03-10 23:25:26 +01:00
$this->createMethodBuilder('test')
->addStmt(new Print_(new String_('test')))
2012-03-10 23:25:26 +01:00
->makeAbstract()
;
}
public function testInvalidParamError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected parameter node, got "Name"');
2012-03-10 23:25:26 +01:00
$this->createMethodBuilder('test')
->addParam(new Node\Name('foo'))
2012-03-10 23:25:26 +01:00
;
}
}