2014-02-06 14:44:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpParser;
|
|
|
|
|
2014-12-19 18:35:19 +01:00
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestFactory
|
|
|
|
*/
|
|
|
|
public function testFactory($methodName, $className) {
|
|
|
|
$factory = new BuilderFactory;
|
|
|
|
$this->assertInstanceOf($className, $factory->$methodName('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestFactory() {
|
|
|
|
return array(
|
2014-12-11 17:24:10 +01:00
|
|
|
array('namespace', 'PhpParser\Builder\Namespace_'),
|
2014-02-06 14:44:16 +01:00
|
|
|
array('class', 'PhpParser\Builder\Class_'),
|
|
|
|
array('interface', 'PhpParser\Builder\Interface_'),
|
2014-12-19 17:58:47 +01:00
|
|
|
array('trait', 'PhpParser\Builder\Trait_'),
|
2014-02-06 14:44:16 +01:00
|
|
|
array('method', 'PhpParser\Builder\Method'),
|
|
|
|
array('function', 'PhpParser\Builder\Function_'),
|
|
|
|
array('property', 'PhpParser\Builder\Property'),
|
|
|
|
array('param', 'PhpParser\Builder\Param'),
|
|
|
|
);
|
|
|
|
}
|
2014-12-19 18:35:19 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
2014-12-19 17:58:47 +01:00
|
|
|
}
|