2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-11 09:02:52 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
2014-12-13 13:44:40 +01:00
|
|
|
use PhpParser\Comment;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Print_;
|
2017-01-19 23:32:49 +01:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2015-03-20 21:47:20 +01:00
|
|
|
use PhpParser\Node\Scalar\String_;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class FunctionTest extends \PHPUnit\Framework\TestCase
|
2012-03-11 09:02:52 +01:00
|
|
|
{
|
|
|
|
public function createFunctionBuilder($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
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() {
|
2015-03-20 21:47:20 +01:00
|
|
|
$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' => [
|
2017-01-19 22:39:21 +01:00
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-13 13:44:40 +01:00
|
|
|
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);
|
2014-12-13 13:44:40 +01:00
|
|
|
}
|
|
|
|
|
2016-04-09 11:41:38 +02:00
|
|
|
public function testReturnType() {
|
|
|
|
$node = $this->createFunctionBuilder('test')
|
2016-09-16 13:52:47 +02:00
|
|
|
->setReturnType('void')
|
2016-04-09 11:41:38 +02:00
|
|
|
->getNode();
|
|
|
|
|
2017-08-13 14:35:03 +02:00
|
|
|
$this->assertEquals(new Stmt\Function_('test', [
|
2016-09-16 13:52:47 +02:00
|
|
|
'returnType' => 'void'
|
2017-08-13 14:35:03 +02:00
|
|
|
], []), $node);
|
2016-04-09 11:41:38 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 13:52:47 +02:00
|
|
|
public function testInvalidNullableVoidType() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage('void type cannot be nullable');
|
2016-09-16 13:52:47 +02:00
|
|
|
$this->createFunctionBuilder('test')->setReturnType('?void');
|
|
|
|
}
|
|
|
|
|
2012-03-11 09:02:52 +01:00
|
|
|
public function testInvalidParamError() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage('Expected parameter node, got "Name"');
|
2012-03-11 09:02:52 +01:00
|
|
|
$this->createFunctionBuilder('test')
|
2014-02-06 14:44:16 +01:00
|
|
|
->addParam(new Node\Name('foo'))
|
2012-03-11 09:02:52 +01:00
|
|
|
;
|
|
|
|
}
|
2017-01-19 22:39:21 +01:00
|
|
|
|
|
|
|
public function testAddNonStmt() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage('Expected statement or expression node');
|
2017-01-19 22:39:21 +01:00
|
|
|
$this->createFunctionBuilder('test')
|
|
|
|
->addStmt(new Node\Name('Test'));
|
|
|
|
}
|
2015-03-20 21:47:20 +01:00
|
|
|
}
|