TraitTest.php 1.3 KB

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