| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php declare(strict_types=1);
- namespace PhpParser\Builder;
- use PhpParser\Node;
- use PhpParser\Node\Arg;
- use PhpParser\Node\Attribute;
- use PhpParser\Node\AttributeGroup;
- use PhpParser\Node\Expr;
- use PhpParser\Node\Identifier;
- use PhpParser\Node\Name;
- use PhpParser\Node\Scalar;
- use PhpParser\Node\Scalar\LNumber;
- class ParamTest extends \PHPUnit\Framework\TestCase
- {
- public function createParamBuilder($name) {
- return new Param($name);
- }
- /**
- * @dataProvider provideTestDefaultValues
- */
- public function testDefaultValues($value, $expectedValueNode) {
- $node = $this->createParamBuilder('test')
- ->setDefault($value)
- ->getNode()
- ;
- $this->assertEquals($expectedValueNode, $node->default);
- }
- public function provideTestDefaultValues() {
- return [
- [
- null,
- new Expr\ConstFetch(new Node\Name('null'))
- ],
- [
- true,
- new Expr\ConstFetch(new Node\Name('true'))
- ],
- [
- false,
- new Expr\ConstFetch(new Node\Name('false'))
- ],
- [
- 31415,
- new Scalar\LNumber(31415)
- ],
- [
- 3.1415,
- new Scalar\DNumber(3.1415)
- ],
- [
- 'Hallo World',
- new Scalar\String_('Hallo World')
- ],
- [
- [1, 2, 3],
- new Expr\Array_([
- new Expr\ArrayItem(new Scalar\LNumber(1)),
- new Expr\ArrayItem(new Scalar\LNumber(2)),
- new Expr\ArrayItem(new Scalar\LNumber(3)),
- ])
- ],
- [
- ['foo' => 'bar', 'bar' => 'foo'],
- new Expr\Array_([
- new Expr\ArrayItem(
- new Scalar\String_('bar'),
- new Scalar\String_('foo')
- ),
- new Expr\ArrayItem(
- new Scalar\String_('foo'),
- new Scalar\String_('bar')
- ),
- ])
- ],
- [
- new Scalar\MagicConst\Dir,
- new Scalar\MagicConst\Dir
- ]
- ];
- }
- /**
- * @dataProvider provideTestTypes
- * @dataProvider provideTestNullableTypes
- * @dataProvider provideTestUnionTypes
- */
- public function testTypes($typeHint, $expectedType) {
- $node = $this->createParamBuilder('test')
- ->setTypeHint($typeHint)
- ->getNode()
- ;
- $type = $node->type;
- /* Manually implement comparison to avoid __toString stupidity */
- if ($expectedType instanceof Node\NullableType) {
- $this->assertInstanceOf(get_class($expectedType), $type);
- $expectedType = $expectedType->type;
- $type = $type->type;
- }
- $this->assertInstanceOf(get_class($expectedType), $type);
- $this->assertEquals($expectedType, $type);
- }
- public function provideTestTypes() {
- return [
- ['array', new Node\Identifier('array')],
- ['callable', new Node\Identifier('callable')],
- ['bool', new Node\Identifier('bool')],
- ['int', new Node\Identifier('int')],
- ['float', new Node\Identifier('float')],
- ['string', new Node\Identifier('string')],
- ['iterable', new Node\Identifier('iterable')],
- ['object', new Node\Identifier('object')],
- ['Array', new Node\Identifier('array')],
- ['CALLABLE', new Node\Identifier('callable')],
- ['mixed', new Node\Identifier('mixed')],
- ['Some\Class', new Node\Name('Some\Class')],
- ['\Foo', new Node\Name\FullyQualified('Foo')],
- ['self', new Node\Name('self')],
- [new Node\Name('Some\Class'), new Node\Name('Some\Class')],
- ];
- }
- public function provideTestNullableTypes() {
- return [
- ['?array', new Node\NullableType(new Node\Identifier('array'))],
- ['?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))],
- [
- new Node\NullableType(new Node\Identifier('int')),
- new Node\NullableType(new Node\Identifier('int'))
- ],
- [
- new Node\NullableType(new Node\Name('Some\Class')),
- new Node\NullableType(new Node\Name('Some\Class'))
- ],
- ];
- }
- public function provideTestUnionTypes() {
- return [
- [
- new Node\UnionType([
- new Node\Name('Some\Class'),
- new Node\Identifier('array'),
- ]),
- new Node\UnionType([
- new Node\Name('Some\Class'),
- new Node\Identifier('array'),
- ]),
- ],
- [
- new Node\UnionType([
- new Node\Identifier('self'),
- new Node\Identifier('array'),
- new Node\Name\FullyQualified('Foo')
- ]),
- new Node\UnionType([
- new Node\Identifier('self'),
- new Node\Identifier('array'),
- new Node\Name\FullyQualified('Foo')
- ]),
- ],
- ];
- }
- public function testVoidTypeError() {
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage('Parameter type cannot be void');
- $this->createParamBuilder('test')->setType('void');
- }
- public function testInvalidTypeError() {
- $this->expectException(\LogicException::class);
- $this->expectExceptionMessage('Type must be a string, or an instance of Name, Identifier or ComplexType');
- $this->createParamBuilder('test')->setType(new \stdClass);
- }
- public function testByRef() {
- $node = $this->createParamBuilder('test')
- ->makeByRef()
- ->getNode()
- ;
- $this->assertEquals(
- new Node\Param(new Expr\Variable('test'), null, null, true),
- $node
- );
- }
- public function testVariadic() {
- $node = $this->createParamBuilder('test')
- ->makeVariadic()
- ->getNode()
- ;
- $this->assertEquals(
- new Node\Param(new Expr\Variable('test'), null, null, false, true),
- $node
- );
- }
- public function testMakePublic() {
- $node = $this->createParamBuilder('test')
- ->makePublic()
- ->getNode()
- ;
- $this->assertEquals(
- new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_PUBLIC),
- $node
- );
- }
- public function testMakeProtected() {
- $node = $this->createParamBuilder('test')
- ->makeProtected()
- ->getNode()
- ;
- $this->assertEquals(
- new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_PROTECTED),
- $node
- );
- }
- public function testMakePrivate() {
- $node = $this->createParamBuilder('test')
- ->makePrivate()
- ->getNode()
- ;
- $this->assertEquals(
- new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_PRIVATE),
- $node
- );
- }
- public function testMakeReadonly() {
- $node = $this->createParamBuilder('test')
- ->makeReadonly()
- ->getNode()
- ;
- $this->assertEquals(
- new Node\Param(new Expr\Variable('test'), null, null, false, false, [], Node\Stmt\Class_::MODIFIER_READONLY),
- $node
- );
- }
- public function testAddAttribute() {
- $attribute = new Attribute(
- new Name('Attr'),
- [new Arg(new LNumber(1), false, false, [], new Identifier('name'))]
- );
- $attributeGroup = new AttributeGroup([$attribute]);
- $node = $this->createParamBuilder('attributeGroup')
- ->addAttribute($attributeGroup)
- ->getNode();
- $this->assertEquals(
- new Node\Param(new Expr\Variable('attributeGroup'), null, null, false, false, [], 0, [$attributeGroup]),
- $node
- );
- }
- }
|