Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / BuilderFactoryTest.php
index 1c3ef18dfa4fcb7fdf730ab0c84ccf5735e35fce..465f600f5b286cdc8f13fae8dc2cd7e4724b011f 100644 (file)
@@ -1,10 +1,19 @@
-<?php
+<?php declare(strict_types=1);
 
 namespace PhpParser;
 
+use PhpParser\Builder;
+use PhpParser\Node\Arg;
 use PhpParser\Node\Expr;
+use PhpParser\Node\Expr\BinaryOp\Concat;
+use PhpParser\Node\Identifier;
+use PhpParser\Node\Name;
+use PhpParser\Node\Scalar\LNumber;
+use PhpParser\Node\Scalar\String_;
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Yaml\Tests\A;
 
-class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
+class BuilderFactoryTest extends TestCase
 {
     /**
      * @dataProvider provideTestFactory
@@ -15,23 +24,192 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
     }
 
     public function provideTestFactory() {
-        return array(
-            array('namespace', 'PhpParser\Builder\Namespace_'),
-            array('class',     'PhpParser\Builder\Class_'),
-            array('interface', 'PhpParser\Builder\Interface_'),
-            array('trait',     'PhpParser\Builder\Trait_'),
-            array('method',    'PhpParser\Builder\Method'),
-            array('function',  'PhpParser\Builder\Function_'),
-            array('property',  'PhpParser\Builder\Property'),
-            array('param',     'PhpParser\Builder\Param'),
-            array('use',       'PhpParser\Builder\Use_'),
+        return [
+            ['namespace', Builder\Namespace_::class],
+            ['class',     Builder\Class_::class],
+            ['interface', Builder\Interface_::class],
+            ['trait',     Builder\Trait_::class],
+            ['method',    Builder\Method::class],
+            ['function',  Builder\Function_::class],
+            ['property',  Builder\Property::class],
+            ['param',     Builder\Param::class],
+            ['use',       Builder\Use_::class],
+        ];
+    }
+
+    public function testVal() {
+        // This method is a wrapper around BuilderHelpers::normalizeValue(),
+        // which is already tested elsewhere
+        $factory = new BuilderFactory();
+        $this->assertEquals(
+            new String_("foo"),
+            $factory->val("foo")
+        );
+    }
+
+    public function testConcat() {
+        $factory = new BuilderFactory();
+        $varA = new Expr\Variable('a');
+        $varB = new Expr\Variable('b');
+        $varC = new Expr\Variable('c');
+
+        $this->assertEquals(
+            new Concat($varA, $varB),
+            $factory->concat($varA, $varB)
+        );
+        $this->assertEquals(
+            new Concat(new Concat($varA, $varB), $varC),
+            $factory->concat($varA, $varB, $varC)
+        );
+        $this->assertEquals(
+            new Concat(new Concat(new String_("a"), $varB), new String_("c")),
+            $factory->concat("a", $varB, "c")
+        );
+    }
+
+    /**
+     * @expectedException \LogicException
+     * @expectedExceptionMessage Expected at least two expressions
+     */
+    public function testConcatOneError() {
+        (new BuilderFactory())->concat("a");
+    }
+
+    /**
+     * @expectedException \LogicException
+     * @expectedExceptionMessage Expected string or Expr
+     */
+    public function testConcatInvalidExpr() {
+        (new BuilderFactory())->concat("a", 42);
+    }
+
+    public function testArgs() {
+        $factory = new BuilderFactory();
+        $unpack = new Arg(new Expr\Variable('c'), false, true);
+        $this->assertEquals(
+            [
+                new Arg(new Expr\Variable('a')),
+                new Arg(new String_('b')),
+                $unpack
+            ],
+            $factory->args([new Expr\Variable('a'), 'b', $unpack])
         );
     }
 
-    public function testNonExistingMethod() {
-        $this->setExpectedException('LogicException', 'Method "foo" does not exist');
+    public function testCalls() {
         $factory = new BuilderFactory();
-        $factory->foo();
+
+        // Simple function call
+        $this->assertEquals(
+            new Expr\FuncCall(
+                new Name('var_dump'),
+                [new Arg(new String_('str'))]
+            ),
+            $factory->funcCall('var_dump', ['str'])
+        );
+        // Dynamic function call
+        $this->assertEquals(
+            new Expr\FuncCall(new Expr\Variable('fn')),
+            $factory->funcCall(new Expr\Variable('fn'))
+        );
+
+        // Simple method call
+        $this->assertEquals(
+            new Expr\MethodCall(
+                new Expr\Variable('obj'),
+                new Identifier('method'),
+                [new Arg(new LNumber(42))]
+            ),
+            $factory->methodCall(new Expr\Variable('obj'), 'method', [42])
+        );
+        // Explicitly pass Identifier node
+        $this->assertEquals(
+            new Expr\MethodCall(
+                new Expr\Variable('obj'),
+                new Identifier('method')
+            ),
+            $factory->methodCall(new Expr\Variable('obj'), new Identifier('method'))
+        );
+        // Dynamic method call
+        $this->assertEquals(
+            new Expr\MethodCall(
+                new Expr\Variable('obj'),
+                new Expr\Variable('method')
+            ),
+            $factory->methodCall(new Expr\Variable('obj'), new Expr\Variable('method'))
+        );
+
+        // Simple static method call
+        $this->assertEquals(
+            new Expr\StaticCall(
+                new Name\FullyQualified('Foo'),
+                new Identifier('bar'),
+                [new Arg(new Expr\Variable('baz'))]
+            ),
+            $factory->staticCall('\Foo', 'bar', [new Expr\Variable('baz')])
+        );
+        // Dynamic static method call
+        $this->assertEquals(
+            new Expr\StaticCall(
+                new Expr\Variable('foo'),
+                new Expr\Variable('bar')
+            ),
+            $factory->staticCall(new Expr\Variable('foo'), new Expr\Variable('bar'))
+        );
+
+        // Simple new call
+        $this->assertEquals(
+            new Expr\New_(new Name\FullyQualified('stdClass')),
+            $factory->new('\stdClass')
+        );
+        // Dynamic new call
+        $this->assertEquals(
+            new Expr\New_(
+                new Expr\Variable('foo'),
+                [new Arg(new String_('bar'))]
+            ),
+            $factory->new(new Expr\Variable('foo'), ['bar'])
+        );
+    }
+
+    public function testConstFetches() {
+        $factory = new BuilderFactory();
+        $this->assertEquals(
+            new Expr\ConstFetch(new Name('FOO')),
+            $factory->constFetch('FOO')
+        );
+        $this->assertEquals(
+            new Expr\ClassConstFetch(new Name('Foo'), new Identifier('BAR')),
+            $factory->classConstFetch('Foo', 'BAR')
+        );
+        $this->assertEquals(
+            new Expr\ClassConstFetch(new Expr\Variable('foo'), new Identifier('BAR')),
+            $factory->classConstFetch(new Expr\Variable('foo'), 'BAR')
+        );
+    }
+
+    /**
+     * @expectedException \LogicException
+     * @expectedExceptionMessage Expected string or instance of Node\Identifier
+     */
+    public function testInvalidIdentifier() {
+        (new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo'));
+    }
+
+    /**
+     * @expectedException \LogicException
+     * @expectedExceptionMessage Expected string or instance of Node\Identifier or Node\Expr
+     */
+    public function testInvalidIdentifierOrExpr() {
+        (new BuilderFactory())->staticCall('Foo', new Name('bar'));
+    }
+
+    /**
+     * @expectedException \LogicException
+     * @expectedExceptionMessage Name must be a string or an instance of Node\Name or Node\Expr
+     */
+    public function testInvalidNameOrExpr() {
+        (new BuilderFactory())->funcCall(new Node\Stmt\Return_());
     }
 
     public function testIntegration() {
@@ -65,7 +243,7 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
                 ->addStmt($factory->property('someProperty')->makeProtected())
                 ->addStmt($factory->property('anotherProperty')
                     ->makePrivate()
-                    ->setDefault(array(1, 2, 3))))
+                    ->setDefault([1, 2, 3])))
             ->getNode()
         ;
 
@@ -96,7 +274,7 @@ abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
 }
 EOC;
 
-        $stmts = array($node);
+        $stmts = [$node];
         $prettyPrinter = new PrettyPrinter\Standard();
         $generated = $prettyPrinter->prettyPrintFile($stmts);