FunctionTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. class PHPParser_Tests_Builder_FunctionTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function createFunctionBuilder($name) {
  5. return new PHPParser_Builder_Function($name);
  6. }
  7. public function testReturnByRef() {
  8. $node = $this->createFunctionBuilder('test')
  9. ->makeReturnByRef()
  10. ->getNode()
  11. ;
  12. $this->assertEquals(
  13. new PHPParser_Node_Stmt_Function('test', array(
  14. 'byRef' => true
  15. )),
  16. $node
  17. );
  18. }
  19. public function testParams() {
  20. $param1 = new PHPParser_Node_Param('test1');
  21. $param2 = new PHPParser_Node_Param('test2');
  22. $param3 = new PHPParser_Node_Param('test3');
  23. $node = $this->createFunctionBuilder('test')
  24. ->addParam($param1)
  25. ->addParams(array($param2, $param3))
  26. ->getNode()
  27. ;
  28. $this->assertEquals(
  29. new PHPParser_Node_Stmt_Function('test', array(
  30. 'params' => array($param1, $param2, $param3)
  31. )),
  32. $node
  33. );
  34. }
  35. public function testStmts() {
  36. $stmt1 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test1'));
  37. $stmt2 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test2'));
  38. $stmt3 = new PHPParser_Node_Expr_Print(new PHPParser_Node_Scalar_String('test3'));
  39. $node = $this->createFunctionBuilder('test')
  40. ->addStmt($stmt1)
  41. ->addStmts(array($stmt2, $stmt3))
  42. ->getNode()
  43. ;
  44. $this->assertEquals(
  45. new PHPParser_Node_Stmt_Function('test', array(
  46. 'stmts' => array($stmt1, $stmt2, $stmt3)
  47. )),
  48. $node
  49. );
  50. }
  51. /**
  52. * @expectedException LogicException
  53. * @expectedExceptionMessage Expected parameter node, got "Name"
  54. */
  55. public function testInvalidParamError() {
  56. $this->createFunctionBuilder('test')
  57. ->addParam(new PHPParser_Node_Name('foo'))
  58. ;
  59. }
  60. }