菜谱项目

TraitTest.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  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, array(
  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. /**
  35. * @expectedException \LogicException
  36. * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
  37. */
  38. public function testInvalidStmtError() {
  39. $this->createTraitBuilder('Test')
  40. ->addStmt(new Stmt\Echo_(array()))
  41. ;
  42. }
  43. }