菜谱项目

MethodTest.php 4.4KB

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