1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-11 17:09:41 +01:00
PHP-Parser/test/PhpParser/Builder/FunctionTest.php

121 lines
3.2 KiB
PHP
Raw Normal View History

2012-03-11 09:02:52 +01:00
<?php
namespace PhpParser\Builder;
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_;
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(
new Stmt\Function_('test', array(
2012-03-11 09:02:52 +01:00
'byRef' => true
)),
$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)
->addParams(array($param2, $param3))
->getNode()
;
$this->assertEquals(
new Stmt\Function_('test', array(
2012-03-11 09:02:52 +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-11 09:02:52 +01:00
$node = $this->createFunctionBuilder('test')
->addStmt($stmt1)
->addStmts(array($stmt2, $stmt3))
->getNode()
;
$this->assertEquals(
new Stmt\Function_('test', array(
'stmts' => array(
new Stmt\Expression($stmt1),
new Stmt\Expression($stmt2),
new Stmt\Expression($stmt3),
)
2012-03-11 09:02:52 +01:00
)),
$node
);
}
public function testDocComment() {
$node = $this->createFunctionBuilder('test')
->setDocComment('/** Test */')
->getNode();
$this->assertEquals(new Stmt\Function_('test', array(), array(
'comments' => array(new Comment\Doc('/** Test */'))
)), $node);
}
public function testReturnType() {
$node = $this->createFunctionBuilder('test')
->setReturnType('void')
->getNode();
$this->assertEquals(new Stmt\Function_('test', array(
'returnType' => 'void'
), array()), $node);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage void type cannot be nullable
*/
public function testInvalidNullableVoidType() {
$this->createFunctionBuilder('test')->setReturnType('?void');
}
2012-03-11 09:02:52 +01:00
/**
* @expectedException \LogicException
2012-03-11 09:02:52 +01:00
* @expectedExceptionMessage Expected parameter node, got "Name"
*/
public function testInvalidParamError() {
$this->createFunctionBuilder('test')
->addParam(new Node\Name('foo'))
2012-03-11 09:02:52 +01:00
;
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Expected statement or expression node
*/
public function testAddNonStmt() {
$this->createFunctionBuilder('test')
->addStmt(new Node\Name('Test'));
}
}