EnumTest.php 4.4 KB

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