1 <?php declare(strict_types=1);
3 namespace PhpParser\Builder;
6 use PhpParser\Node\Name;
7 use PhpParser\Node\Stmt;
8 use PHPUnit\Framework\TestCase;
10 class TraitTest extends TestCase
12 protected function createTraitBuilder($class) {
13 return new Trait_($class);
16 public function testStmtAddition() {
17 $method1 = new Stmt\ClassMethod('test1');
18 $method2 = new Stmt\ClassMethod('test2');
19 $method3 = new Stmt\ClassMethod('test3');
20 $prop = new Stmt\Property(Stmt\Class_::MODIFIER_PUBLIC, [
21 new Stmt\PropertyProperty('test')
23 $use = new Stmt\TraitUse([new Name('OtherTrait')]);
24 $trait = $this->createTraitBuilder('TestTrait')
25 ->setDocComment('/** Nice trait */')
27 ->addStmts([$method2, $method3])
31 $this->assertEquals(new Stmt\Trait_('TestTrait', [
32 'stmts' => [$use, $prop, $method1, $method2, $method3]
35 new Comment\Doc('/** Nice trait */')
40 public function testInvalidStmtError() {
41 $this->expectException(\LogicException::class);
42 $this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
43 $this->createTraitBuilder('Test')
44 ->addStmt(new Stmt\Echo_([]))