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

137 lines
3.9 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2012-03-11 09:02:52 +01:00
namespace PhpParser\Builder;
use PhpParser\Comment;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr\Print_;
2017-01-19 23:32:49 +01:00
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
2016-11-23 22:58:18 +01:00
use PhpParser\Node\Stmt;
class FunctionTest extends \PHPUnit\Framework\TestCase
2012-03-11 09:02:52 +01:00
{
public function createFunctionBuilder($name) {
return new Function_($name);
2012-03-11 09:02:52 +01:00
}
public function testReturnByRef() {
$node = $this->createFunctionBuilder('test')
->makeReturnByRef()
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\Function_('test', [
2012-03-11 09:02:52 +01:00
'byRef' => true
2017-08-13 14:35:03 +02:00
]),
2012-03-11 09:02:52 +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-11 09:02:52 +01:00
$node = $this->createFunctionBuilder('test')
->addParam($param1)
2017-08-13 14:35:03 +02:00
->addParams([$param2, $param3])
2012-03-11 09:02:52 +01:00
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\Function_('test', [
'params' => [$param1, $param2, $param3]
]),
2012-03-11 09:02:52 +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-11 09:02:52 +01:00
$node = $this->createFunctionBuilder('test')
->addStmt($stmt1)
2017-08-13 14:35:03 +02:00
->addStmts([$stmt2, $stmt3])
2012-03-11 09:02:52 +01:00
->getNode()
;
$this->assertEquals(
2017-08-13 14:35:03 +02:00
new Stmt\Function_('test', [
'stmts' => [
new Stmt\Expression($stmt1),
new Stmt\Expression($stmt2),
new Stmt\Expression($stmt3),
2017-08-13 14:35:03 +02:00
]
]),
2012-03-11 09:02:52 +01:00
$node
);
}
public function testDocComment() {
$node = $this->createFunctionBuilder('test')
->setDocComment('/** Test */')
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\Function_('test', [], [
'comments' => [new Comment\Doc('/** Test */')]
]), $node);
}
public function testAddAttribute() {
$attribute = new Attribute(
new Name('Attr'),
[new Arg(new LNumber(1), false, false, [], new Identifier('name'))]
);
$attributeGroup = new AttributeGroup([$attribute]);
$node = $this->createFunctionBuilder('attrGroup')
->addAttribute($attributeGroup)
->getNode();
$this->assertEquals(new Stmt\Function_('attrGroup', [
'attrGroups' => [$attributeGroup],
], []), $node);
}
public function testReturnType() {
$node = $this->createFunctionBuilder('test')
->setReturnType('void')
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\Function_('test', [
'returnType' => 'void'
2017-08-13 14:35:03 +02:00
], []), $node);
}
public function testInvalidNullableVoidType() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('void type cannot be nullable');
$this->createFunctionBuilder('test')->setReturnType('?void');
}
2012-03-11 09:02:52 +01:00
public function testInvalidParamError() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected parameter node, got "Name"');
2012-03-11 09:02:52 +01:00
$this->createFunctionBuilder('test')
->addParam(new Node\Name('foo'))
2012-03-11 09:02:52 +01:00
;
}
public function testAddNonStmt() {
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Expected statement or expression node');
$this->createFunctionBuilder('test')
->addStmt(new Node\Name('Test'));
}
}