Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 use PhpParser\Node\Expr;
|
Chris@0
|
6
|
Chris@0
|
7 class BuilderFactoryTest extends \PHPUnit_Framework_TestCase
|
Chris@0
|
8 {
|
Chris@0
|
9 /**
|
Chris@0
|
10 * @dataProvider provideTestFactory
|
Chris@0
|
11 */
|
Chris@0
|
12 public function testFactory($methodName, $className) {
|
Chris@0
|
13 $factory = new BuilderFactory;
|
Chris@0
|
14 $this->assertInstanceOf($className, $factory->$methodName('test'));
|
Chris@0
|
15 }
|
Chris@0
|
16
|
Chris@0
|
17 public function provideTestFactory() {
|
Chris@0
|
18 return array(
|
Chris@0
|
19 array('namespace', 'PhpParser\Builder\Namespace_'),
|
Chris@0
|
20 array('class', 'PhpParser\Builder\Class_'),
|
Chris@0
|
21 array('interface', 'PhpParser\Builder\Interface_'),
|
Chris@0
|
22 array('trait', 'PhpParser\Builder\Trait_'),
|
Chris@0
|
23 array('method', 'PhpParser\Builder\Method'),
|
Chris@0
|
24 array('function', 'PhpParser\Builder\Function_'),
|
Chris@0
|
25 array('property', 'PhpParser\Builder\Property'),
|
Chris@0
|
26 array('param', 'PhpParser\Builder\Param'),
|
Chris@0
|
27 array('use', 'PhpParser\Builder\Use_'),
|
Chris@0
|
28 );
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 public function testNonExistingMethod() {
|
Chris@0
|
32 $this->setExpectedException('LogicException', 'Method "foo" does not exist');
|
Chris@0
|
33 $factory = new BuilderFactory();
|
Chris@0
|
34 $factory->foo();
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 public function testIntegration() {
|
Chris@0
|
38 $factory = new BuilderFactory;
|
Chris@0
|
39 $node = $factory->namespace('Name\Space')
|
Chris@0
|
40 ->addStmt($factory->use('Foo\Bar\SomeOtherClass'))
|
Chris@0
|
41 ->addStmt($factory->use('Foo\Bar')->as('A'))
|
Chris@0
|
42 ->addStmt($factory
|
Chris@0
|
43 ->class('SomeClass')
|
Chris@0
|
44 ->extend('SomeOtherClass')
|
Chris@0
|
45 ->implement('A\Few', '\Interfaces')
|
Chris@0
|
46 ->makeAbstract()
|
Chris@0
|
47
|
Chris@0
|
48 ->addStmt($factory->method('firstMethod'))
|
Chris@0
|
49
|
Chris@0
|
50 ->addStmt($factory->method('someMethod')
|
Chris@0
|
51 ->makePublic()
|
Chris@0
|
52 ->makeAbstract()
|
Chris@0
|
53 ->addParam($factory->param('someParam')->setTypeHint('SomeClass'))
|
Chris@0
|
54 ->setDocComment('/**
|
Chris@0
|
55 * This method does something.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @param SomeClass And takes a parameter
|
Chris@0
|
58 */'))
|
Chris@0
|
59
|
Chris@0
|
60 ->addStmt($factory->method('anotherMethod')
|
Chris@0
|
61 ->makeProtected()
|
Chris@0
|
62 ->addParam($factory->param('someParam')->setDefault('test'))
|
Chris@0
|
63 ->addStmt(new Expr\Print_(new Expr\Variable('someParam'))))
|
Chris@0
|
64
|
Chris@0
|
65 ->addStmt($factory->property('someProperty')->makeProtected())
|
Chris@0
|
66 ->addStmt($factory->property('anotherProperty')
|
Chris@0
|
67 ->makePrivate()
|
Chris@0
|
68 ->setDefault(array(1, 2, 3))))
|
Chris@0
|
69 ->getNode()
|
Chris@0
|
70 ;
|
Chris@0
|
71
|
Chris@0
|
72 $expected = <<<'EOC'
|
Chris@0
|
73 <?php
|
Chris@0
|
74
|
Chris@0
|
75 namespace Name\Space;
|
Chris@0
|
76
|
Chris@0
|
77 use Foo\Bar\SomeOtherClass;
|
Chris@0
|
78 use Foo\Bar as A;
|
Chris@0
|
79 abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces
|
Chris@0
|
80 {
|
Chris@0
|
81 protected $someProperty;
|
Chris@0
|
82 private $anotherProperty = array(1, 2, 3);
|
Chris@0
|
83 function firstMethod()
|
Chris@0
|
84 {
|
Chris@0
|
85 }
|
Chris@0
|
86 /**
|
Chris@0
|
87 * This method does something.
|
Chris@0
|
88 *
|
Chris@0
|
89 * @param SomeClass And takes a parameter
|
Chris@0
|
90 */
|
Chris@0
|
91 public abstract function someMethod(SomeClass $someParam);
|
Chris@0
|
92 protected function anotherMethod($someParam = 'test')
|
Chris@0
|
93 {
|
Chris@0
|
94 print $someParam;
|
Chris@0
|
95 }
|
Chris@0
|
96 }
|
Chris@0
|
97 EOC;
|
Chris@0
|
98
|
Chris@0
|
99 $stmts = array($node);
|
Chris@0
|
100 $prettyPrinter = new PrettyPrinter\Standard();
|
Chris@0
|
101 $generated = $prettyPrinter->prettyPrintFile($stmts);
|
Chris@0
|
102
|
Chris@0
|
103 $this->assertEquals(
|
Chris@0
|
104 str_replace("\r\n", "\n", $expected),
|
Chris@0
|
105 str_replace("\r\n", "\n", $generated)
|
Chris@0
|
106 );
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|