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