TraitUseTest.php 1.5 KB

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