5 use PhpParser\Node\Expr;
7 class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
10 * @dataProvider provideTestFactory
12 public function testFactory($methodName, $className) {
13 $factory = new BuilderFactory;
14 $this->assertInstanceOf($className, $factory->$methodName('test'));
17 public function provideTestFactory() {
19 array('namespace', 'PhpParser\Builder\Namespace_'),
20 array('class', 'PhpParser\Builder\Class_'),
21 array('interface', 'PhpParser\Builder\Interface_'),
22 array('trait', 'PhpParser\Builder\Trait_'),
23 array('method', 'PhpParser\Builder\Method'),
24 array('function', 'PhpParser\Builder\Function_'),
25 array('property', 'PhpParser\Builder\Property'),
26 array('param', 'PhpParser\Builder\Param'),
27 array('use', 'PhpParser\Builder\Use_'),
31 public function testNonExistingMethod() {
32 $this->setExpectedException('LogicException', 'Method "foo" does not exist');
33 $factory = new BuilderFactory();
37 public function testIntegration() {
38 $factory = new BuilderFactory;
39 $node = $factory->namespace('Name\Space')
40 ->addStmt($factory->use('Foo\Bar\SomeOtherClass'))
41 ->addStmt($factory->use('Foo\Bar')->as('A'))
44 ->extend('SomeOtherClass')
45 ->implement('A\Few', '\Interfaces')
48 ->addStmt($factory->method('firstMethod'))
50 ->addStmt($factory->method('someMethod')
53 ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
55 * This method does something.
57 * @param SomeClass And takes a parameter
60 ->addStmt($factory->method('anotherMethod')
62 ->addParam($factory->param('someParam')->setDefault('test'))
63 ->addStmt(new Expr\Print_(new Expr\Variable('someParam'))))
65 ->addStmt($factory->property('someProperty')->makeProtected())
66 ->addStmt($factory->property('anotherProperty')
68 ->setDefault(array(1, 2, 3))))
77 use Foo\Bar\SomeOtherClass;
79 abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
81 protected $someProperty;
82 private $anotherProperty = array(1, 2, 3);
83 function firstMethod()
87 * This method does something.
89 * @param SomeClass And takes a parameter
91 public abstract function someMethod(SomeClass $someParam);
92 protected function anotherMethod($someParam = 'test')
99 $stmts = array($node);
100 $prettyPrinter = new PrettyPrinter\Standard();
101 $generated = $prettyPrinter->prettyPrintFile($stmts);
104 str_replace("\r\n", "\n", $expected),
105 str_replace("\r\n", "\n", $generated)