FunctionTest.php 3.2 KB

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