comparison vendor/nikic/php-parser/test/PhpParser/BuilderFactoryTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 5fb285c0d0e3
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php declare(strict_types=1); 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\Arg;
7 use PhpParser\Node\Expr; 6 use PhpParser\Node\Expr;
8 use PhpParser\Node\Expr\BinaryOp\Concat; 7 use PhpParser\Node\Expr\BinaryOp\Concat;
9 use PhpParser\Node\Identifier; 8 use PhpParser\Node\Identifier;
10 use PhpParser\Node\Name; 9 use PhpParser\Node\Name;
11 use PhpParser\Node\Scalar\LNumber; 10 use PhpParser\Node\Scalar\LNumber;
12 use PhpParser\Node\Scalar\String_; 11 use PhpParser\Node\Scalar\String_;
13 use PHPUnit\Framework\TestCase; 12
14 use Symfony\Component\Yaml\Tests\A; 13 class BuilderFactoryTest extends \PHPUnit\Framework\TestCase
15
16 class BuilderFactoryTest extends TestCase
17 { 14 {
18 /** 15 /**
19 * @dataProvider provideTestFactory 16 * @dataProvider provideTestFactory
20 */ 17 */
21 public function testFactory($methodName, $className) { 18 public function testFactory($methodName, $className) {
23 $this->assertInstanceOf($className, $factory->$methodName('test')); 20 $this->assertInstanceOf($className, $factory->$methodName('test'));
24 } 21 }
25 22
26 public function provideTestFactory() { 23 public function provideTestFactory() {
27 return [ 24 return [
28 ['namespace', Builder\Namespace_::class], 25 ['namespace', Builder\Namespace_::class],
29 ['class', Builder\Class_::class], 26 ['class', Builder\Class_::class],
30 ['interface', Builder\Interface_::class], 27 ['interface', Builder\Interface_::class],
31 ['trait', Builder\Trait_::class], 28 ['trait', Builder\Trait_::class],
32 ['method', Builder\Method::class], 29 ['method', Builder\Method::class],
33 ['function', Builder\Function_::class], 30 ['function', Builder\Function_::class],
34 ['property', Builder\Property::class], 31 ['property', Builder\Property::class],
35 ['param', Builder\Param::class], 32 ['param', Builder\Param::class],
36 ['use', Builder\Use_::class], 33 ['use', Builder\Use_::class],
34 ['useFunction', Builder\Use_::class],
35 ['useConst', Builder\Use_::class],
37 ]; 36 ];
38 } 37 }
39 38
40 public function testVal() { 39 public function testVal() {
41 // This method is a wrapper around BuilderHelpers::normalizeValue(), 40 // This method is a wrapper around BuilderHelpers::normalizeValue(),
65 new Concat(new Concat(new String_("a"), $varB), new String_("c")), 64 new Concat(new Concat(new String_("a"), $varB), new String_("c")),
66 $factory->concat("a", $varB, "c") 65 $factory->concat("a", $varB, "c")
67 ); 66 );
68 } 67 }
69 68
70 /**
71 * @expectedException \LogicException
72 * @expectedExceptionMessage Expected at least two expressions
73 */
74 public function testConcatOneError() { 69 public function testConcatOneError() {
70 $this->expectException(\LogicException::class);
71 $this->expectExceptionMessage('Expected at least two expressions');
75 (new BuilderFactory())->concat("a"); 72 (new BuilderFactory())->concat("a");
76 } 73 }
77 74
78 /**
79 * @expectedException \LogicException
80 * @expectedExceptionMessage Expected string or Expr
81 */
82 public function testConcatInvalidExpr() { 75 public function testConcatInvalidExpr() {
76 $this->expectException(\LogicException::class);
77 $this->expectExceptionMessage('Expected string or Expr');
83 (new BuilderFactory())->concat("a", 42); 78 (new BuilderFactory())->concat("a", 42);
84 } 79 }
85 80
86 public function testArgs() { 81 public function testArgs() {
87 $factory = new BuilderFactory(); 82 $factory = new BuilderFactory();
186 new Expr\ClassConstFetch(new Expr\Variable('foo'), new Identifier('BAR')), 181 new Expr\ClassConstFetch(new Expr\Variable('foo'), new Identifier('BAR')),
187 $factory->classConstFetch(new Expr\Variable('foo'), 'BAR') 182 $factory->classConstFetch(new Expr\Variable('foo'), 'BAR')
188 ); 183 );
189 } 184 }
190 185
191 /** 186 public function testVar() {
192 * @expectedException \LogicException 187 $factory = new BuilderFactory();
193 * @expectedExceptionMessage Expected string or instance of Node\Identifier 188 $this->assertEquals(
194 */ 189 new Expr\Variable("foo"),
190 $factory->var("foo")
191 );
192 $this->assertEquals(
193 new Expr\Variable(new Expr\Variable("foo")),
194 $factory->var($factory->var("foo"))
195 );
196 }
197
198 public function testPropertyFetch() {
199 $f = new BuilderFactory();
200 $this->assertEquals(
201 new Expr\PropertyFetch(new Expr\Variable('foo'), 'bar'),
202 $f->propertyFetch($f->var('foo'), 'bar')
203 );
204 $this->assertEquals(
205 new Expr\PropertyFetch(new Expr\Variable('foo'), 'bar'),
206 $f->propertyFetch($f->var('foo'), new Identifier('bar'))
207 );
208 $this->assertEquals(
209 new Expr\PropertyFetch(new Expr\Variable('foo'), new Expr\Variable('bar')),
210 $f->propertyFetch($f->var('foo'), $f->var('bar'))
211 );
212 }
213
195 public function testInvalidIdentifier() { 214 public function testInvalidIdentifier() {
215 $this->expectException(\LogicException::class);
216 $this->expectExceptionMessage('Expected string or instance of Node\Identifier');
196 (new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo')); 217 (new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo'));
197 } 218 }
198 219
199 /**
200 * @expectedException \LogicException
201 * @expectedExceptionMessage Expected string or instance of Node\Identifier or Node\Expr
202 */
203 public function testInvalidIdentifierOrExpr() { 220 public function testInvalidIdentifierOrExpr() {
221 $this->expectException(\LogicException::class);
222 $this->expectExceptionMessage('Expected string or instance of Node\Identifier or Node\Expr');
204 (new BuilderFactory())->staticCall('Foo', new Name('bar')); 223 (new BuilderFactory())->staticCall('Foo', new Name('bar'));
205 } 224 }
206 225
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() { 226 public function testInvalidNameOrExpr() {
227 $this->expectException(\LogicException::class);
228 $this->expectExceptionMessage('Name must be a string or an instance of Node\Name or Node\Expr');
212 (new BuilderFactory())->funcCall(new Node\Stmt\Return_()); 229 (new BuilderFactory())->funcCall(new Node\Stmt\Return_());
230 }
231
232 public function testInvalidVar() {
233 $this->expectException(\LogicException::class);
234 $this->expectExceptionMessage('Variable name must be string or Expr');
235 (new BuilderFactory())->var(new Node\Stmt\Return_());
213 } 236 }
214 237
215 public function testIntegration() { 238 public function testIntegration() {
216 $factory = new BuilderFactory; 239 $factory = new BuilderFactory;
217 $node = $factory->namespace('Name\Space') 240 $node = $factory->namespace('Name\Space')
218 ->addStmt($factory->use('Foo\Bar\SomeOtherClass')) 241 ->addStmt($factory->use('Foo\Bar\SomeOtherClass'))
219 ->addStmt($factory->use('Foo\Bar')->as('A')) 242 ->addStmt($factory->use('Foo\Bar')->as('A'))
243 ->addStmt($factory->useFunction('strlen'))
244 ->addStmt($factory->useConst('PHP_VERSION'))
220 ->addStmt($factory 245 ->addStmt($factory
221 ->class('SomeClass') 246 ->class('SomeClass')
222 ->extend('SomeOtherClass') 247 ->extend('SomeOtherClass')
223 ->implement('A\Few', '\Interfaces') 248 ->implement('A\Few', '\Interfaces')
224 ->makeAbstract() 249 ->makeAbstract()
225 250
251 ->addStmt($factory->useTrait('FirstTrait'))
252
253 ->addStmt($factory->useTrait('SecondTrait', 'ThirdTrait')
254 ->and('AnotherTrait')
255 ->with($factory->traitUseAdaptation('foo')->as('bar'))
256 ->with($factory->traitUseAdaptation('AnotherTrait', 'baz')->as('test'))
257 ->with($factory->traitUseAdaptation('AnotherTrait', 'func')->insteadof('SecondTrait')))
258
226 ->addStmt($factory->method('firstMethod')) 259 ->addStmt($factory->method('firstMethod'))
227 260
228 ->addStmt($factory->method('someMethod') 261 ->addStmt($factory->method('someMethod')
229 ->makePublic() 262 ->makePublic()
230 ->makeAbstract() 263 ->makeAbstract()
231 ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) 264 ->addParam($factory->param('someParam')->setType('SomeClass'))
232 ->setDocComment('/** 265 ->setDocComment('/**
233 * This method does something. 266 * This method does something.
234 * 267 *
235 * @param SomeClass And takes a parameter 268 * @param SomeClass And takes a parameter
236 */')) 269 */'))
252 285
253 namespace Name\Space; 286 namespace Name\Space;
254 287
255 use Foo\Bar\SomeOtherClass; 288 use Foo\Bar\SomeOtherClass;
256 use Foo\Bar as A; 289 use Foo\Bar as A;
290 use function strlen;
291 use const PHP_VERSION;
257 abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces 292 abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
258 { 293 {
294 use FirstTrait;
295 use SecondTrait, ThirdTrait, AnotherTrait {
296 foo as bar;
297 AnotherTrait::baz as test;
298 AnotherTrait::func insteadof SecondTrait;
299 }
259 protected $someProperty; 300 protected $someProperty;
260 private $anotherProperty = array(1, 2, 3); 301 private $anotherProperty = array(1, 2, 3);
261 function firstMethod() 302 function firstMethod()
262 { 303 {
263 } 304 }