TraitTest.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Name;
  6. use PhpParser\Node\Stmt;
  7. class TraitTest extends \PHPUnit_Framework_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, array(
  17. new Stmt\PropertyProperty('test')
  18. ));
  19. $trait = $this->createTraitBuilder('TestTrait')
  20. ->setDocComment('/** Nice trait */')
  21. ->addStmt($method1)
  22. ->addStmts(array($method2, $method3))
  23. ->addStmt($prop)
  24. ->getNode();
  25. $this->assertEquals(new Stmt\Trait_('TestTrait', array(
  26. $prop, $method1, $method2, $method3
  27. ), array(
  28. 'comments' => array(
  29. new Comment\Doc('/** Nice trait */')
  30. )
  31. )), $trait);
  32. }
  33. /**
  34. * @expectedException \LogicException
  35. * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
  36. */
  37. public function testInvalidStmtError() {
  38. $this->createTraitBuilder('Test')
  39. ->addStmt(new Stmt\Echo_(array()))
  40. ;
  41. }
  42. }