3 namespace PhpParser\Builder;
7 use PhpParser\Node\Name;
8 use PhpParser\Node\Stmt;
10 class ClassTest extends \PHPUnit_Framework_TestCase
12 protected function createClassBuilder($class) {
13 return new Class_($class);
16 public function testExtendsImplements() {
17 $node = $this->createClassBuilder('SomeLogger')
18 ->extend('BaseLogger')
19 ->implement('Namespaced\Logger', new Name('SomeInterface'))
20 ->implement('\Fully\Qualified', 'namespace\NamespaceRelative')
25 new Stmt\Class_('SomeLogger', array(
26 'extends' => new Name('BaseLogger'),
27 'implements' => array(
28 new Name('Namespaced\Logger'),
29 new Name('SomeInterface'),
30 new Name\FullyQualified('Fully\Qualified'),
31 new Name\Relative('NamespaceRelative'),
38 public function testAbstract() {
39 $node = $this->createClassBuilder('Test')
45 new Stmt\Class_('Test', array(
46 'flags' => Stmt\Class_::MODIFIER_ABSTRACT
52 public function testFinal() {
53 $node = $this->createClassBuilder('Test')
59 new Stmt\Class_('Test', array(
60 'flags' => Stmt\Class_::MODIFIER_FINAL
66 public function testStatementOrder() {
67 $method = new Stmt\ClassMethod('testMethod');
68 $property = new Stmt\Property(
69 Stmt\Class_::MODIFIER_PUBLIC,
70 array(new Stmt\PropertyProperty('testProperty'))
72 $const = new Stmt\ClassConst(array(
73 new Node\Const_('TEST_CONST', new Node\Scalar\String_('ABC'))
75 $use = new Stmt\TraitUse(array(new Name('SomeTrait')));
77 $node = $this->createClassBuilder('Test')
80 ->addStmts(array($const, $use))
85 new Stmt\Class_('Test', array(
86 'stmts' => array($use, $const, $property, $method)
92 public function testDocComment() {
93 $docComment = <<<'DOC'
98 $class = $this->createClassBuilder('Test')
99 ->setDocComment($docComment)
103 new Stmt\Class_('Test', array(), array(
105 new Comment\Doc($docComment)
111 $class = $this->createClassBuilder('Test')
112 ->setDocComment(new Comment\Doc($docComment))
116 new Stmt\Class_('Test', array(), array(
118 new Comment\Doc($docComment)
126 * @expectedException \LogicException
127 * @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
129 public function testInvalidStmtError() {
130 $this->createClassBuilder('Test')
131 ->addStmt(new Stmt\Echo_(array()))
136 * @expectedException \LogicException
137 * @expectedExceptionMessage Doc comment must be a string or an instance of PhpParser\Comment\Doc
139 public function testInvalidDocComment() {
140 $this->createClassBuilder('Test')
141 ->setDocComment(new Comment('Test'));
145 * @expectedException \LogicException
146 * @expectedExceptionMessage Name cannot be empty
148 public function testEmptyName() {
149 $this->createClassBuilder('Test')
154 * @expectedException \LogicException
155 * @expectedExceptionMessage Name must be a string or an instance of PhpParser\Node\Name
157 public function testInvalidName() {
158 $this->createClassBuilder('Test')
159 ->extend(array('Foo'));