Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / BuilderFactoryTest.php
1 <?php declare(strict_types=1);
2
3 namespace PhpParser;
4
5 use PhpParser\Builder;
6 use PhpParser\Node\Arg;
7 use PhpParser\Node\Expr;
8 use PhpParser\Node\Expr\BinaryOp\Concat;
9 use PhpParser\Node\Identifier;
10 use PhpParser\Node\Name;
11 use PhpParser\Node\Scalar\LNumber;
12 use PhpParser\Node\Scalar\String_;
13 use PHPUnit\Framework\TestCase;
14 use Symfony\Component\Yaml\Tests\A;
15
16 class BuilderFactoryTest extends TestCase
17 {
18     /**
19      * @dataProvider provideTestFactory
20      */
21     public function testFactory($methodName, $className) {
22         $factory = new BuilderFactory;
23         $this->assertInstanceOf($className, $factory->$methodName('test'));
24     }
25
26     public function provideTestFactory() {
27         return [
28             ['namespace', Builder\Namespace_::class],
29             ['class',     Builder\Class_::class],
30             ['interface', Builder\Interface_::class],
31             ['trait',     Builder\Trait_::class],
32             ['method',    Builder\Method::class],
33             ['function',  Builder\Function_::class],
34             ['property',  Builder\Property::class],
35             ['param',     Builder\Param::class],
36             ['use',       Builder\Use_::class],
37         ];
38     }
39
40     public function testVal() {
41         // This method is a wrapper around BuilderHelpers::normalizeValue(),
42         // which is already tested elsewhere
43         $factory = new BuilderFactory();
44         $this->assertEquals(
45             new String_("foo"),
46             $factory->val("foo")
47         );
48     }
49
50     public function testConcat() {
51         $factory = new BuilderFactory();
52         $varA = new Expr\Variable('a');
53         $varB = new Expr\Variable('b');
54         $varC = new Expr\Variable('c');
55
56         $this->assertEquals(
57             new Concat($varA, $varB),
58             $factory->concat($varA, $varB)
59         );
60         $this->assertEquals(
61             new Concat(new Concat($varA, $varB), $varC),
62             $factory->concat($varA, $varB, $varC)
63         );
64         $this->assertEquals(
65             new Concat(new Concat(new String_("a"), $varB), new String_("c")),
66             $factory->concat("a", $varB, "c")
67         );
68     }
69
70     /**
71      * @expectedException \LogicException
72      * @expectedExceptionMessage Expected at least two expressions
73      */
74     public function testConcatOneError() {
75         (new BuilderFactory())->concat("a");
76     }
77
78     /**
79      * @expectedException \LogicException
80      * @expectedExceptionMessage Expected string or Expr
81      */
82     public function testConcatInvalidExpr() {
83         (new BuilderFactory())->concat("a", 42);
84     }
85
86     public function testArgs() {
87         $factory = new BuilderFactory();
88         $unpack = new Arg(new Expr\Variable('c'), false, true);
89         $this->assertEquals(
90             [
91                 new Arg(new Expr\Variable('a')),
92                 new Arg(new String_('b')),
93                 $unpack
94             ],
95             $factory->args([new Expr\Variable('a'), 'b', $unpack])
96         );
97     }
98
99     public function testCalls() {
100         $factory = new BuilderFactory();
101
102         // Simple function call
103         $this->assertEquals(
104             new Expr\FuncCall(
105                 new Name('var_dump'),
106                 [new Arg(new String_('str'))]
107             ),
108             $factory->funcCall('var_dump', ['str'])
109         );
110         // Dynamic function call
111         $this->assertEquals(
112             new Expr\FuncCall(new Expr\Variable('fn')),
113             $factory->funcCall(new Expr\Variable('fn'))
114         );
115
116         // Simple method call
117         $this->assertEquals(
118             new Expr\MethodCall(
119                 new Expr\Variable('obj'),
120                 new Identifier('method'),
121                 [new Arg(new LNumber(42))]
122             ),
123             $factory->methodCall(new Expr\Variable('obj'), 'method', [42])
124         );
125         // Explicitly pass Identifier node
126         $this->assertEquals(
127             new Expr\MethodCall(
128                 new Expr\Variable('obj'),
129                 new Identifier('method')
130             ),
131             $factory->methodCall(new Expr\Variable('obj'), new Identifier('method'))
132         );
133         // Dynamic method call
134         $this->assertEquals(
135             new Expr\MethodCall(
136                 new Expr\Variable('obj'),
137                 new Expr\Variable('method')
138             ),
139             $factory->methodCall(new Expr\Variable('obj'), new Expr\Variable('method'))
140         );
141
142         // Simple static method call
143         $this->assertEquals(
144             new Expr\StaticCall(
145                 new Name\FullyQualified('Foo'),
146                 new Identifier('bar'),
147                 [new Arg(new Expr\Variable('baz'))]
148             ),
149             $factory->staticCall('\Foo', 'bar', [new Expr\Variable('baz')])
150         );
151         // Dynamic static method call
152         $this->assertEquals(
153             new Expr\StaticCall(
154                 new Expr\Variable('foo'),
155                 new Expr\Variable('bar')
156             ),
157             $factory->staticCall(new Expr\Variable('foo'), new Expr\Variable('bar'))
158         );
159
160         // Simple new call
161         $this->assertEquals(
162             new Expr\New_(new Name\FullyQualified('stdClass')),
163             $factory->new('\stdClass')
164         );
165         // Dynamic new call
166         $this->assertEquals(
167             new Expr\New_(
168                 new Expr\Variable('foo'),
169                 [new Arg(new String_('bar'))]
170             ),
171             $factory->new(new Expr\Variable('foo'), ['bar'])
172         );
173     }
174
175     public function testConstFetches() {
176         $factory = new BuilderFactory();
177         $this->assertEquals(
178             new Expr\ConstFetch(new Name('FOO')),
179             $factory->constFetch('FOO')
180         );
181         $this->assertEquals(
182             new Expr\ClassConstFetch(new Name('Foo'), new Identifier('BAR')),
183             $factory->classConstFetch('Foo', 'BAR')
184         );
185         $this->assertEquals(
186             new Expr\ClassConstFetch(new Expr\Variable('foo'), new Identifier('BAR')),
187             $factory->classConstFetch(new Expr\Variable('foo'), 'BAR')
188         );
189     }
190
191     /**
192      * @expectedException \LogicException
193      * @expectedExceptionMessage Expected string or instance of Node\Identifier
194      */
195     public function testInvalidIdentifier() {
196         (new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo'));
197     }
198
199     /**
200      * @expectedException \LogicException
201      * @expectedExceptionMessage Expected string or instance of Node\Identifier or Node\Expr
202      */
203     public function testInvalidIdentifierOrExpr() {
204         (new BuilderFactory())->staticCall('Foo', new Name('bar'));
205     }
206
207     /**
208      * @expectedException \LogicException
209      * @expectedExceptionMessage Name must be a string or an instance of Node\Name or Node\Expr
210      */
211     public function testInvalidNameOrExpr() {
212         (new BuilderFactory())->funcCall(new Node\Stmt\Return_());
213     }
214
215     public function testIntegration() {
216         $factory = new BuilderFactory;
217         $node = $factory->namespace('Name\Space')
218             ->addStmt($factory->use('Foo\Bar\SomeOtherClass'))
219             ->addStmt($factory->use('Foo\Bar')->as('A'))
220             ->addStmt($factory
221                 ->class('SomeClass')
222                 ->extend('SomeOtherClass')
223                 ->implement('A\Few', '\Interfaces')
224                 ->makeAbstract()
225
226                 ->addStmt($factory->method('firstMethod'))
227
228                 ->addStmt($factory->method('someMethod')
229                     ->makePublic()
230                     ->makeAbstract()
231                     ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
232                     ->setDocComment('/**
233                                       * This method does something.
234                                       *
235                                       * @param SomeClass And takes a parameter
236                                       */'))
237
238                 ->addStmt($factory->method('anotherMethod')
239                     ->makeProtected()
240                     ->addParam($factory->param('someParam')->setDefault('test'))
241                     ->addStmt(new Expr\Print_(new Expr\Variable('someParam'))))
242
243                 ->addStmt($factory->property('someProperty')->makeProtected())
244                 ->addStmt($factory->property('anotherProperty')
245                     ->makePrivate()
246                     ->setDefault([1, 2, 3])))
247             ->getNode()
248         ;
249
250         $expected = <<<'EOC'
251 <?php
252
253 namespace Name\Space;
254
255 use Foo\Bar\SomeOtherClass;
256 use Foo\Bar as A;
257 abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
258 {
259     protected $someProperty;
260     private $anotherProperty = array(1, 2, 3);
261     function firstMethod()
262     {
263     }
264     /**
265      * This method does something.
266      *
267      * @param SomeClass And takes a parameter
268      */
269     public abstract function someMethod(SomeClass $someParam);
270     protected function anotherMethod($someParam = 'test')
271     {
272         print $someParam;
273     }
274 }
275 EOC;
276
277         $stmts = [$node];
278         $prettyPrinter = new PrettyPrinter\Standard();
279         $generated = $prettyPrinter->prettyPrintFile($stmts);
280
281         $this->assertEquals(
282             str_replace("\r\n", "\n", $expected),
283             str_replace("\r\n", "\n", $generated)
284         );
285     }
286 }