NamespaceTest.php 1.2 KB

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