InterfaceTest.php 3.9 KB

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