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