php-parser/test/PhpParser/Builder/TraitTest.php

50 lines
1.4 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace PhpParser\Builder;
use PhpParser\Comment;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
2017-04-27 18:14:07 +02:00
use PHPUnit\Framework\TestCase;
2017-04-27 18:14:07 +02:00
class TraitTest extends TestCase
{
protected function createTraitBuilder($class) {
return new Trait_($class);
}
public function testStmtAddition() {
$method1 = new Stmt\ClassMethod('test1');
$method2 = new Stmt\ClassMethod('test2');
$method3 = new Stmt\ClassMethod('test3');
2017-08-13 14:35:03 +02:00
$prop = new Stmt\Property(Stmt\Class_::MODIFIER_PUBLIC, [
2015-07-11 22:31:45 +02:00
new Stmt\PropertyProperty('test')
2017-08-13 14:35:03 +02:00
]);
$use = new Stmt\TraitUse([new Name('OtherTrait')]);
$trait = $this->createTraitBuilder('TestTrait')
->setDocComment('/** Nice trait */')
->addStmt($method1)
2017-08-13 14:35:03 +02:00
->addStmts([$method2, $method3])
2015-07-11 22:31:45 +02:00
->addStmt($prop)
->addStmt($use)
->getNode();
2017-08-13 14:35:03 +02:00
$this->assertEquals(new Stmt\Trait_('TestTrait', [
'stmts' => [$use, $prop, $method1, $method2, $method3]
2017-08-13 14:35:03 +02:00
], [
'comments' => [
new Comment\Doc('/** Nice trait */')
2017-08-13 14:35:03 +02:00
]
]), $trait);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Unexpected node of type "Stmt_Echo"
*/
public function testInvalidStmtError() {
$this->createTraitBuilder('Test')
2017-08-13 14:35:03 +02:00
->addStmt(new Stmt\Echo_([]))
;
}
}