TraitUseTest.php 1.5 KB

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