TraitUseTest.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. {
  7. protected function createTraitUseBuilder(...$traits) {
  8. return new TraitUse(...$traits);
  9. }
  10. public function testAnd() {
  11. $node = $this->createTraitUseBuilder('SomeTrait')
  12. ->and('AnotherTrait')
  13. ->getNode()
  14. ;
  15. $this->assertEquals(
  16. new Stmt\TraitUse([
  17. new Name('SomeTrait'),
  18. new Name('AnotherTrait')
  19. ]),
  20. $node
  21. );
  22. }
  23. public function testWith() {
  24. $node = $this->createTraitUseBuilder('SomeTrait')
  25. ->with(new Stmt\TraitUseAdaptation\Alias(null, 'foo', null, 'bar'))
  26. ->with((new TraitUseAdaptation(null, 'test'))->as('baz'))
  27. ->getNode()
  28. ;
  29. $this->assertEquals(
  30. new Stmt\TraitUse([new Name('SomeTrait')], [
  31. new Stmt\TraitUseAdaptation\Alias(null, 'foo', null, 'bar'),
  32. new Stmt\TraitUseAdaptation\Alias(null, 'test', null, 'baz')
  33. ]),
  34. $node
  35. );
  36. }
  37. public function testInvalidAdaptationNode() {
  38. $this->expectException(\LogicException::class);
  39. $this->expectExceptionMessage('Adaptation must have type TraitUseAdaptation');
  40. $this->createTraitUseBuilder('Test')
  41. ->with(new Stmt\Echo_([]))
  42. ;
  43. }
  44. }