ClassTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Builder;
  3. use PhpParser\Comment;
  4. use PhpParser\Node;
  5. use PhpParser\Node\Name;
  6. use PhpParser\Node\Stmt;
  7. use PHPUnit\Framework\TestCase;
  8. class ClassTest extends TestCase
  9. {
  10. protected function createClassBuilder($class) {
  11. return new Class_($class);
  12. }
  13. public function testExtendsImplements() {
  14. $node = $this->createClassBuilder('SomeLogger')
  15. ->extend('BaseLogger')
  16. ->implement('Namespaced\Logger', new Name('SomeInterface'))
  17. ->implement('\Fully\Qualified', 'namespace\NamespaceRelative')
  18. ->getNode()
  19. ;
  20. $this->assertEquals(
  21. new Stmt\Class_('SomeLogger', [
  22. 'extends' => new Name('BaseLogger'),
  23. 'implements' => [
  24. new Name('Namespaced\Logger'),
  25. new Name('SomeInterface'),
  26. new Name\FullyQualified('Fully\Qualified'),
  27. new Name\Relative('NamespaceRelative'),
  28. ],
  29. ]),
  30. $node
  31. );
  32. }
  33. public function testAbstract() {
  34. $node = $this->createClassBuilder('Test')
  35. ->makeAbstract()
  36. ->getNode()
  37. ;
  38. $this->assertEquals(
  39. new Stmt\Class_('Test', [
  40. 'flags' => Stmt\Class_::MODIFIER_ABSTRACT
  41. ]),
  42. $node
  43. );
  44. }
  45. public function testFinal() {
  46. $node = $this->createClassBuilder('Test')
  47. ->makeFinal()
  48. ->getNode()
  49. ;
  50. $this->assertEquals(
  51. new Stmt\Class_('Test', [
  52. 'flags' => Stmt\Class_::MODIFIER_FINAL
  53. ]),
  54. $node
  55. );
  56. }
  57. public function testStatementOrder() {
  58. $method = new Stmt\ClassMethod('testMethod');
  59. $property = new Stmt\Property(
  60. Stmt\Class_::MODIFIER_PUBLIC,
  61. [new Stmt\PropertyProperty('testProperty')]
  62. );
  63. $const = new Stmt\ClassConst([
  64. new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC'))
  65. ]);
  66. $use = new Stmt\TraitUse([new Name('SomeTrait')]);
  67. $node = $this->createClassBuilder('Test')
  68. ->addStmt($method)
  69. ->addStmt($property)
  70. ->addStmts([$const, $use])
  71. ->getNode()
  72. ;
  73. $this->assertEquals(
  74. new Stmt\Class_('Test', [
  75. 'stmts' => [$use, $const, $property, $method]
  76. ]),
  77. $node
  78. );
  79. }
  80. public function testDocComment() {
  81. $docComment = <<<'DOC'
  82. /**
  83. * Test
  84. */
  85. DOC;
  86. $class = $this->createClassBuilder('Test')
  87. ->setDocComment($docComment)
  88. ->getNode();
  89. $this->assertEquals(
  90. new Stmt\Class_('Test', [], [
  91. 'comments' => [
  92. new Comment\Doc($docComment)
  93. ]
  94. ]),
  95. $class
  96. );
  97. $class = $this->createClassBuilder('Test')
  98. ->setDocComment(new Comment\Doc($docComment))
  99. ->getNode();
  100. $this->assertEquals(
  101. new Stmt\Class_('Test', [], [
  102. 'comments' => [
  103. new Comment\Doc($docComment)
  104. ]
  105. ]),
  106. $class
  107. );
  108. }
  109. public function testInvalidStmtError() {
  110. $this->expectException(\LogicException::class);
  111. $this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
  112. $this->createClassBuilder('Test')
  113. ->addStmt(new Stmt\Echo_([]))
  114. ;
  115. }
  116. public function testInvalidDocComment() {
  117. $this->expectException(\LogicException::class);
  118. $this->expectExceptionMessage('Doc comment must be a string or an instance of PhpParser\Comment\Doc');
  119. $this->createClassBuilder('Test')
  120. ->setDocComment(new Comment('Test'));
  121. }
  122. public function testEmptyName() {
  123. $this->expectException(\LogicException::class);
  124. $this->expectExceptionMessage('Name cannot be empty');
  125. $this->createClassBuilder('Test')
  126. ->extend('');
  127. }
  128. public function testInvalidName() {
  129. $this->expectException(\LogicException::class);
  130. $this->expectExceptionMessage('Name must be a string or an instance of Node\Name');
  131. $this->createClassBuilder('Test')
  132. ->extend(['Foo']);
  133. }
  134. }