菜谱项目

ClassTest.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  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', array(
  21. 'extends' => new Name('BaseLogger'),
  22. 'implements' => array(
  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', array(
  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', array(
  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. array(new Stmt\PropertyProperty('testProperty'))
  61. );
  62. $const = new Stmt\ClassConst(array(
  63. new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC'))
  64. ));
  65. $use = new Stmt\TraitUse(array(new Name('SomeTrait')));
  66. $node = $this->createClassBuilder('Test')
  67. ->addStmt($method)
  68. ->addStmt($property)
  69. ->addStmts(array($const, $use))
  70. ->getNode()
  71. ;
  72. $this->assertEquals(
  73. new Stmt\Class_('Test', array(
  74. 'stmts' => array($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', array(), array(
  90. 'comments' => array(
  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', array(), array(
  101. 'comments' => array(
  102. new Comment\Doc($docComment)
  103. )
  104. )),
  105. $class
  106. );
  107. }
  108. /**
  109. * @expectedException \LogicException
  110. * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
  111. */
  112. public function testInvalidStmtError() {
  113. $this->createClassBuilder('Test')
  114. ->addStmt(new Stmt\Echo_(array()))
  115. ;
  116. }
  117. /**
  118. * @expectedException \LogicException
  119. * @expectedExceptionMessage Doc comment must be a string or an instance of PhpParser\Comment\Doc
  120. */
  121. public function testInvalidDocComment() {
  122. $this->createClassBuilder('Test')
  123. ->setDocComment(new Comment('Test'));
  124. }
  125. /**
  126. * @expectedException \LogicException
  127. * @expectedExceptionMessage Name cannot be empty
  128. */
  129. public function testEmptyName() {
  130. $this->createClassBuilder('Test')
  131. ->extend('');
  132. }
  133. /**
  134. * @expectedException \LogicException
  135. * @expectedExceptionMessage Name must be a string or an instance of PhpParser\Node\Name
  136. */
  137. public function testInvalidName() {
  138. $this->createClassBuilder('Test')
  139. ->extend(array('Foo'));
  140. }
  141. }