Mercurial > hg > isophonics-drupal-site
comparison vendor/nikic/php-parser/test/PhpParser/BuilderFactoryTest.php @ 13:5fb285c0d0e3
Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've
been lucky to get away with this so far, as we don't support self-registration
which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5
was vulnerable to.
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:33:26 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
12:7a779792577d | 13:5fb285c0d0e3 |
---|---|
1 <?php | 1 <?php declare(strict_types=1); |
2 | 2 |
3 namespace PhpParser; | 3 namespace PhpParser; |
4 | 4 |
5 use PhpParser\Builder; | |
6 use PhpParser\Node\Arg; | |
5 use PhpParser\Node\Expr; | 7 use PhpParser\Node\Expr; |
6 | 8 use PhpParser\Node\Expr\BinaryOp\Concat; |
7 class BuilderFactoryTest extends \PHPUnit_Framework_TestCase | 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 | |
8 { | 17 { |
9 /** | 18 /** |
10 * @dataProvider provideTestFactory | 19 * @dataProvider provideTestFactory |
11 */ | 20 */ |
12 public function testFactory($methodName, $className) { | 21 public function testFactory($methodName, $className) { |
13 $factory = new BuilderFactory; | 22 $factory = new BuilderFactory; |
14 $this->assertInstanceOf($className, $factory->$methodName('test')); | 23 $this->assertInstanceOf($className, $factory->$methodName('test')); |
15 } | 24 } |
16 | 25 |
17 public function provideTestFactory() { | 26 public function provideTestFactory() { |
18 return array( | 27 return [ |
19 array('namespace', 'PhpParser\Builder\Namespace_'), | 28 ['namespace', Builder\Namespace_::class], |
20 array('class', 'PhpParser\Builder\Class_'), | 29 ['class', Builder\Class_::class], |
21 array('interface', 'PhpParser\Builder\Interface_'), | 30 ['interface', Builder\Interface_::class], |
22 array('trait', 'PhpParser\Builder\Trait_'), | 31 ['trait', Builder\Trait_::class], |
23 array('method', 'PhpParser\Builder\Method'), | 32 ['method', Builder\Method::class], |
24 array('function', 'PhpParser\Builder\Function_'), | 33 ['function', Builder\Function_::class], |
25 array('property', 'PhpParser\Builder\Property'), | 34 ['property', Builder\Property::class], |
26 array('param', 'PhpParser\Builder\Param'), | 35 ['param', Builder\Param::class], |
27 array('use', 'PhpParser\Builder\Use_'), | 36 ['use', Builder\Use_::class], |
28 ); | 37 ]; |
29 } | 38 } |
30 | 39 |
31 public function testNonExistingMethod() { | 40 public function testVal() { |
32 $this->setExpectedException('LogicException', 'Method "foo" does not exist'); | 41 // This method is a wrapper around BuilderHelpers::normalizeValue(), |
33 $factory = new BuilderFactory(); | 42 // which is already tested elsewhere |
34 $factory->foo(); | 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_()); | |
35 } | 213 } |
36 | 214 |
37 public function testIntegration() { | 215 public function testIntegration() { |
38 $factory = new BuilderFactory; | 216 $factory = new BuilderFactory; |
39 $node = $factory->namespace('Name\Space') | 217 $node = $factory->namespace('Name\Space') |
63 ->addStmt(new Expr\Print_(new Expr\Variable('someParam')))) | 241 ->addStmt(new Expr\Print_(new Expr\Variable('someParam')))) |
64 | 242 |
65 ->addStmt($factory->property('someProperty')->makeProtected()) | 243 ->addStmt($factory->property('someProperty')->makeProtected()) |
66 ->addStmt($factory->property('anotherProperty') | 244 ->addStmt($factory->property('anotherProperty') |
67 ->makePrivate() | 245 ->makePrivate() |
68 ->setDefault(array(1, 2, 3)))) | 246 ->setDefault([1, 2, 3]))) |
69 ->getNode() | 247 ->getNode() |
70 ; | 248 ; |
71 | 249 |
72 $expected = <<<'EOC' | 250 $expected = <<<'EOC' |
73 <?php | 251 <?php |
94 print $someParam; | 272 print $someParam; |
95 } | 273 } |
96 } | 274 } |
97 EOC; | 275 EOC; |
98 | 276 |
99 $stmts = array($node); | 277 $stmts = [$node]; |
100 $prettyPrinter = new PrettyPrinter\Standard(); | 278 $prettyPrinter = new PrettyPrinter\Standard(); |
101 $generated = $prettyPrinter->prettyPrintFile($stmts); | 279 $generated = $prettyPrinter->prettyPrintFile($stmts); |
102 | 280 |
103 $this->assertEquals( | 281 $this->assertEquals( |
104 str_replace("\r\n", "\n", $expected), | 282 str_replace("\r\n", "\n", $expected), |