InterfaceTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * This class unit-tests the interface builder
  4. */
  5. class PHPParser_Tests_Builder_InterfaceTest extends PHPUnit_Framework_TestCase
  6. {
  7. /** @var PHPParser_Builder_Interface */
  8. protected $builder;
  9. protected function setUp() {
  10. $this->builder = new PHPParser_Builder_Interface('Contract');
  11. }
  12. private function dump($node) {
  13. $pp = new PHPParser_PrettyPrinter_Default();
  14. return $pp->prettyPrint(array($node));
  15. }
  16. public function testEmpty() {
  17. $contract = $this->builder->getNode();
  18. $this->assertInstanceOf('PHPParser_Node_Stmt_Interface', $contract);
  19. $this->assertEquals('Contract', $contract->name);
  20. }
  21. public function testExtending() {
  22. $contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
  23. $this->assertEquals(
  24. new PHPParser_Node_Stmt_Interface('Contract', array(
  25. 'extends' => array(
  26. new PHPParser_Node_Name('Space\Root1'),
  27. new PHPParser_Node_Name('Root2')
  28. ),
  29. )), $contract
  30. );
  31. }
  32. public function testAddMethod() {
  33. $method = new PHPParser_Node_Stmt_ClassMethod('doSomething');
  34. $contract = $this->builder->addStmt($method)->getNode();
  35. $this->assertEquals(array($method), $contract->stmts);
  36. }
  37. public function testAddConst() {
  38. $const = new PHPParser_Node_Stmt_ClassConst(array(
  39. new PHPParser_Node_Const('SPEED_OF_LIGHT', new PHPParser_Node_Scalar_DNumber(299792458))
  40. ));
  41. $contract = $this->builder->addStmt($const)->getNode();
  42. $this->assertEquals(299792458, $contract->stmts[0]->consts[0]->value->value);
  43. }
  44. public function testOrder() {
  45. $const = new PHPParser_Node_Stmt_ClassConst(array(
  46. new PHPParser_Node_Const('SPEED_OF_LIGHT', new PHPParser_Node_Scalar_DNumber(299792458))
  47. ));
  48. $method = new PHPParser_Node_Stmt_ClassMethod('doSomething');
  49. $contract = $this->builder
  50. ->addStmt($method)
  51. ->addStmt($const)
  52. ->getNode()
  53. ;
  54. $this->assertInstanceOf('PHPParser_Node_Stmt_ClassConst', $contract->stmts[0]);
  55. $this->assertInstanceOf('PHPParser_Node_Stmt_ClassMethod', $contract->stmts[1]);
  56. }
  57. /**
  58. * @expectedException LogicException
  59. * @expectedExceptionMessage Unexpected node of type "Stmt_PropertyProperty"
  60. */
  61. public function testInvalidStmtError() {
  62. $this->builder->addStmt(new PHPParser_Node_Stmt_PropertyProperty('invalid'));
  63. }
  64. public function testFullFunctional() {
  65. $const = new PHPParser_Node_Stmt_ClassConst(array(
  66. new PHPParser_Node_Const('SPEED_OF_LIGHT', new PHPParser_Node_Scalar_DNumber(299792458))
  67. ));
  68. $method = new PHPParser_Node_Stmt_ClassMethod('doSomething');
  69. $contract = $this->builder
  70. ->addStmt($method)
  71. ->addStmt($const)
  72. ->getNode()
  73. ;
  74. eval($this->dump($contract));
  75. $this->assertTrue(interface_exists('Contract', false));
  76. }
  77. }