FunctionTest.php 3.9 KB

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