3ea3533c7ef4b2ce12071a73b02bd8fbb75dd281
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / Builder / InterfaceTest.php
1 <?php declare(strict_types=1);
2
3 namespace PhpParser\Builder;
4
5 use PhpParser\Comment;
6 use PhpParser\Node;
7 use PhpParser\Node\Scalar\DNumber;
8 use PhpParser\Node\Stmt;
9 use PHPUnit\Framework\TestCase;
10
11 class InterfaceTest extends TestCase
12 {
13     /** @var Interface_ */
14     protected $builder;
15
16     protected function setUp() {
17         $this->builder = new Interface_('Contract');
18     }
19
20     private function dump($node) {
21         $pp = new \PhpParser\PrettyPrinter\Standard;
22         return $pp->prettyPrint([$node]);
23     }
24
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);
29     }
30
31     public function testExtending() {
32         $contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
33         $this->assertEquals(
34             new Stmt\Interface_('Contract', [
35                 'extends' => [
36                     new Node\Name('Space\Root1'),
37                     new Node\Name('Root2')
38                 ],
39             ]), $contract
40         );
41     }
42
43     public function testAddMethod() {
44         $method = new Stmt\ClassMethod('doSomething');
45         $contract = $this->builder->addStmt($method)->getNode();
46         $this->assertSame([$method], $contract->stmts);
47     }
48
49     public function testAddConst() {
50         $const = new Stmt\ClassConst([
51             new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
52         ]);
53         $contract = $this->builder->addStmt($const)->getNode();
54         $this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
55     }
56
57     public function testOrder() {
58         $const = new Stmt\ClassConst([
59             new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
60         ]);
61         $method = new Stmt\ClassMethod('doSomething');
62         $contract = $this->builder
63             ->addStmt($method)
64             ->addStmt($const)
65             ->getNode()
66         ;
67
68         $this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
69         $this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
70     }
71
72     public function testDocComment() {
73         $node = $this->builder
74             ->setDocComment('/** Test */')
75             ->getNode();
76
77         $this->assertEquals(new Stmt\Interface_('Contract', [], [
78             'comments' => [new Comment\Doc('/** Test */')]
79         ]), $node);
80     }
81
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'));
86     }
87
88     public function testFullFunctional() {
89         $const = new Stmt\ClassConst([
90             new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
91         ]);
92         $method = new Stmt\ClassMethod('doSomething');
93         $contract = $this->builder
94             ->addStmt($method)
95             ->addStmt($const)
96             ->getNode()
97         ;
98
99         eval($this->dump($contract));
100
101         $this->assertTrue(interface_exists('Contract', false));
102     }
103 }