2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Comment;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Name;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Scalar;
|
2016-11-23 22:58:18 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class PropertyTest extends \PHPUnit\Framework\TestCase
|
2014-02-06 14:44:16 +01:00
|
|
|
{
|
|
|
|
public function createPropertyBuilder($name) {
|
|
|
|
return new Property($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testModifiers() {
|
|
|
|
$node = $this->createPropertyBuilder('test')
|
|
|
|
->makePrivate()
|
|
|
|
->makeStatic()
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
new Stmt\Property(
|
|
|
|
Stmt\Class_::MODIFIER_PRIVATE
|
|
|
|
| Stmt\Class_::MODIFIER_STATIC,
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
new Stmt\PropertyProperty('test')
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
2014-02-06 14:44:16 +01:00
|
|
|
),
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$node = $this->createPropertyBuilder('test')
|
|
|
|
->makeProtected()
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
new Stmt\Property(
|
|
|
|
Stmt\Class_::MODIFIER_PROTECTED,
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
new Stmt\PropertyProperty('test')
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
2014-02-06 14:44:16 +01:00
|
|
|
),
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$node = $this->createPropertyBuilder('test')
|
|
|
|
->makePublic()
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
new Stmt\Property(
|
|
|
|
Stmt\Class_::MODIFIER_PUBLIC,
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
new Stmt\PropertyProperty('test')
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
2014-02-06 14:44:16 +01:00
|
|
|
),
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-12-13 13:44:40 +01:00
|
|
|
public function testDocComment() {
|
|
|
|
$node = $this->createPropertyBuilder('test')
|
|
|
|
->setDocComment('/** Test */')
|
|
|
|
->getNode();
|
|
|
|
|
|
|
|
$this->assertEquals(new Stmt\Property(
|
|
|
|
Stmt\Class_::MODIFIER_PUBLIC,
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-12-13 13:44:40 +01:00
|
|
|
new Stmt\PropertyProperty('test')
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'comments' => [new Comment\Doc('/** Test */')]
|
|
|
|
]
|
2014-12-13 13:44:40 +01:00
|
|
|
), $node);
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestDefaultValues
|
|
|
|
*/
|
|
|
|
public function testDefaultValues($value, $expectedValueNode) {
|
|
|
|
$node = $this->createPropertyBuilder('test')
|
|
|
|
->setDefault($value)
|
|
|
|
->getNode()
|
|
|
|
;
|
|
|
|
|
|
|
|
$this->assertEquals($expectedValueNode, $node->props[0]->default);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestDefaultValues() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
null,
|
|
|
|
new Expr\ConstFetch(new Name('null'))
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
true,
|
|
|
|
new Expr\ConstFetch(new Name('true'))
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
false,
|
|
|
|
new Expr\ConstFetch(new Name('false'))
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
31415,
|
|
|
|
new Scalar\LNumber(31415)
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
3.1415,
|
|
|
|
new Scalar\DNumber(3.1415)
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
'Hallo World',
|
2015-03-20 21:47:20 +01:00
|
|
|
new Scalar\String_('Hallo World')
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
[1, 2, 3],
|
|
|
|
new Expr\Array_([
|
2014-02-06 14:44:16 +01:00
|
|
|
new Expr\ArrayItem(new Scalar\LNumber(1)),
|
|
|
|
new Expr\ArrayItem(new Scalar\LNumber(2)),
|
|
|
|
new Expr\ArrayItem(new Scalar\LNumber(3)),
|
2017-08-13 14:35:03 +02:00
|
|
|
])
|
|
|
|
],
|
|
|
|
[
|
|
|
|
['foo' => 'bar', 'bar' => 'foo'],
|
|
|
|
new Expr\Array_([
|
2014-02-06 14:44:16 +01:00
|
|
|
new Expr\ArrayItem(
|
2015-03-20 21:47:20 +01:00
|
|
|
new Scalar\String_('bar'),
|
|
|
|
new Scalar\String_('foo')
|
2014-02-06 14:44:16 +01:00
|
|
|
),
|
|
|
|
new Expr\ArrayItem(
|
2015-03-20 21:47:20 +01:00
|
|
|
new Scalar\String_('foo'),
|
|
|
|
new Scalar\String_('bar')
|
2014-02-06 14:44:16 +01:00
|
|
|
),
|
2017-08-13 14:35:03 +02:00
|
|
|
])
|
|
|
|
],
|
|
|
|
[
|
2014-02-06 14:44:16 +01:00
|
|
|
new Scalar\MagicConst\Dir,
|
|
|
|
new Scalar\MagicConst\Dir
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
|
|
|
];
|
2014-02-06 14:44:16 +01:00
|
|
|
}
|
2015-03-20 21:47:20 +01:00
|
|
|
}
|