菜谱项目

PropertyTest.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node\Expr;
  5. use PhpParser\Node\Name;
  6. use PhpParser\Node\Scalar;
  7. use PhpParser\Node\Stmt;
  8. class PropertyTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public function createPropertyBuilder($name) {
  11. return new Property($name);
  12. }
  13. public function testModifiers() {
  14. $node = $this->createPropertyBuilder('test')
  15. ->makePrivate()
  16. ->makeStatic()
  17. ->getNode()
  18. ;
  19. $this->assertEquals(
  20. new Stmt\Property(
  21. Stmt\Class_::MODIFIER_PRIVATE
  22. | Stmt\Class_::MODIFIER_STATIC,
  23. array(
  24. new Stmt\PropertyProperty('test')
  25. )
  26. ),
  27. $node
  28. );
  29. $node = $this->createPropertyBuilder('test')
  30. ->makeProtected()
  31. ->getNode()
  32. ;
  33. $this->assertEquals(
  34. new Stmt\Property(
  35. Stmt\Class_::MODIFIER_PROTECTED,
  36. array(
  37. new Stmt\PropertyProperty('test')
  38. )
  39. ),
  40. $node
  41. );
  42. $node = $this->createPropertyBuilder('test')
  43. ->makePublic()
  44. ->getNode()
  45. ;
  46. $this->assertEquals(
  47. new Stmt\Property(
  48. Stmt\Class_::MODIFIER_PUBLIC,
  49. array(
  50. new Stmt\PropertyProperty('test')
  51. )
  52. ),
  53. $node
  54. );
  55. }
  56. public function testDocComment() {
  57. $node = $this->createPropertyBuilder('test')
  58. ->setDocComment('/** Test */')
  59. ->getNode();
  60. $this->assertEquals(new Stmt\Property(
  61. Stmt\Class_::MODIFIER_PUBLIC,
  62. array(
  63. new Stmt\PropertyProperty('test')
  64. ),
  65. array(
  66. 'comments' => array(new Comment\Doc('/** Test */'))
  67. )
  68. ), $node);
  69. }
  70. /**
  71. * @dataProvider provideTestDefaultValues
  72. */
  73. public function testDefaultValues($value, $expectedValueNode) {
  74. $node = $this->createPropertyBuilder('test')
  75. ->setDefault($value)
  76. ->getNode()
  77. ;
  78. $this->assertEquals($expectedValueNode, $node->props[0]->default);
  79. }
  80. public function provideTestDefaultValues() {
  81. return array(
  82. array(
  83. null,
  84. new Expr\ConstFetch(new Name('null'))
  85. ),
  86. array(
  87. true,
  88. new Expr\ConstFetch(new Name('true'))
  89. ),
  90. array(
  91. false,
  92. new Expr\ConstFetch(new Name('false'))
  93. ),
  94. array(
  95. 31415,
  96. new Scalar\LNumber(31415)
  97. ),
  98. array(
  99. 3.1415,
  100. new Scalar\DNumber(3.1415)
  101. ),
  102. array(
  103. 'Hallo World',
  104. new Scalar\String_('Hallo World')
  105. ),
  106. array(
  107. array(1, 2, 3),
  108. new Expr\Array_(array(
  109. new Expr\ArrayItem(new Scalar\LNumber(1)),
  110. new Expr\ArrayItem(new Scalar\LNumber(2)),
  111. new Expr\ArrayItem(new Scalar\LNumber(3)),
  112. ))
  113. ),
  114. array(
  115. array('foo' => 'bar', 'bar' => 'foo'),
  116. new Expr\Array_(array(
  117. new Expr\ArrayItem(
  118. new Scalar\String_('bar'),
  119. new Scalar\String_('foo')
  120. ),
  121. new Expr\ArrayItem(
  122. new Scalar\String_('foo'),
  123. new Scalar\String_('bar')
  124. ),
  125. ))
  126. ),
  127. array(
  128. new Scalar\MagicConst\Dir,
  129. new Scalar\MagicConst\Dir
  130. )
  131. );
  132. }
  133. }