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