1 <?php declare(strict_types=1);
3 namespace PhpParser\Builder;
7 use PhpParser\Node\Scalar\DNumber;
8 use PhpParser\Node\Stmt;
9 use PHPUnit\Framework\TestCase;
11 class InterfaceTest extends TestCase
13 /** @var Interface_ */
16 protected function setUp() {
17 $this->builder = new Interface_('Contract');
20 private function dump($node) {
21 $pp = new \PhpParser\PrettyPrinter\Standard;
22 return $pp->prettyPrint([$node]);
25 public function testEmpty() {
26 $contract = $this->builder->getNode();
27 $this->assertInstanceOf(Stmt\Interface_::class, $contract);
28 $this->assertEquals(new Node\Identifier('Contract'), $contract->name);
31 public function testExtending() {
32 $contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
34 new Stmt\Interface_('Contract', [
36 new Node\Name('Space\Root1'),
37 new Node\Name('Root2')
43 public function testAddMethod() {
44 $method = new Stmt\ClassMethod('doSomething');
45 $contract = $this->builder->addStmt($method)->getNode();
46 $this->assertSame([$method], $contract->stmts);
49 public function testAddConst() {
50 $const = new Stmt\ClassConst([
51 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
53 $contract = $this->builder->addStmt($const)->getNode();
54 $this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
57 public function testOrder() {
58 $const = new Stmt\ClassConst([
59 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
61 $method = new Stmt\ClassMethod('doSomething');
62 $contract = $this->builder
68 $this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
69 $this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
72 public function testDocComment() {
73 $node = $this->builder
74 ->setDocComment('/** Test */')
77 $this->assertEquals(new Stmt\Interface_('Contract', [], [
78 'comments' => [new Comment\Doc('/** Test */')]
82 public function testInvalidStmtError() {
83 $this->expectException(\LogicException::class);
84 $this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
85 $this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
88 public function testFullFunctional() {
89 $const = new Stmt\ClassConst([
90 new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
92 $method = new Stmt\ClassMethod('doSomething');
93 $contract = $this->builder
99 eval($this->dump($contract));
101 $this->assertTrue(interface_exists('Contract', false));