MethodTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 MethodTest extends TestCase
  11. {
  12. public function createMethodBuilder($name) {
  13. return new Method($name);
  14. }
  15. public function testModifiers() {
  16. $node = $this->createMethodBuilder('test')
  17. ->makePublic()
  18. ->makeAbstract()
  19. ->makeStatic()
  20. ->getNode()
  21. ;
  22. $this->assertEquals(
  23. new Stmt\ClassMethod('test', [
  24. 'flags' => Stmt\Class_::MODIFIER_PUBLIC
  25. | Stmt\Class_::MODIFIER_ABSTRACT
  26. | Stmt\Class_::MODIFIER_STATIC,
  27. 'stmts' => null,
  28. ]),
  29. $node
  30. );
  31. $node = $this->createMethodBuilder('test')
  32. ->makeProtected()
  33. ->makeFinal()
  34. ->getNode()
  35. ;
  36. $this->assertEquals(
  37. new Stmt\ClassMethod('test', [
  38. 'flags' => Stmt\Class_::MODIFIER_PROTECTED
  39. | Stmt\Class_::MODIFIER_FINAL
  40. ]),
  41. $node
  42. );
  43. $node = $this->createMethodBuilder('test')
  44. ->makePrivate()
  45. ->getNode()
  46. ;
  47. $this->assertEquals(
  48. new Stmt\ClassMethod('test', [
  49. 'type' => Stmt\Class_::MODIFIER_PRIVATE
  50. ]),
  51. $node
  52. );
  53. }
  54. public function testReturnByRef() {
  55. $node = $this->createMethodBuilder('test')
  56. ->makeReturnByRef()
  57. ->getNode()
  58. ;
  59. $this->assertEquals(
  60. new Stmt\ClassMethod('test', [
  61. 'byRef' => true
  62. ]),
  63. $node
  64. );
  65. }
  66. public function testParams() {
  67. $param1 = new Node\Param(new Variable('test1'));
  68. $param2 = new Node\Param(new Variable('test2'));
  69. $param3 = new Node\Param(new Variable('test3'));
  70. $node = $this->createMethodBuilder('test')
  71. ->addParam($param1)
  72. ->addParams([$param2, $param3])
  73. ->getNode()
  74. ;
  75. $this->assertEquals(
  76. new Stmt\ClassMethod('test', [
  77. 'params' => [$param1, $param2, $param3]
  78. ]),
  79. $node
  80. );
  81. }
  82. public function testStmts() {
  83. $stmt1 = new Print_(new String_('test1'));
  84. $stmt2 = new Print_(new String_('test2'));
  85. $stmt3 = new Print_(new String_('test3'));
  86. $node = $this->createMethodBuilder('test')
  87. ->addStmt($stmt1)
  88. ->addStmts([$stmt2, $stmt3])
  89. ->getNode()
  90. ;
  91. $this->assertEquals(
  92. new Stmt\ClassMethod('test', [
  93. 'stmts' => [
  94. new Stmt\Expression($stmt1),
  95. new Stmt\Expression($stmt2),
  96. new Stmt\Expression($stmt3),
  97. ]
  98. ]),
  99. $node
  100. );
  101. }
  102. public function testDocComment() {
  103. $node = $this->createMethodBuilder('test')
  104. ->setDocComment('/** Test */')
  105. ->getNode();
  106. $this->assertEquals(new Stmt\ClassMethod('test', [], [
  107. 'comments' => [new Comment\Doc('/** Test */')]
  108. ]), $node);
  109. }
  110. public function testReturnType() {
  111. $node = $this->createMethodBuilder('test')
  112. ->setReturnType('bool')
  113. ->getNode();
  114. $this->assertEquals(new Stmt\ClassMethod('test', [
  115. 'returnType' => 'bool'
  116. ], []), $node);
  117. }
  118. public function testAddStmtToAbstractMethodError() {
  119. $this->expectException(\LogicException::class);
  120. $this->expectExceptionMessage('Cannot add statements to an abstract method');
  121. $this->createMethodBuilder('test')
  122. ->makeAbstract()
  123. ->addStmt(new Print_(new String_('test')))
  124. ;
  125. }
  126. public function testMakeMethodWithStmtsAbstractError() {
  127. $this->expectException(\LogicException::class);
  128. $this->expectExceptionMessage('Cannot make method with statements abstract');
  129. $this->createMethodBuilder('test')
  130. ->addStmt(new Print_(new String_('test')))
  131. ->makeAbstract()
  132. ;
  133. }
  134. public function testInvalidParamError() {
  135. $this->expectException(\LogicException::class);
  136. $this->expectExceptionMessage('Expected parameter node, got "Name"');
  137. $this->createMethodBuilder('test')
  138. ->addParam(new Node\Name('foo'))
  139. ;
  140. }
  141. }