php-parser/test/PhpParser/Builder/NamespaceTest.php

48 lines
1.4 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2014-12-11 17:24:10 +01:00
namespace PhpParser\Builder;
use PhpParser\Comment\Doc;
2014-12-11 17:24:10 +01:00
use PhpParser\Node;
use PhpParser\Node\Stmt;
2017-04-27 18:14:07 +02:00
use PHPUnit\Framework\TestCase;
2014-12-11 17:24:10 +01:00
2017-04-27 18:14:07 +02:00
class NamespaceTest extends TestCase
2014-12-11 17:24:10 +01:00
{
protected function createNamespaceBuilder($fqn) {
return new Namespace_($fqn);
}
public function testCreation() {
$stmt1 = new Stmt\Class_('SomeClass');
$stmt2 = new Stmt\Interface_('SomeInterface');
$stmt3 = new Stmt\Function_('someFunction');
$docComment = new Doc('/** Test */');
2014-12-11 17:24:10 +01:00
$expected = new Stmt\Namespace_(
2014-12-19 18:35:19 +01:00
new Node\Name('Name\Space'),
[$stmt1, $stmt2, $stmt3],
['comments' => [$docComment]]
2014-12-11 17:24:10 +01:00
);
2014-12-19 18:35:19 +01:00
$node = $this->createNamespaceBuilder('Name\Space')
2014-12-11 17:24:10 +01:00
->addStmt($stmt1)
2017-08-13 14:35:03 +02:00
->addStmts([$stmt2, $stmt3])
->setDocComment($docComment)
2014-12-11 17:24:10 +01:00
->getNode()
;
$this->assertEquals($expected, $node);
2017-08-13 14:35:03 +02:00
$node = $this->createNamespaceBuilder(new Node\Name(['Name', 'Space']))
->setDocComment($docComment)
2017-08-13 14:35:03 +02:00
->addStmts([$stmt1, $stmt2])
2014-12-11 17:24:10 +01:00
->addStmt($stmt3)
->getNode()
;
$this->assertEquals($expected, $node);
$node = $this->createNamespaceBuilder(null)->getNode();
$this->assertNull($node->name);
$this->assertEmpty($node->stmts);
2014-12-11 17:24:10 +01:00
}
}