菜谱项目

NamespaceTest.php 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment\Doc;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Stmt;
  6. class NamespaceTest extends \PHPUnit_Framework_TestCase
  7. {
  8. protected function createNamespaceBuilder($fqn) {
  9. return new Namespace_($fqn);
  10. }
  11. public function testCreation() {
  12. $stmt1 = new Stmt\Class_('SomeClass');
  13. $stmt2 = new Stmt\Interface_('SomeInterface');
  14. $stmt3 = new Stmt\Function_('someFunction');
  15. $docComment = new Doc('/** Test */');
  16. $expected = new Stmt\Namespace_(
  17. new Node\Name('Name\Space'),
  18. array($stmt1, $stmt2, $stmt3),
  19. array('comments' => array($docComment))
  20. );
  21. $node = $this->createNamespaceBuilder('Name\Space')
  22. ->addStmt($stmt1)
  23. ->addStmts(array($stmt2, $stmt3))
  24. ->setDocComment($docComment)
  25. ->getNode()
  26. ;
  27. $this->assertEquals($expected, $node);
  28. $node = $this->createNamespaceBuilder(new Node\Name(array('Name', 'Space')))
  29. ->setDocComment($docComment)
  30. ->addStmts(array($stmt1, $stmt2))
  31. ->addStmt($stmt3)
  32. ->getNode()
  33. ;
  34. $this->assertEquals($expected, $node);
  35. $node = $this->createNamespaceBuilder(null)->getNode();
  36. $this->assertNull($node->name);
  37. $this->assertEmpty($node->stmts);
  38. }
  39. }