MethodTest.php 5.2 KB

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