1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2025-01-20 04:36:57 +01:00

Add integration test for builders

This commit is contained in:
Nikita Popov 2014-12-19 18:35:19 +01:00
parent 01643e06d3
commit b4b93ccb21
3 changed files with 95 additions and 27 deletions

View File

@ -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
<?php
$factory = new PhpParser\BuilderFactory;
$node = $factory->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
<?php
namespace Name\Space;
abstract class SomeClass extends SomeOtherClass implements A\Few, Interfaces
{
protected $someProperty;

View File

@ -16,18 +16,18 @@ class NamespaceTest extends \PHPUnit_Framework_TestCase
$stmt2 = new Stmt\Interface_('SomeInterface');
$stmt3 = new Stmt\Function_('someFunction');
$expected = new Stmt\Namespace_(
new Node\Name('Some\Namespace'),
new Node\Name('Name\Space'),
array($stmt1, $stmt2, $stmt3)
);
$node = $this->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()

View File

@ -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'
<?php
namespace Name\Space;
abstract class SomeClass extends SomeOtherClass implements A\Few, Interfaces
{
protected $someProperty;
private $anotherProperty = array(1, 2, 3);
/**
* This method does something.
*
* @param SomeClass And takes a parameter
*/
public abstract function someMethod(SomeClass $someParam);
protected function anotherMethod($someParam = 'test')
{
print $someParam;
}
}
EOC;
$stmts = array($node);
$prettyPrinter = new PrettyPrinter\Standard();
$generated = $prettyPrinter->prettyPrintFile($stmts);
$this->assertEquals(
str_replace("\r\n", "\n", $expected),
str_replace("\r\n", "\n", $generated)
);
}
}