NamespaceTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php declare(strict_types=1);
  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. [$stmt1, $stmt2, $stmt3],
  19. ['comments' => [$docComment]]
  20. );
  21. $node = $this->createNamespaceBuilder('Name\Space')
  22. ->addStmt($stmt1)
  23. ->addStmts([$stmt2, $stmt3])
  24. ->setDocComment($docComment)
  25. ->getNode()
  26. ;
  27. $this->assertEquals($expected, $node);
  28. $node = $this->createNamespaceBuilder(new Node\Name(['Name', 'Space']))
  29. ->setDocComment($docComment)
  30. ->addStmts([$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. }