TraitTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Identifier;
  10. use PhpParser\Node\Name;
  11. use PhpParser\Node\Scalar\Int_;
  12. use PhpParser\Node\Stmt;
  13. use PhpParser\Node\Stmt\ClassConst;
  14. use PhpParser\Node\Stmt\ClassMethod;
  15. use PhpParser\Node\Stmt\Property;
  16. use PhpParser\Node\PropertyItem;
  17. use PhpParser\Node\Stmt\TraitUse;
  18. class TraitTest extends \PHPUnit\Framework\TestCase {
  19. protected function createTraitBuilder($class) {
  20. return new Trait_($class);
  21. }
  22. public function testStmtAddition(): void {
  23. $method1 = new Stmt\ClassMethod('test1');
  24. $method2 = new Stmt\ClassMethod('test2');
  25. $method3 = new Stmt\ClassMethod('test3');
  26. $prop = new Stmt\Property(Modifiers::PUBLIC, [
  27. new PropertyItem('test')
  28. ]);
  29. $const = new ClassConst([new Const_('FOO', new Int_(0))]);
  30. $use = new Stmt\TraitUse([new Name('OtherTrait')]);
  31. $trait = $this->createTraitBuilder('TestTrait')
  32. ->setDocComment('/** Nice trait */')
  33. ->addStmt($method1)
  34. ->addStmts([$method2, $method3])
  35. ->addStmt($prop)
  36. ->addStmt($use)
  37. ->addStmt($const)
  38. ->getNode();
  39. $this->assertEquals(new Stmt\Trait_('TestTrait', [
  40. 'stmts' => [$use, $const, $prop, $method1, $method2, $method3]
  41. ], [
  42. 'comments' => [
  43. new Comment\Doc('/** Nice trait */')
  44. ]
  45. ]), $trait);
  46. }
  47. public function testInvalidStmtError(): void {
  48. $this->expectException(\LogicException::class);
  49. $this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
  50. $this->createTraitBuilder('Test')
  51. ->addStmt(new Stmt\Echo_([]))
  52. ;
  53. }
  54. public function testGetMethods(): void {
  55. $methods = [
  56. new ClassMethod('foo'),
  57. new ClassMethod('bar'),
  58. new ClassMethod('fooBar'),
  59. ];
  60. $trait = new Stmt\Trait_('Foo', [
  61. 'stmts' => [
  62. new TraitUse([]),
  63. $methods[0],
  64. new ClassConst([]),
  65. $methods[1],
  66. new Property(0, []),
  67. $methods[2],
  68. ]
  69. ]);
  70. $this->assertSame($methods, $trait->getMethods());
  71. }
  72. public function testGetProperties(): void {
  73. $properties = [
  74. new Property(Modifiers::PUBLIC, [new PropertyItem('foo')]),
  75. new Property(Modifiers::PUBLIC, [new PropertyItem('bar')]),
  76. ];
  77. $trait = new Stmt\Trait_('Foo', [
  78. 'stmts' => [
  79. new TraitUse([]),
  80. $properties[0],
  81. new ClassConst([]),
  82. $properties[1],
  83. new ClassMethod('fooBar'),
  84. ]
  85. ]);
  86. $this->assertSame($properties, $trait->getProperties());
  87. }
  88. public function testAddAttribute(): void {
  89. $attribute = new Attribute(
  90. new Name('Attr'),
  91. [new Arg(new Int_(1), false, false, [], new Identifier('name'))]
  92. );
  93. $attributeGroup = new AttributeGroup([$attribute]);
  94. $node = $this->createTraitBuilder('AttributeGroup')
  95. ->addAttribute($attributeGroup)
  96. ->getNode()
  97. ;
  98. $this->assertEquals(
  99. new Stmt\Trait_(
  100. 'AttributeGroup',
  101. [
  102. 'attrGroups' => [$attributeGroup],
  103. ]
  104. ),
  105. $node
  106. );
  107. }
  108. }