MethodTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. class PHPParser_Tests_Builder_MethodTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function createMethodBuilder($name) {
  5. return new PHPParser_Builder_Method($name);
  6. }
  7. public function testModifiers() {
  8. $node = $this->createMethodBuilder('test')
  9. ->makePublic()
  10. ->makeAbstract()
  11. ->makeStatic()
  12. ->getNode()
  13. ;
  14. $this->assertEquals(
  15. new PHPParser_Node_Stmt_ClassMethod('test', array(
  16. 'type' => PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC
  17. | PHPParser_Node_Stmt_Class::MODIFIER_ABSTRACT
  18. | PHPParser_Node_Stmt_Class::MODIFIER_STATIC,
  19. 'stmts' => null,
  20. )),
  21. $node
  22. );
  23. $node = $this->createMethodBuilder('test')
  24. ->makeProtected()
  25. ->makeFinal()
  26. ->getNode()
  27. ;
  28. $this->assertEquals(
  29. new PHPParser_Node_Stmt_ClassMethod('test', array(
  30. 'type' => PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED
  31. | PHPParser_Node_Stmt_Class::MODIFIER_FINAL
  32. )),
  33. $node
  34. );
  35. $node = $this->createMethodBuilder('test')
  36. ->makePrivate()
  37. ->getNode()
  38. ;
  39. $this->assertEquals(
  40. new PHPParser_Node_Stmt_ClassMethod('test', array(
  41. 'type' => PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE
  42. )),
  43. $node
  44. );
  45. }
  46. public function testReturnByRef() {
  47. $node = $this->createMethodBuilder('test')
  48. ->makeReturnByRef()
  49. ->getNode()
  50. ;
  51. $this->assertEquals(
  52. new PHPParser_Node_Stmt_ClassMethod('test', array(
  53. 'byRef' => true
  54. )),
  55. $node
  56. );
  57. }
  58. public function testParams() {
  59. $param1 = new PHPParser_Node_Param('test1');
  60. $param2 = new PHPParser_Node_Param('test2');
  61. $param3 = new PHPParser_Node_Param('test3');
  62. $node = $this->createMethodBuilder('test')
  63. ->addParam($param1)
  64. ->addParams(array($param2, $param3))
  65. ->getNode()
  66. ;
  67. $this->assertEquals(
  68. new PHPParser_Node_Stmt_ClassMethod('test', array(
  69. 'params' => array($param1, $param2, $param3)
  70. )),
  71. $node
  72. );
  73. }
  74. public function testStmts() {
  75. $stmt1 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test1'));
  76. $stmt2 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test2'));
  77. $stmt3 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test3'));
  78. $node = $this->createMethodBuilder('test')
  79. ->addStmt($stmt1)
  80. ->addStmts(array($stmt2, $stmt3))
  81. ->getNode()
  82. ;
  83. $this->assertEquals(
  84. new PHPParser_Node_Stmt_ClassMethod('test', array(
  85. 'stmts' => array($stmt1, $stmt2, $stmt3)
  86. )),
  87. $node
  88. );
  89. }
  90. /**
  91. * @expectedException LogicException
  92. * @expectedExceptionMessage Cannot add statements to an abstract method
  93. */
  94. public function testAddStmtToAbstractMethodError() {
  95. $this->createMethodBuilder('test')
  96. ->makeAbstract()
  97. ->addStmt(new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test')))
  98. ;
  99. }
  100. /**
  101. * @expectedException LogicException
  102. * @expectedExceptionMessage Cannot make method with statements abstract
  103. */
  104. public function testMakeMethodWithStmtsAbstractError() {
  105. $this->createMethodBuilder('test')
  106. ->addStmt(new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test')))
  107. ->makeAbstract()
  108. ;
  109. }
  110. /**
  111. * @expectedException LogicException
  112. * @expectedExceptionMessage Expected parameter node, got "Name"
  113. */
  114. public function testInvalidParamError() {
  115. $this->createMethodBuilder('test')
  116. ->addParam(new PHPParser_Node_Name('foo'))
  117. ;
  118. }
  119. }