PropertyTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. class PHPParser_Tests_Builder_PropertyTest extends PHPUnit_Framework_TestCase
  3. {
  4. public function createPropertyBuilder($name) {
  5. return new PHPParser_Builder_Property($name);
  6. }
  7. public function testModifiers() {
  8. $node = $this->createPropertyBuilder('test')
  9. ->makePrivate()
  10. ->makeStatic()
  11. ->getNode()
  12. ;
  13. $this->assertEquals(
  14. new PHPParser_Node_Stmt_Property(
  15. PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE
  16. | PHPParser_Node_Stmt_Class::MODIFIER_STATIC,
  17. array(
  18. new PHPParser_Node_Stmt_PropertyProperty('test')
  19. )
  20. ),
  21. $node
  22. );
  23. $node = $this->createPropertyBuilder('test')
  24. ->makeProtected()
  25. ->getNode()
  26. ;
  27. $this->assertEquals(
  28. new PHPParser_Node_Stmt_Property(
  29. PHPParser_Node_Stmt_Class::MODIFIER_PROTECTED,
  30. array(
  31. new PHPParser_Node_Stmt_PropertyProperty('test')
  32. )
  33. ),
  34. $node
  35. );
  36. $node = $this->createPropertyBuilder('test')
  37. ->makePublic()
  38. ->getNode()
  39. ;
  40. $this->assertEquals(
  41. new PHPParser_Node_Stmt_Property(
  42. PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
  43. array(
  44. new PHPParser_Node_Stmt_PropertyProperty('test')
  45. )
  46. ),
  47. $node
  48. );
  49. }
  50. /**
  51. * @dataProvider provideTestDefaultValues
  52. */
  53. public function testDefaultValues($value, $expectedValueNode) {
  54. $node = $this->createPropertyBuilder('test')
  55. ->setDefault($value)
  56. ->getNode()
  57. ;
  58. $this->assertEquals($expectedValueNode, $node->props[0]->default);
  59. }
  60. public function provideTestDefaultValues() {
  61. return array(
  62. array(
  63. null,
  64. new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('null'))
  65. ),
  66. array(
  67. true,
  68. new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('true'))
  69. ),
  70. array(
  71. false,
  72. new PHPParser_Node_Expr_ConstFetch(new PHPParser_Node_Name('false'))
  73. ),
  74. array(
  75. 31415,
  76. new PHPParser_Node_Scalar_LNumber(31415)
  77. ),
  78. array(
  79. 3.1415,
  80. new PHPParser_Node_Scalar_DNumber(3.1415)
  81. ),
  82. array(
  83. 'Hallo World',
  84. new PHPParser_Node_Scalar_String('Hallo World')
  85. ),
  86. array(
  87. array(1, 2, 3),
  88. new PHPParser_Node_Expr_Array(array(
  89. new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(1)),
  90. new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(2)),
  91. new PHPParser_Node_Expr_ArrayItem(new PHPParser_Node_Scalar_LNumber(3)),
  92. ))
  93. ),
  94. array(
  95. array('foo' => 'bar', 'bar' => 'foo'),
  96. new PHPParser_Node_Expr_Array(array(
  97. new PHPParser_Node_Expr_ArrayItem(
  98. new PHPParser_Node_Scalar_String('bar'),
  99. new PHPParser_Node_Scalar_String('foo')
  100. ),
  101. new PHPParser_Node_Expr_ArrayItem(
  102. new PHPParser_Node_Scalar_String('foo'),
  103. new PHPParser_Node_Scalar_String('bar')
  104. ),
  105. ))
  106. ),
  107. array(
  108. new PHPParser_Node_Scalar_DirConst,
  109. new PHPParser_Node_Scalar_DirConst
  110. )
  111. );
  112. }
  113. }