BuilderFactoryTest.php 3.5 KB

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