MethodTest.php 5.2 KB

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