FunctionTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Expr\Print_;
  6. use PhpParser\Node\Expr\Variable;
  7. use PhpParser\Node\Scalar\String_;
  8. use PhpParser\Node\Stmt;
  9. class FunctionTest extends \PHPUnit\Framework\TestCase
  10. {
  11. public function createFunctionBuilder($name) {
  12. return new Function_($name);
  13. }
  14. public function testReturnByRef() {
  15. $node = $this->createFunctionBuilder('test')
  16. ->makeReturnByRef()
  17. ->getNode()
  18. ;
  19. $this->assertEquals(
  20. new Stmt\Function_('test', [
  21. 'byRef' => true
  22. ]),
  23. $node
  24. );
  25. }
  26. public function testParams() {
  27. $param1 = new Node\Param(new Variable('test1'));
  28. $param2 = new Node\Param(new Variable('test2'));
  29. $param3 = new Node\Param(new Variable('test3'));
  30. $node = $this->createFunctionBuilder('test')
  31. ->addParam($param1)
  32. ->addParams([$param2, $param3])
  33. ->getNode()
  34. ;
  35. $this->assertEquals(
  36. new Stmt\Function_('test', [
  37. 'params' => [$param1, $param2, $param3]
  38. ]),
  39. $node
  40. );
  41. }
  42. public function testStmts() {
  43. $stmt1 = new Print_(new String_('test1'));
  44. $stmt2 = new Print_(new String_('test2'));
  45. $stmt3 = new Print_(new String_('test3'));
  46. $node = $this->createFunctionBuilder('test')
  47. ->addStmt($stmt1)
  48. ->addStmts([$stmt2, $stmt3])
  49. ->getNode()
  50. ;
  51. $this->assertEquals(
  52. new Stmt\Function_('test', [
  53. 'stmts' => [
  54. new Stmt\Expression($stmt1),
  55. new Stmt\Expression($stmt2),
  56. new Stmt\Expression($stmt3),
  57. ]
  58. ]),
  59. $node
  60. );
  61. }
  62. public function testDocComment() {
  63. $node = $this->createFunctionBuilder('test')
  64. ->setDocComment('/** Test */')
  65. ->getNode();
  66. $this->assertEquals(new Stmt\Function_('test', [], [
  67. 'comments' => [new Comment\Doc('/** Test */')]
  68. ]), $node);
  69. }
  70. public function testReturnType() {
  71. $node = $this->createFunctionBuilder('test')
  72. ->setReturnType('void')
  73. ->getNode();
  74. $this->assertEquals(new Stmt\Function_('test', [
  75. 'returnType' => 'void'
  76. ], []), $node);
  77. }
  78. public function testInvalidNullableVoidType() {
  79. $this->expectException(\LogicException::class);
  80. $this->expectExceptionMessage('void type cannot be nullable');
  81. $this->createFunctionBuilder('test')->setReturnType('?void');
  82. }
  83. public function testInvalidParamError() {
  84. $this->expectException(\LogicException::class);
  85. $this->expectExceptionMessage('Expected parameter node, got "Name"');
  86. $this->createFunctionBuilder('test')
  87. ->addParam(new Node\Name('foo'))
  88. ;
  89. }
  90. public function testAddNonStmt() {
  91. $this->expectException(\LogicException::class);
  92. $this->expectExceptionMessage('Expected statement or expression node');
  93. $this->createFunctionBuilder('test')
  94. ->addStmt(new Node\Name('Test'));
  95. }
  96. }