EnumTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Arg;
  6. use PhpParser\Node\Attribute;
  7. use PhpParser\Node\AttributeGroup;
  8. use PhpParser\Node\Identifier;
  9. use PhpParser\Node\Name;
  10. use PhpParser\Node\Scalar\LNumber;
  11. use PhpParser\Node\Stmt;
  12. class EnumTest extends \PHPUnit\Framework\TestCase
  13. {
  14. protected function createEnumBuilder($class) {
  15. return new Enum_($class);
  16. }
  17. public function testImplements() {
  18. $node = $this->createEnumBuilder('SomeEnum')
  19. ->implement('Namespaced\SomeInterface', new Name('OtherInterface'))
  20. ->getNode()
  21. ;
  22. $this->assertEquals(
  23. new Stmt\Enum_('SomeEnum', [
  24. 'implements' => [
  25. new Name('Namespaced\SomeInterface'),
  26. new Name('OtherInterface'),
  27. ],
  28. ]),
  29. $node
  30. );
  31. }
  32. public function testSetScalarType() {
  33. $node = $this->createEnumBuilder('Test')
  34. ->setScalarType('int')
  35. ->getNode()
  36. ;
  37. $this->assertEquals(
  38. new Stmt\Enum_('Test', [
  39. 'scalarType' => new Identifier('int'),
  40. ]),
  41. $node
  42. );
  43. }
  44. public function testStatementOrder() {
  45. $method = new Stmt\ClassMethod('testMethod');
  46. $enumCase = new Stmt\EnumCase(
  47. 'TEST_ENUM_CASE'
  48. );
  49. $const = new Stmt\ClassConst([
  50. new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC'))
  51. ]);
  52. $use = new Stmt\TraitUse([new Name('SomeTrait')]);
  53. $node = $this->createEnumBuilder('Test')
  54. ->addStmt($method)
  55. ->addStmt($enumCase)
  56. ->addStmts([$const, $use])
  57. ->getNode()
  58. ;
  59. $this->assertEquals(
  60. new Stmt\Enum_('Test', [
  61. 'stmts' => [$use, $enumCase, $const, $method]
  62. ]),
  63. $node
  64. );
  65. }
  66. public function testDocComment() {
  67. $docComment = <<<'DOC'
  68. /**
  69. * Test
  70. */
  71. DOC;
  72. $enum = $this->createEnumBuilder('Test')
  73. ->setDocComment($docComment)
  74. ->getNode();
  75. $this->assertEquals(
  76. new Stmt\Enum_('Test', [], [
  77. 'comments' => [
  78. new Comment\Doc($docComment)
  79. ]
  80. ]),
  81. $enum
  82. );
  83. $enum = $this->createEnumBuilder('Test')
  84. ->setDocComment(new Comment\Doc($docComment))
  85. ->getNode();
  86. $this->assertEquals(
  87. new Stmt\Enum_('Test', [], [
  88. 'comments' => [
  89. new Comment\Doc($docComment)
  90. ]
  91. ]),
  92. $enum
  93. );
  94. }
  95. public function testAddAttribute() {
  96. $attribute = new Attribute(
  97. new Name('Attr'),
  98. [new Arg(new LNumber(1), false, false, [], new Identifier('name'))]
  99. );
  100. $attributeGroup = new AttributeGroup([$attribute]);
  101. $enum = $this->createEnumBuilder('ATTR_GROUP')
  102. ->addAttribute($attributeGroup)
  103. ->getNode();
  104. $this->assertEquals(
  105. new Stmt\Enum_('ATTR_GROUP', [
  106. 'attrGroups' => [
  107. $attributeGroup,
  108. ]
  109. ], []),
  110. $enum
  111. );
  112. }
  113. public function testInvalidStmtError() {
  114. $this->expectException(\LogicException::class);
  115. $this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
  116. $this->createEnumBuilder('Test')
  117. ->addStmt(new Stmt\PropertyProperty('property'))
  118. ;
  119. }
  120. public function testInvalidDocComment() {
  121. $this->expectException(\LogicException::class);
  122. $this->expectExceptionMessage('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
  123. $this->createEnumBuilder('Test')
  124. ->setDocComment(new Comment('Test'));
  125. }
  126. public function testEmptyName() {
  127. $this->expectException(\LogicException::class);
  128. $this->expectExceptionMessage('Name cannot be empty');
  129. $this->createEnumBuilder('Test')
  130. ->implement('');
  131. }
  132. public function testInvalidName() {
  133. $this->expectException(\LogicException::class);
  134. $this->expectExceptionMessage('Name must be a string or an instance of Node\Name');
  135. $this->createEnumBuilder('Test')
  136. ->implement(['Foo']);
  137. }
  138. }