2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-10 23:25:26 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
2016-11-23 22:58:18 +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_;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2017-04-27 18:14:07 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2017-04-27 18:14:07 +02:00
|
|
|
class MethodTest extends TestCase
|
2012-03-10 23:25:26 +01:00
|
|
|
{
|
|
|
|
public function createMethodBuilder($name) {
|
2014-02-06 14:44:16 +01:00
|
|
|
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', [
|
2016-09-16 14:38:31 +02:00
|
|
|
'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', [
|
2016-09-16 14:38:31 +02:00
|
|
|
'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', [
|
2014-02-06 14:44:16 +01:00
|
|
|
'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() {
|
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-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' => [
|
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-10 23:25:26 +01:00
|
|
|
$node
|
|
|
|
);
|
|
|
|
}
|
2014-12-13 13:44:40 +01:00
|
|
|
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);
|
2014-12-13 13:44:40 +01:00
|
|
|
}
|
2012-03-10 23:25:26 +01:00
|
|
|
|
2016-04-09 11:41:38 +02: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', [
|
2016-04-09 11:41:38 +02:00
|
|
|
'returnType' => 'bool'
|
2017-08-13 14:35:03 +02:00
|
|
|
], []), $node);
|
2016-04-09 11:41:38 +02:00
|
|
|
}
|
|
|
|
|
2012-03-10 23:25:26 +01:00
|
|
|
/**
|
2014-02-06 14:44:16 +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()
|
2015-03-20 21:47:20 +01:00
|
|
|
->addStmt(new Print_(new String_('test')))
|
2012-03-10 23:25:26 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-06 14:44:16 +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')
|
2015-03-20 21:47:20 +01:00
|
|
|
->addStmt(new Print_(new String_('test')))
|
2012-03-10 23:25:26 +01:00
|
|
|
->makeAbstract()
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-06 14:44:16 +01:00
|
|
|
* @expectedException \LogicException
|
2012-03-10 23:25:26 +01:00
|
|
|
* @expectedExceptionMessage Expected parameter node, got "Name"
|
|
|
|
*/
|
|
|
|
public function testInvalidParamError() {
|
|
|
|
$this->createMethodBuilder('test')
|
2014-02-06 14:44:16 +01:00
|
|
|
->addParam(new Node\Name('foo'))
|
2012-03-10 23:25:26 +01:00
|
|
|
;
|
|
|
|
}
|
2015-03-02 11:33:41 +01:00
|
|
|
}
|