ParamTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. class PHPParser_Tests_Builder_ParamTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function createParamBuilder($name) {
  5. return new PHPParser_Builder_Param($name);
  6. }
  7. /**
  8. * @dataProvider provideTestDefaultValues
  9. */
  10. public function testDefaultValues($value, $expectedValueNode) {
  11. $node = $this->createParamBuilder('test')
  12. ->setDefault($value)
  13. ->getNode()
  14. ;
  15. $this->assertEquals($expectedValueNode, $node->default);
  16. }
  17. public function provideTestDefaultValues() {
  18. return array(
  19. array(
  20. null,
  21. new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('null'))
  22. ),
  23. array(
  24. true,
  25. new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('true'))
  26. ),
  27. array(
  28. false,
  29. new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('false'))
  30. ),
  31. array(
  32. 31415,
  33. new PHPParser_Node_Scalar_LNumber(31415)
  34. ),
  35. array(
  36. 3.1415,
  37. new PHPParser_Node_Scalar_DNumber(3.1415)
  38. ),
  39. array(
  40. 'Hallo World',
  41. new PHPParser_Node_Scalar_String('Hallo World')
  42. ),
  43. array(
  44. array(1, 2, 3),
  45. new PHPParser_Node_Expr_Array(array(
  46. new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(1)),
  47. new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(2)),
  48. new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(3)),
  49. ))
  50. ),
  51. array(
  52. array('foo' => 'bar', 'bar' => 'foo'),
  53. new PHPParser_Node_Expr_Array(array(
  54. new PHPParser_Node_Expr_ArrayItem(
  55. new PHPParser_Node_Scalar_String('bar'),
  56. new PHPParser_Node_Scalar_String('foo')
  57. ),
  58. new PHPParser_Node_Expr_ArrayItem(
  59. new PHPParser_Node_Scalar_String('foo'),
  60. new PHPParser_Node_Scalar_String('bar')
  61. ),
  62. ))
  63. ),
  64. array(
  65. new PHPParser_Node_Scalar_DirConst,
  66. new PHPParser_Node_Scalar_DirConst
  67. )
  68. );
  69. }
  70. public function testTypeHints() {
  71. $node = $this->createParamBuilder('test')
  72. ->setTypeHint('array')
  73. ->getNode()
  74. ;
  75. $this->assertEquals(
  76. new PHPParser_Node_Param('test', null, 'array'),
  77. $node
  78. );
  79. $node = $this->createParamBuilder('test')
  80. ->setTypeHint('callable')
  81. ->getNode()
  82. ;
  83. $this->assertEquals(
  84. new PHPParser_Node_Param('test', null, 'callable'),
  85. $node
  86. );
  87. $node = $this->createParamBuilder('test')
  88. ->setTypeHint('Some\Class')
  89. ->getNode()
  90. ;
  91. $this->assertEquals(
  92. new PHPParser_Node_Param('test', null, new PHPParser_Node_Name('Some\Class')),
  93. $node
  94. );
  95. }
  96. public function testByRef() {
  97. $node = $this->createParamBuilder('test')
  98. ->makeByRef()
  99. ->getNode()
  100. ;
  101. $this->assertEquals(
  102. new PHPParser_Node_Param('test', null, null, true),
  103. $node
  104. );
  105. }
  106. }