0f7418212ed87f9ea361fa7b6f2c80362245c1e6
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / Builder / ClassTest.php
1 <?php
2
3 namespace PhpParser\Builder;
4
5 use PhpParser\Comment;
6 use PhpParser\Node;
7 use PhpParser\Node\Name;
8 use PhpParser\Node\Stmt;
9
10 class ClassTest extends \PHPUnit_Framework_TestCase
11 {
12     protected function createClassBuilder($class) {
13         return new Class_($class);
14     }
15
16     public function testExtendsImplements() {
17         $node = $this->createClassBuilder('SomeLogger')
18             ->extend('BaseLogger')
19             ->implement('Namespaced\Logger', new Name('SomeInterface'))
20             ->implement('\Fully\Qualified', 'namespace\NamespaceRelative')
21             ->getNode()
22         ;
23
24         $this->assertEquals(
25             new Stmt\Class_('SomeLogger', array(
26                 'extends' => new Name('BaseLogger'),
27                 'implements' => array(
28                     new Name('Namespaced\Logger'),
29                     new Name('SomeInterface'),
30                     new Name\FullyQualified('Fully\Qualified'),
31                     new Name\Relative('NamespaceRelative'),
32                 ),
33             )),
34             $node
35         );
36     }
37
38     public function testAbstract() {
39         $node = $this->createClassBuilder('Test')
40             ->makeAbstract()
41             ->getNode()
42         ;
43
44         $this->assertEquals(
45             new Stmt\Class_('Test', array(
46                 'flags' => Stmt\Class_::MODIFIER_ABSTRACT
47             )),
48             $node
49         );
50     }
51
52     public function testFinal() {
53         $node = $this->createClassBuilder('Test')
54             ->makeFinal()
55             ->getNode()
56         ;
57
58         $this->assertEquals(
59             new Stmt\Class_('Test', array(
60                 'flags' => Stmt\Class_::MODIFIER_FINAL
61             )),
62             $node
63         );
64     }
65
66     public function testStatementOrder() {
67         $method = new Stmt\ClassMethod('testMethod');
68         $property = new Stmt\Property(
69             Stmt\Class_::MODIFIER_PUBLIC,
70             array(new Stmt\PropertyProperty('testProperty'))
71         );
72         $const = new Stmt\ClassConst(array(
73             new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC'))
74         ));
75         $use = new Stmt\TraitUse(array(new Name('SomeTrait')));
76
77         $node = $this->createClassBuilder('Test')
78             ->addStmt($method)
79             ->addStmt($property)
80             ->addStmts(array($const, $use))
81             ->getNode()
82         ;
83
84         $this->assertEquals(
85             new Stmt\Class_('Test', array(
86                 'stmts' => array($use, $const, $property, $method)
87             )),
88             $node
89         );
90     }
91
92     public function testDocComment() {
93         $docComment = <<<'DOC'
94 /**
95  * Test
96  */
97 DOC;
98         $class = $this->createClassBuilder('Test')
99             ->setDocComment($docComment)
100             ->getNode();
101
102         $this->assertEquals(
103             new Stmt\Class_('Test', array(), array(
104                 'comments' => array(
105                     new Comment\Doc($docComment)
106                 )
107             )),
108             $class
109         );
110
111         $class = $this->createClassBuilder('Test')
112             ->setDocComment(new Comment\Doc($docComment))
113             ->getNode();
114
115         $this->assertEquals(
116             new Stmt\Class_('Test', array(), array(
117                 'comments' => array(
118                     new Comment\Doc($docComment)
119                 )
120             )),
121             $class
122         );
123     }
124
125     /**
126      * @expectedException \LogicException
127      * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
128      */
129     public function testInvalidStmtError() {
130         $this->createClassBuilder('Test')
131             ->addStmt(new Stmt\Echo_(array()))
132         ;
133     }
134
135     /**
136      * @expectedException \LogicException
137      * @expectedExceptionMessage Doc comment must be a string or an instance of PhpParser\Comment\Doc
138      */
139     public function testInvalidDocComment() {
140         $this->createClassBuilder('Test')
141             ->setDocComment(new Comment('Test'));
142     }
143
144     /**
145      * @expectedException \LogicException
146      * @expectedExceptionMessage Name cannot be empty
147      */
148     public function testEmptyName() {
149         $this->createClassBuilder('Test')
150             ->extend('');
151     }
152
153     /**
154      * @expectedException \LogicException
155      * @expectedExceptionMessage Name must be a string or an instance of PhpParser\Node\Name
156      */
157     public function testInvalidName() {
158         $this->createClassBuilder('Test')
159             ->extend(array('Foo'));
160     }
161 }