Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InterfaceTest.php 3.2KB

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