TraitTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node\Name;
  5. use PhpParser\Node\Stmt;
  6. class TraitTest extends \PHPUnit\Framework\TestCase
  7. {
  8. protected function createTraitBuilder($class) {
  9. return new Trait_($class);
  10. }
  11. public function testStmtAddition() {
  12. $method1 = new Stmt\ClassMethod('test1');
  13. $method2 = new Stmt\ClassMethod('test2');
  14. $method3 = new Stmt\ClassMethod('test3');
  15. $prop = new Stmt\Property(Stmt\Class_::MODIFIER_PUBLIC, [
  16. new Stmt\PropertyProperty('test')
  17. ]);
  18. $use = new Stmt\TraitUse([new Name('OtherTrait')]);
  19. $trait = $this->createTraitBuilder('TestTrait')
  20. ->setDocComment('/** Nice trait */')
  21. ->addStmt($method1)
  22. ->addStmts([$method2, $method3])
  23. ->addStmt($prop)
  24. ->addStmt($use)
  25. ->getNode();
  26. $this->assertEquals(new Stmt\Trait_('TestTrait', [
  27. 'stmts' => [$use, $prop, $method1, $method2, $method3]
  28. ], [
  29. 'comments' => [
  30. new Comment\Doc('/** Nice trait */')
  31. ]
  32. ]), $trait);
  33. }
  34. public function testInvalidStmtError() {
  35. $this->expectException(\LogicException::class);
  36. $this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
  37. $this->createTraitBuilder('Test')
  38. ->addStmt(new Stmt\Echo_([]))
  39. ;
  40. }
  41. }