InterfaceTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Arg;
  6. use PhpParser\Node\Attribute;
  7. use PhpParser\Node\AttributeGroup;
  8. use PhpParser\Node\Identifier;
  9. use PhpParser\Node\Name;
  10. use PhpParser\Node\Scalar\DNumber;
  11. use PhpParser\Node\Scalar\LNumber;
  12. use PhpParser\Node\Stmt;
  13. class InterfaceTest extends \PHPUnit\Framework\TestCase
  14. {
  15. protected function createInterfaceBuilder() {
  16. return new Interface_('Contract');
  17. }
  18. private function dump($node) {
  19. $pp = new \PhpParser\PrettyPrinter\Standard;
  20. return $pp->prettyPrint([$node]);
  21. }
  22. public function testEmpty() {
  23. $contract = $this->createInterfaceBuilder()->getNode();
  24. $this->assertInstanceOf(Stmt\Interface_::class, $contract);
  25. $this->assertEquals(new Node\Identifier('Contract'), $contract->name);
  26. }
  27. public function testExtending() {
  28. $contract = $this->createInterfaceBuilder()
  29. ->extend('Space\Root1', 'Root2')->getNode();
  30. $this->assertEquals(
  31. new Stmt\Interface_('Contract', [
  32. 'extends' => [
  33. new Node\Name('Space\Root1'),
  34. new Node\Name('Root2')
  35. ],
  36. ]), $contract
  37. );
  38. }
  39. public function testAddMethod() {
  40. $method = new Stmt\ClassMethod('doSomething');
  41. $contract = $this->createInterfaceBuilder()->addStmt($method)->getNode();
  42. $this->assertSame([$method], $contract->stmts);
  43. }
  44. public function testAddConst() {
  45. $const = new Stmt\ClassConst([
  46. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458.0))
  47. ]);
  48. $contract = $this->createInterfaceBuilder()->addStmt($const)->getNode();
  49. $this->assertSame(299792458.0, $contract->stmts[0]->consts[0]->value->value);
  50. }
  51. public function testOrder() {
  52. $const = new Stmt\ClassConst([
  53. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
  54. ]);
  55. $method = new Stmt\ClassMethod('doSomething');
  56. $contract = $this->createInterfaceBuilder()
  57. ->addStmt($method)
  58. ->addStmt($const)
  59. ->getNode()
  60. ;
  61. $this->assertInstanceOf(Stmt\ClassConst::class, $contract->stmts[0]);
  62. $this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
  63. }
  64. public function testDocComment() {
  65. $node = $this->createInterfaceBuilder()
  66. ->setDocComment('/** Test */')
  67. ->getNode();
  68. $this->assertEquals(new Stmt\Interface_('Contract', [], [
  69. 'comments' => [new Comment\Doc('/** Test */')]
  70. ]), $node);
  71. }
  72. public function testAddAttribute() {
  73. $attribute = new Attribute(
  74. new Name('Attr'),
  75. [new Arg(new LNumber(1), false, false, [], new Identifier('name'))]
  76. );
  77. $attributeGroup = new AttributeGroup([$attribute]);
  78. $node = $this->createInterfaceBuilder()
  79. ->addAttribute($attributeGroup)
  80. ->getNode();
  81. $this->assertEquals(new Stmt\Interface_('Contract', [
  82. 'attrGroups' => [$attributeGroup],
  83. ], []), $node);
  84. }
  85. public function testInvalidStmtError() {
  86. $this->expectException(\LogicException::class);
  87. $this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
  88. $this->createInterfaceBuilder()->addStmt(new Stmt\PropertyProperty('invalid'));
  89. }
  90. public function testFullFunctional() {
  91. $const = new Stmt\ClassConst([
  92. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
  93. ]);
  94. $method = new Stmt\ClassMethod('doSomething');
  95. $contract = $this->createInterfaceBuilder()
  96. ->addStmt($method)
  97. ->addStmt($const)
  98. ->getNode()
  99. ;
  100. eval($this->dump($contract));
  101. $this->assertTrue(interface_exists('Contract', false));
  102. }
  103. }