MethodTest.php 4.5 KB

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