ClassTest.php 4.3 KB

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