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