FunctionTest.php 4.0 KB

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