TraitTest.php 3.5 KB

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