InterfaceTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /** @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([$node]);
  17. }
  18. public function testEmpty() {
  19. $contract = $this->builder->getNode();
  20. $this->assertInstanceOf(Stmt\Interface_::class, $contract);
  21. $this->assertEquals(new Node\Identifier('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', [
  27. 'extends' => [
  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([$method], $contract->stmts);
  38. }
  39. public function testAddConst() {
  40. $const = new Stmt\ClassConst([
  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([
  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(Stmt\ClassConst::class, $contract->stmts[0]);
  57. $this->assertInstanceOf(Stmt\ClassMethod::class, $contract->stmts[1]);
  58. }
  59. public function testDocComment() {
  60. $node = $this->builder
  61. ->setDocComment('/** Test */')
  62. ->getNode();
  63. $this->assertEquals(new Stmt\Interface_('Contract', [], [
  64. 'comments' => [new Comment\Doc('/** Test */')]
  65. ]), $node);
  66. }
  67. public function testInvalidStmtError() {
  68. $this->expectException(\LogicException::class);
  69. $this->expectExceptionMessage('Unexpected node of type "Stmt_PropertyProperty"');
  70. $this->builder->addStmt(new Stmt\PropertyProperty('invalid'));
  71. }
  72. public function testFullFunctional() {
  73. $const = new Stmt\ClassConst([
  74. new Node\Const_('SPEED_OF_LIGHT', new DNumber(299792458))
  75. ]);
  76. $method = new Stmt\ClassMethod('doSomething');
  77. $contract = $this->builder
  78. ->addStmt($method)
  79. ->addStmt($const)
  80. ->getNode()
  81. ;
  82. eval($this->dump($contract));
  83. $this->assertTrue(interface_exists('Contract', false));
  84. }
  85. }