1<?php declare(strict_types=1);
2
3namespace PhpParser\Builder;
4
5use PhpParser\Node\Name;
6use PhpParser\Node\Stmt;
7
8class TraitUseTest extends \PHPUnit\Framework\TestCase {
9    protected function createTraitUseBuilder(...$traits) {
10        return new TraitUse(...$traits);
11    }
12
13    public function testAnd(): void {
14        $node = $this->createTraitUseBuilder('SomeTrait')
15            ->and('AnotherTrait')
16            ->getNode()
17        ;
18
19        $this->assertEquals(
20            new Stmt\TraitUse([
21                new Name('SomeTrait'),
22                new Name('AnotherTrait')
23            ]),
24            $node
25        );
26    }
27
28    public function testWith(): void {
29        $node = $this->createTraitUseBuilder('SomeTrait')
30            ->with(new Stmt\TraitUseAdaptation\Alias(null, 'foo', null, 'bar'))
31            ->with((new TraitUseAdaptation(null, 'test'))->as('baz'))
32            ->getNode()
33        ;
34
35        $this->assertEquals(
36            new Stmt\TraitUse([new Name('SomeTrait')], [
37                new Stmt\TraitUseAdaptation\Alias(null, 'foo', null, 'bar'),
38                new Stmt\TraitUseAdaptation\Alias(null, 'test', null, 'baz')
39            ]),
40            $node
41        );
42    }
43
44    public function testInvalidAdaptationNode(): void {
45        $this->expectException(\LogicException::class);
46        $this->expectExceptionMessage('Adaptation must have type TraitUseAdaptation');
47        $this->createTraitUseBuilder('Test')
48            ->with(new Stmt\Echo_([]))
49        ;
50    }
51}
52