菜谱项目

InterfaceTest.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Scalar\DNumber;
  6. use PhpParser\Node\Stmt;
  7. class InterfaceTest extends \PHPUnit_Framework_TestCase
  8. {
  9. /** @var Interface_ */
  10. protected $builder;
  11. protected function setUp() {
  12. $this->builder = new Interface_('Contract');
  13. }
  14. private function dump($node) {
  15. $pp = new \PhpParser\PrettyPrinter\Standard;
  16. return $pp->prettyPrint(array($node));
  17. }
  18. public function testEmpty() {
  19. $contract = $this->builder->getNode();
  20. $this->assertInstanceOf('PhpParser\Node\Stmt\Interface_', $contract);
  21. $this->assertSame('Contract', $contract->name);
  22. }
  23. public function testExtending() {
  24. $contract = $this->builder->extend('Space\Root1', 'Root2')->getNode();
  25. $this->assertEquals(
  26. new Stmt\Interface_('Contract', array(
  27. 'extends' => array(
  28. new Node\Name('Space\Root1'),
  29. new Node\Name('Root2')
  30. ),
  31. )), $contract
  32. );
  33. }
  34. public function testAddMethod() {
  35. $method = new Stmt\ClassMethod('doSomething');
  36. $contract = $this->builder->addStmt($method)->getNode();
  37. $this->assertSame(array($method), $contract->stmts);
  38. }
  39. public function testAddConst() {
  40. $const = new Stmt\ClassConst(array(
  41. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
  42. ));
  43. $contract = $this->builder->addStmt($const)->getNode();
  44. $this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
  45. }
  46. public function testOrder() {
  47. $const = new Stmt\ClassConst(array(
  48. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
  49. ));
  50. $method = new Stmt\ClassMethod('doSomething');
  51. $contract = $this->builder
  52. ->addStmt($method)
  53. ->addStmt($const)
  54. ->getNode()
  55. ;
  56. $this->assertInstanceOf('PhpParser\Node\Stmt\ClassConst', $contract->stmts[0]);
  57. $this->assertInstanceOf('PhpParser\Node\Stmt\ClassMethod', $contract->stmts[1]);
  58. }
  59. public function testDocComment() {
  60. $node = $this->builder
  61. ->setDocComment('/** Test */')
  62. ->getNode();
  63. $this->assertEquals(new Stmt\Interface_('Contract', array(), array(
  64. 'comments' => array(new Comment\Doc('/** Test */'))
  65. )), $node);
  66. }
  67. /**
  68. * @expectedException \LogicException
  69. * @expectedExceptionMessage Unexpected node of type "Stmt_PropertyProperty"
  70. */
  71. public function testInvalidStmtError() {
  72. $this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
  73. }
  74. public function testFullFunctional() {
  75. $const = new Stmt\ClassConst(array(
  76. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
  77. ));
  78. $method = new Stmt\ClassMethod('doSomething');
  79. $contract = $this->builder
  80. ->addStmt($method)
  81. ->addStmt($const)
  82. ->getNode()
  83. ;
  84. eval($this->dump($contract));
  85. $this->assertTrue(interface_exists('Contract', false));
  86. }
  87. }