Chris@13: assertInstanceOf($className, $factory->$methodName('test')); Chris@0: } Chris@0: Chris@0: public function provideTestFactory() { Chris@13: return [ Chris@17: ['namespace', Builder\Namespace_::class], Chris@17: ['class', Builder\Class_::class], Chris@17: ['interface', Builder\Interface_::class], Chris@17: ['trait', Builder\Trait_::class], Chris@17: ['method', Builder\Method::class], Chris@17: ['function', Builder\Function_::class], Chris@17: ['property', Builder\Property::class], Chris@17: ['param', Builder\Param::class], Chris@17: ['use', Builder\Use_::class], Chris@17: ['useFunction', Builder\Use_::class], Chris@17: ['useConst', Builder\Use_::class], Chris@13: ]; Chris@13: } Chris@13: Chris@13: public function testVal() { Chris@13: // This method is a wrapper around BuilderHelpers::normalizeValue(), Chris@13: // which is already tested elsewhere Chris@13: $factory = new BuilderFactory(); Chris@13: $this->assertEquals( Chris@13: new String_("foo"), Chris@13: $factory->val("foo") Chris@0: ); Chris@0: } Chris@0: Chris@13: public function testConcat() { Chris@0: $factory = new BuilderFactory(); Chris@13: $varA = new Expr\Variable('a'); Chris@13: $varB = new Expr\Variable('b'); Chris@13: $varC = new Expr\Variable('c'); Chris@13: Chris@13: $this->assertEquals( Chris@13: new Concat($varA, $varB), Chris@13: $factory->concat($varA, $varB) Chris@13: ); Chris@13: $this->assertEquals( Chris@13: new Concat(new Concat($varA, $varB), $varC), Chris@13: $factory->concat($varA, $varB, $varC) Chris@13: ); Chris@13: $this->assertEquals( Chris@13: new Concat(new Concat(new String_("a"), $varB), new String_("c")), Chris@13: $factory->concat("a", $varB, "c") Chris@13: ); Chris@13: } Chris@13: Chris@13: public function testConcatOneError() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Expected at least two expressions'); Chris@13: (new BuilderFactory())->concat("a"); Chris@13: } Chris@13: Chris@13: public function testConcatInvalidExpr() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Expected string or Expr'); Chris@13: (new BuilderFactory())->concat("a", 42); Chris@13: } Chris@13: Chris@13: public function testArgs() { Chris@13: $factory = new BuilderFactory(); Chris@13: $unpack = new Arg(new Expr\Variable('c'), false, true); Chris@13: $this->assertEquals( Chris@13: [ Chris@13: new Arg(new Expr\Variable('a')), Chris@13: new Arg(new String_('b')), Chris@13: $unpack Chris@13: ], Chris@13: $factory->args([new Expr\Variable('a'), 'b', $unpack]) Chris@13: ); Chris@13: } Chris@13: Chris@13: public function testCalls() { Chris@13: $factory = new BuilderFactory(); Chris@13: Chris@13: // Simple function call Chris@13: $this->assertEquals( Chris@13: new Expr\FuncCall( Chris@13: new Name('var_dump'), Chris@13: [new Arg(new String_('str'))] Chris@13: ), Chris@13: $factory->funcCall('var_dump', ['str']) Chris@13: ); Chris@13: // Dynamic function call Chris@13: $this->assertEquals( Chris@13: new Expr\FuncCall(new Expr\Variable('fn')), Chris@13: $factory->funcCall(new Expr\Variable('fn')) Chris@13: ); Chris@13: Chris@13: // Simple method call Chris@13: $this->assertEquals( Chris@13: new Expr\MethodCall( Chris@13: new Expr\Variable('obj'), Chris@13: new Identifier('method'), Chris@13: [new Arg(new LNumber(42))] Chris@13: ), Chris@13: $factory->methodCall(new Expr\Variable('obj'), 'method', [42]) Chris@13: ); Chris@13: // Explicitly pass Identifier node Chris@13: $this->assertEquals( Chris@13: new Expr\MethodCall( Chris@13: new Expr\Variable('obj'), Chris@13: new Identifier('method') Chris@13: ), Chris@13: $factory->methodCall(new Expr\Variable('obj'), new Identifier('method')) Chris@13: ); Chris@13: // Dynamic method call Chris@13: $this->assertEquals( Chris@13: new Expr\MethodCall( Chris@13: new Expr\Variable('obj'), Chris@13: new Expr\Variable('method') Chris@13: ), Chris@13: $factory->methodCall(new Expr\Variable('obj'), new Expr\Variable('method')) Chris@13: ); Chris@13: Chris@13: // Simple static method call Chris@13: $this->assertEquals( Chris@13: new Expr\StaticCall( Chris@13: new Name\FullyQualified('Foo'), Chris@13: new Identifier('bar'), Chris@13: [new Arg(new Expr\Variable('baz'))] Chris@13: ), Chris@13: $factory->staticCall('\Foo', 'bar', [new Expr\Variable('baz')]) Chris@13: ); Chris@13: // Dynamic static method call Chris@13: $this->assertEquals( Chris@13: new Expr\StaticCall( Chris@13: new Expr\Variable('foo'), Chris@13: new Expr\Variable('bar') Chris@13: ), Chris@13: $factory->staticCall(new Expr\Variable('foo'), new Expr\Variable('bar')) Chris@13: ); Chris@13: Chris@13: // Simple new call Chris@13: $this->assertEquals( Chris@13: new Expr\New_(new Name\FullyQualified('stdClass')), Chris@13: $factory->new('\stdClass') Chris@13: ); Chris@13: // Dynamic new call Chris@13: $this->assertEquals( Chris@13: new Expr\New_( Chris@13: new Expr\Variable('foo'), Chris@13: [new Arg(new String_('bar'))] Chris@13: ), Chris@13: $factory->new(new Expr\Variable('foo'), ['bar']) Chris@13: ); Chris@13: } Chris@13: Chris@13: public function testConstFetches() { Chris@13: $factory = new BuilderFactory(); Chris@13: $this->assertEquals( Chris@13: new Expr\ConstFetch(new Name('FOO')), Chris@13: $factory->constFetch('FOO') Chris@13: ); Chris@13: $this->assertEquals( Chris@13: new Expr\ClassConstFetch(new Name('Foo'), new Identifier('BAR')), Chris@13: $factory->classConstFetch('Foo', 'BAR') Chris@13: ); Chris@13: $this->assertEquals( Chris@13: new Expr\ClassConstFetch(new Expr\Variable('foo'), new Identifier('BAR')), Chris@13: $factory->classConstFetch(new Expr\Variable('foo'), 'BAR') Chris@13: ); Chris@13: } Chris@13: Chris@17: public function testVar() { Chris@17: $factory = new BuilderFactory(); Chris@17: $this->assertEquals( Chris@17: new Expr\Variable("foo"), Chris@17: $factory->var("foo") Chris@17: ); Chris@17: $this->assertEquals( Chris@17: new Expr\Variable(new Expr\Variable("foo")), Chris@17: $factory->var($factory->var("foo")) Chris@17: ); Chris@17: } Chris@17: Chris@17: public function testPropertyFetch() { Chris@17: $f = new BuilderFactory(); Chris@17: $this->assertEquals( Chris@17: new Expr\PropertyFetch(new Expr\Variable('foo'), 'bar'), Chris@17: $f->propertyFetch($f->var('foo'), 'bar') Chris@17: ); Chris@17: $this->assertEquals( Chris@17: new Expr\PropertyFetch(new Expr\Variable('foo'), 'bar'), Chris@17: $f->propertyFetch($f->var('foo'), new Identifier('bar')) Chris@17: ); Chris@17: $this->assertEquals( Chris@17: new Expr\PropertyFetch(new Expr\Variable('foo'), new Expr\Variable('bar')), Chris@17: $f->propertyFetch($f->var('foo'), $f->var('bar')) Chris@17: ); Chris@17: } Chris@17: Chris@13: public function testInvalidIdentifier() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Expected string or instance of Node\Identifier'); Chris@13: (new BuilderFactory())->classConstFetch('Foo', new Expr\Variable('foo')); Chris@13: } Chris@13: Chris@13: public function testInvalidIdentifierOrExpr() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Expected string or instance of Node\Identifier or Node\Expr'); Chris@13: (new BuilderFactory())->staticCall('Foo', new Name('bar')); Chris@13: } Chris@13: Chris@13: public function testInvalidNameOrExpr() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Name must be a string or an instance of Node\Name or Node\Expr'); Chris@13: (new BuilderFactory())->funcCall(new Node\Stmt\Return_()); Chris@0: } Chris@0: Chris@17: public function testInvalidVar() { Chris@17: $this->expectException(\LogicException::class); Chris@17: $this->expectExceptionMessage('Variable name must be string or Expr'); Chris@17: (new BuilderFactory())->var(new Node\Stmt\Return_()); Chris@17: } Chris@17: Chris@0: public function testIntegration() { Chris@0: $factory = new BuilderFactory; Chris@0: $node = $factory->namespace('Name\Space') Chris@0: ->addStmt($factory->use('Foo\Bar\SomeOtherClass')) Chris@0: ->addStmt($factory->use('Foo\Bar')->as('A')) Chris@17: ->addStmt($factory->useFunction('strlen')) Chris@17: ->addStmt($factory->useConst('PHP_VERSION')) Chris@0: ->addStmt($factory Chris@0: ->class('SomeClass') Chris@0: ->extend('SomeOtherClass') Chris@0: ->implement('A\Few', '\Interfaces') Chris@0: ->makeAbstract() Chris@0: Chris@17: ->addStmt($factory->useTrait('FirstTrait')) Chris@17: Chris@17: ->addStmt($factory->useTrait('SecondTrait', 'ThirdTrait') Chris@17: ->and('AnotherTrait') Chris@17: ->with($factory->traitUseAdaptation('foo')->as('bar')) Chris@17: ->with($factory->traitUseAdaptation('AnotherTrait', 'baz')->as('test')) Chris@17: ->with($factory->traitUseAdaptation('AnotherTrait', 'func')->insteadof('SecondTrait'))) Chris@17: Chris@0: ->addStmt($factory->method('firstMethod')) Chris@0: Chris@0: ->addStmt($factory->method('someMethod') Chris@0: ->makePublic() Chris@0: ->makeAbstract() Chris@17: ->addParam($factory->param('someParam')->setType('SomeClass')) Chris@0: ->setDocComment('/** Chris@0: * This method does something. Chris@0: * Chris@0: * @param SomeClass And takes a parameter Chris@0: */')) Chris@0: Chris@0: ->addStmt($factory->method('anotherMethod') Chris@0: ->makeProtected() Chris@0: ->addParam($factory->param('someParam')->setDefault('test')) Chris@0: ->addStmt(new Expr\Print_(new Expr\Variable('someParam')))) Chris@0: Chris@0: ->addStmt($factory->property('someProperty')->makeProtected()) Chris@0: ->addStmt($factory->property('anotherProperty') Chris@0: ->makePrivate() Chris@13: ->setDefault([1, 2, 3]))) Chris@0: ->getNode() Chris@0: ; Chris@0: Chris@0: $expected = <<<'EOC' Chris@0: prettyPrintFile($stmts); Chris@0: Chris@0: $this->assertEquals( Chris@0: str_replace("\r\n", "\n", $expected), Chris@0: str_replace("\r\n", "\n", $generated) Chris@0: ); Chris@0: } Chris@0: }