From b4b93ccb21131c5b07bc53fe8f2b2de1ebe29b45 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 19 Dec 2014 18:35:19 +0100 Subject: [PATCH] Add integration test for builders --- doc/4_Code_generation.markdown | 52 ++++++++++--------- test/PhpParser/Builder/NamespaceTest.php | 6 +-- test/PhpParser/BuilderFactoryTest.php | 64 ++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 27 deletions(-) diff --git a/doc/4_Code_generation.markdown b/doc/4_Code_generation.markdown index e345ed1..cbfa422 100644 --- a/doc/4_Code_generation.markdown +++ b/doc/4_Code_generation.markdown @@ -3,39 +3,41 @@ Code generation It is also possible to generate code using the parser, by first creating an Abstract Syntax Tree and then using the pretty printer to convert it to PHP code. To simplify code generation, the project comes with a set of builders for -classes, interfaces, traits, methods, functions, parameters and properties. The builders allow creating node trees using -a fluid interface, instead of instantiating all nodes manually. +namespaces, classes, interfaces, traits, methods, functions, parameters and properties. The builders allow creating node +trees using a fluid interface, instead of instantiating all nodes manually. Here is an example: ```php class('SomeClass') - ->extend('SomeOtherClass') - ->implement('A\Few', 'Interfaces') - ->makeAbstract() // ->makeFinal() - - ->addStmt($factory->method('someMethod') +$node = $factory->namespace('Name\Space') + ->addStmt($factory->class('SomeClass') + ->extend('SomeOtherClass') + ->implement('A\Few', 'Interfaces') ->makeAbstract() // ->makeFinal() - ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) - ->setDocComment('/** - * This method does something. - * - * @param SomeClass And takes a parameter - */') - ) - ->addStmt($factory->method('anotherMethod') - ->makeProtected() // ->makePublic() [default], ->makePrivate() - ->addParam($factory->param('someParam')->setDefault('test')) - // it is possible to add manually created nodes - ->addStmt(new PhpParser\Node\Expr\Print_(new PhpParser\Node\Expr\Variable('someParam'))) - ) + ->addStmt($factory->method('someMethod') + ->makeAbstract() // ->makeFinal() + ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) + ->setDocComment('/** + * This method does something. + * + * @param SomeClass And takes a parameter + */') + ) - // properties will be correctly reordered above the methods - ->addStmt($factory->property('someProperty')->makeProtected()) - ->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3))) + ->addStmt($factory->method('anotherMethod') + ->makeProtected() // ->makePublic() [default], ->makePrivate() + ->addParam($factory->param('someParam')->setDefault('test')) + // it is possible to add manually created nodes + ->addStmt(new PhpParser\Node\Expr\Print_(new PhpParser\Node\Expr\Variable('someParam'))) + ) + + // properties will be correctly reordered above the methods + ->addStmt($factory->property('someProperty')->makeProtected()) + ->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3))) + ) ->getNode() ; @@ -50,6 +52,8 @@ This will produce the following output with the standard pretty printer: ```php createNamespaceBuilder('Some\Namespace') + $node = $this->createNamespaceBuilder('Name\Space') ->addStmt($stmt1) ->addStmts(array($stmt2, $stmt3)) ->getNode() ; $this->assertEquals($expected, $node); - $node = $this->createNamespaceBuilder(new Node\Name(array('Some', 'Namespace'))) + $node = $this->createNamespaceBuilder(new Node\Name(array('Name', 'Space'))) ->addStmts(array($stmt1, $stmt2)) ->addStmt($stmt3) ->getNode() diff --git a/test/PhpParser/BuilderFactoryTest.php b/test/PhpParser/BuilderFactoryTest.php index 4eac850..d43f760 100644 --- a/test/PhpParser/BuilderFactoryTest.php +++ b/test/PhpParser/BuilderFactoryTest.php @@ -2,6 +2,8 @@ namespace PhpParser; +use PhpParser\Node\Expr; + class BuilderFactoryTest extends \PHPUnit_Framework_TestCase { /** @@ -24,4 +26,66 @@ class BuilderFactoryTest extends \PHPUnit_Framework_TestCase array('param', 'PhpParser\Builder\Param'), ); } + + public function testIntegration() { + $factory = new BuilderFactory; + $node = $factory->namespace('Name\Space') + ->addStmt($factory + ->class('SomeClass') + ->extend('SomeOtherClass') + ->implement('A\Few', 'Interfaces') + ->makeAbstract() + + ->addStmt($factory->method('someMethod') + ->makeAbstract() + ->addParam($factory->param('someParam')->setTypeHint('SomeClass')) + ->setDocComment('/** + * This method does something. + * + * @param SomeClass And takes a parameter + */')) + + ->addStmt($factory->method('anotherMethod') + ->makeProtected() + ->addParam($factory->param('someParam')->setDefault('test')) + ->addStmt(new Expr\Print_(new Expr\Variable('someParam')))) + + ->addStmt($factory->property('someProperty')->makeProtected()) + ->addStmt($factory->property('anotherProperty') + ->makePrivate() + ->setDefault(array(1, 2, 3)))) + ->getNode() + ; + + $expected = <<<'EOC' +prettyPrintFile($stmts); + + $this->assertEquals( + str_replace("\r\n", "\n", $expected), + str_replace("\r\n", "\n", $generated) + ); + } }