TraitTest.php 1.4 KB

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