InterfaceTest.php 3.1 KB

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