2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2014-12-19 17:58:47 +01:00
|
|
|
|
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
|
|
|
use PhpParser\Comment;
|
2017-08-29 23:18:59 +02:00
|
|
|
use PhpParser\Node\Name;
|
2014-12-19 17:58:47 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2019-08-30 20:37:35 +02:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassConst;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use PhpParser\Node\Stmt\PropertyProperty;
|
|
|
|
use PhpParser\Node\Stmt\TraitUse;
|
2014-12-19 17:58:47 +01:00
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class TraitTest extends \PHPUnit\Framework\TestCase
|
2014-12-19 17:58:47 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
]);
|
2017-08-29 23:18:59 +02:00
|
|
|
$use = new Stmt\TraitUse([new Name('OtherTrait')]);
|
2014-12-19 17:58:47 +01:00
|
|
|
$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)
|
2017-08-29 23:18:59 +02:00
|
|
|
->addStmt($use)
|
2014-12-19 17:58:47 +01:00
|
|
|
->getNode();
|
2017-08-13 14:35:03 +02:00
|
|
|
$this->assertEquals(new Stmt\Trait_('TestTrait', [
|
2017-08-29 23:18:59 +02:00
|
|
|
'stmts' => [$use, $prop, $method1, $method2, $method3]
|
2017-08-13 14:35:03 +02:00
|
|
|
], [
|
|
|
|
'comments' => [
|
2014-12-19 17:58:47 +01:00
|
|
|
new Comment\Doc('/** Nice trait */')
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
|
|
|
]), $trait);
|
2014-12-19 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidStmtError() {
|
2018-09-22 10:43:54 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage('Unexpected node of type "Stmt_Echo"');
|
2014-12-19 17:58:47 +01:00
|
|
|
$this->createTraitBuilder('Test')
|
2017-08-13 14:35:03 +02:00
|
|
|
->addStmt(new Stmt\Echo_([]))
|
2014-12-19 17:58:47 +01:00
|
|
|
;
|
|
|
|
}
|
2019-08-30 20:37:35 +02:00
|
|
|
|
|
|
|
public function testGetMethods() {
|
|
|
|
$methods = [
|
|
|
|
new ClassMethod('foo'),
|
|
|
|
new ClassMethod('bar'),
|
|
|
|
new ClassMethod('fooBar'),
|
|
|
|
];
|
|
|
|
$trait = new Stmt\Trait_('Foo', [
|
|
|
|
'stmts' => [
|
|
|
|
new TraitUse([]),
|
|
|
|
$methods[0],
|
|
|
|
new ClassConst([]),
|
|
|
|
$methods[1],
|
|
|
|
new Property(0, []),
|
|
|
|
$methods[2],
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($methods, $trait->getMethods());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetProperties()
|
|
|
|
{
|
|
|
|
$properties = [
|
|
|
|
new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('foo')]),
|
|
|
|
new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty('bar')]),
|
|
|
|
];
|
|
|
|
$trait = new Stmt\Trait_('Foo', [
|
|
|
|
'stmts' => [
|
|
|
|
new TraitUse([]),
|
|
|
|
$properties[0],
|
|
|
|
new ClassConst([]),
|
|
|
|
$properties[1],
|
|
|
|
new ClassMethod('fooBar'),
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertSame($properties, $trait->getProperties());
|
|
|
|
}
|
2014-12-19 17:58:47 +01:00
|
|
|
}
|