ClassConstTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Modifiers;
  5. use PhpParser\Node\Arg;
  6. use PhpParser\Node\Attribute;
  7. use PhpParser\Node\AttributeGroup;
  8. use PhpParser\Node\Const_;
  9. use PhpParser\Node\Expr;
  10. use PhpParser\Node\Identifier;
  11. use PhpParser\Node\Name;
  12. use PhpParser\Node\Scalar;
  13. use PhpParser\Node\Scalar\Int_;
  14. use PhpParser\Node\Stmt;
  15. class ClassConstTest extends \PHPUnit\Framework\TestCase {
  16. public function createClassConstBuilder($name, $value) {
  17. return new ClassConst($name, $value);
  18. }
  19. public function testModifiers(): void {
  20. $node = $this->createClassConstBuilder("TEST", 1)
  21. ->makePrivate()
  22. ->getNode()
  23. ;
  24. $this->assertEquals(
  25. new Stmt\ClassConst(
  26. [
  27. new Const_("TEST", new Int_(1))
  28. ],
  29. Modifiers::PRIVATE
  30. ),
  31. $node
  32. );
  33. $node = $this->createClassConstBuilder("TEST", 1)
  34. ->makeProtected()
  35. ->getNode()
  36. ;
  37. $this->assertEquals(
  38. new Stmt\ClassConst(
  39. [
  40. new Const_("TEST", new Int_(1))
  41. ],
  42. Modifiers::PROTECTED
  43. ),
  44. $node
  45. );
  46. $node = $this->createClassConstBuilder("TEST", 1)
  47. ->makePublic()
  48. ->getNode()
  49. ;
  50. $this->assertEquals(
  51. new Stmt\ClassConst(
  52. [
  53. new Const_("TEST", new Int_(1))
  54. ],
  55. Modifiers::PUBLIC
  56. ),
  57. $node
  58. );
  59. $node = $this->createClassConstBuilder("TEST", 1)
  60. ->makeFinal()
  61. ->getNode()
  62. ;
  63. $this->assertEquals(
  64. new Stmt\ClassConst(
  65. [
  66. new Const_("TEST", new Int_(1))
  67. ],
  68. Modifiers::FINAL
  69. ),
  70. $node
  71. );
  72. }
  73. public function testDocComment(): void {
  74. $node = $this->createClassConstBuilder('TEST', 1)
  75. ->setDocComment('/** Test */')
  76. ->makePublic()
  77. ->getNode();
  78. $this->assertEquals(
  79. new Stmt\ClassConst(
  80. [
  81. new Const_("TEST", new Int_(1))
  82. ],
  83. Modifiers::PUBLIC,
  84. [
  85. 'comments' => [new Comment\Doc('/** Test */')]
  86. ]
  87. ),
  88. $node
  89. );
  90. }
  91. public function testAddConst(): void {
  92. $node = $this->createClassConstBuilder('FIRST_TEST', 1)
  93. ->addConst("SECOND_TEST", 2)
  94. ->getNode();
  95. $this->assertEquals(
  96. new Stmt\ClassConst(
  97. [
  98. new Const_("FIRST_TEST", new Int_(1)),
  99. new Const_("SECOND_TEST", new Int_(2))
  100. ]
  101. ),
  102. $node
  103. );
  104. }
  105. public function testAddAttribute(): void {
  106. $attribute = new Attribute(
  107. new Name('Attr'),
  108. [new Arg(new Int_(1), false, false, [], new Identifier('name'))]
  109. );
  110. $attributeGroup = new AttributeGroup([$attribute]);
  111. $node = $this->createClassConstBuilder('ATTR_GROUP', 1)
  112. ->addAttribute($attributeGroup)
  113. ->getNode();
  114. $this->assertEquals(
  115. new Stmt\ClassConst(
  116. [
  117. new Const_("ATTR_GROUP", new Int_(1))
  118. ],
  119. 0,
  120. [],
  121. [$attributeGroup]
  122. ),
  123. $node
  124. );
  125. }
  126. public function testType(): void {
  127. $node = $this->createClassConstBuilder('TYPE', 1)
  128. ->setType('int')
  129. ->getNode();
  130. $this->assertEquals(
  131. new Stmt\ClassConst(
  132. [new Const_('TYPE', new Int_(1))],
  133. 0, [], [], new Identifier('int')),
  134. $node
  135. );
  136. }
  137. /**
  138. * @dataProvider provideTestDefaultValues
  139. */
  140. public function testValues($value, $expectedValueNode): void {
  141. $node = $this->createClassConstBuilder('TEST', $value)
  142. ->getNode()
  143. ;
  144. $this->assertEquals($expectedValueNode, $node->consts[0]->value);
  145. }
  146. public static function provideTestDefaultValues() {
  147. return [
  148. [
  149. null,
  150. new Expr\ConstFetch(new Name('null'))
  151. ],
  152. [
  153. true,
  154. new Expr\ConstFetch(new Name('true'))
  155. ],
  156. [
  157. false,
  158. new Expr\ConstFetch(new Name('false'))
  159. ],
  160. [
  161. 31415,
  162. new Scalar\Int_(31415)
  163. ],
  164. [
  165. 3.1415,
  166. new Scalar\Float_(3.1415)
  167. ],
  168. [
  169. 'Hallo World',
  170. new Scalar\String_('Hallo World')
  171. ],
  172. [
  173. [1, 2, 3],
  174. new Expr\Array_([
  175. new \PhpParser\Node\ArrayItem(new Scalar\Int_(1)),
  176. new \PhpParser\Node\ArrayItem(new Scalar\Int_(2)),
  177. new \PhpParser\Node\ArrayItem(new Scalar\Int_(3)),
  178. ])
  179. ],
  180. [
  181. ['foo' => 'bar', 'bar' => 'foo'],
  182. new Expr\Array_([
  183. new \PhpParser\Node\ArrayItem(
  184. new Scalar\String_('bar'),
  185. new Scalar\String_('foo')
  186. ),
  187. new \PhpParser\Node\ArrayItem(
  188. new Scalar\String_('foo'),
  189. new Scalar\String_('bar')
  190. ),
  191. ])
  192. ],
  193. [
  194. new Scalar\MagicConst\Dir(),
  195. new Scalar\MagicConst\Dir()
  196. ]
  197. ];
  198. }
  199. }