ClassTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. class PHPParser_Tests_Builder_ClassTest extends PHPUnit_Framework_TestCase
  3. {
  4. protected function createClassBuilder($class) {
  5. return new PHPParser_Builder_Class($class);
  6. }
  7. public function testExtendsImplements() {
  8. $node = $this->createClassBuilder('SomeLogger')
  9. ->extend('BaseLogger')
  10. ->implement('Namespaced\Logger', new PHPParser_Node_Name('SomeInterface'))
  11. ->getNode()
  12. ;
  13. $this->assertEquals(
  14. new PHPParser_Node_Stmt_Class('SomeLogger', array(
  15. 'extends' => new PHPParser_Node_Name('BaseLogger'),
  16. 'implements' => array(
  17. new PHPParser_Node_Name('Namespaced\Logger'),
  18. new PHPParser_Node_Name('SomeInterface')
  19. ),
  20. )),
  21. $node
  22. );
  23. }
  24. public function testAbstract() {
  25. $node = $this->createClassBuilder('Test')
  26. ->makeAbstract()
  27. ->getNode()
  28. ;
  29. $this->assertEquals(
  30. new PHPParser_Node_Stmt_Class('Test', array(
  31. 'type' => PHPParser_Node_Stmt_Class::MODIFIER_ABSTRACT
  32. )),
  33. $node
  34. );
  35. }
  36. public function testFinal() {
  37. $node = $this->createClassBuilder('Test')
  38. ->makeFinal()
  39. ->getNode()
  40. ;
  41. $this->assertEquals(
  42. new PHPParser_Node_Stmt_Class('Test', array(
  43. 'type' => PHPParser_Node_Stmt_Class::MODIFIER_FINAL
  44. )),
  45. $node
  46. );
  47. }
  48. public function testStatementOrder() {
  49. $method = new PHPParser_Node_Stmt_ClassMethod('testMethod');
  50. $property = new PHPParser_Node_Stmt_Property(
  51. PHPParser_Node_Stmt_Class::MODIFIER_PUBLIC,
  52. array(new PHPParser_Node_Stmt_PropertyProperty('testProperty'))
  53. );
  54. $const = new PHPParser_Node_Stmt_ClassConst(array(
  55. new PHPParser_Node_Const('TEST_CONST', new PHPParser_Node_Scalar_String('ABC'))
  56. ));
  57. $use = new PHPParser_Node_Stmt_TraitUse(array(new PHPParser_Node_Name('SomeTrait')));
  58. $node = $this->createClassBuilder('Test')
  59. ->addStmt($method)
  60. ->addStmt($property)
  61. ->addStmts(array($const, $use))
  62. ->getNode()
  63. ;
  64. $this->assertEquals(
  65. new PHPParser_Node_Stmt_Class('Test', array(
  66. 'stmts' => array($use, $const, $property, $method)
  67. )),
  68. $node
  69. );
  70. }
  71. /**
  72. * @expectedException LogicException
  73. * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
  74. */
  75. public function testInvalidStmtError() {
  76. $this->createClassBuilder('Test')
  77. ->addStmt(new PHPParser_Node_Stmt_Echo(array()))
  78. ;
  79. }
  80. }