1 <?php declare(strict_types=1);
3 namespace PhpParser\Builder;
6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Scalar;
8 use PHPUnit\Framework\TestCase;
10 class ParamTest extends TestCase
12 public function createParamBuilder($name) {
13 return new Param($name);
17 * @dataProvider provideTestDefaultValues
19 public function testDefaultValues($value, $expectedValueNode) {
20 $node = $this->createParamBuilder('test')
25 $this->assertEquals($expectedValueNode, $node->default);
28 public function provideTestDefaultValues() {
32 new Expr\ConstFetch(new Node\Name('null'))
36 new Expr\ConstFetch(new Node\Name('true'))
40 new Expr\ConstFetch(new Node\Name('false'))
44 new Scalar\LNumber(31415)
48 new Scalar\DNumber(3.1415)
52 new Scalar\String_('Hallo World')
57 new Expr\ArrayItem(new Scalar\LNumber(1)),
58 new Expr\ArrayItem(new Scalar\LNumber(2)),
59 new Expr\ArrayItem(new Scalar\LNumber(3)),
63 ['foo' => 'bar', 'bar' => 'foo'],
66 new Scalar\String_('bar'),
67 new Scalar\String_('foo')
70 new Scalar\String_('foo'),
71 new Scalar\String_('bar')
76 new Scalar\MagicConst\Dir,
77 new Scalar\MagicConst\Dir
83 * @dataProvider provideTestTypeHints
85 public function testTypeHints($typeHint, $expectedType) {
86 $node = $this->createParamBuilder('test')
87 ->setTypeHint($typeHint)
92 /* Manually implement comparison to avoid __toString stupidity */
93 if ($expectedType instanceof Node\NullableType) {
94 $this->assertInstanceOf(get_class($expectedType), $type);
95 $expectedType = $expectedType->type;
99 $this->assertInstanceOf(get_class($expectedType), $type);
100 $this->assertEquals($expectedType, $type);
103 public function provideTestTypeHints() {
105 ['array', new Node\Identifier('array')],
106 ['callable', new Node\Identifier('callable')],
107 ['bool', new Node\Identifier('bool')],
108 ['int', new Node\Identifier('int')],
109 ['float', new Node\Identifier('float')],
110 ['string', new Node\Identifier('string')],
111 ['iterable', new Node\Identifier('iterable')],
112 ['object', new Node\Identifier('object')],
113 ['Array', new Node\Identifier('array')],
114 ['CALLABLE', new Node\Identifier('callable')],
115 ['Some\Class', new Node\Name('Some\Class')],
116 ['\Foo', new Node\Name\FullyQualified('Foo')],
117 ['self', new Node\Name('self')],
118 ['?array', new Node\NullableType(new Node\Identifier('array'))],
119 ['?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))],
120 [new Node\Name('Some\Class'), new Node\Name('Some\Class')],
122 new Node\NullableType(new Node\Identifier('int')),
123 new Node\NullableType(new Node\Identifier('int'))
126 new Node\NullableType(new Node\Name('Some\Class')),
127 new Node\NullableType(new Node\Name('Some\Class'))
133 * @expectedException \LogicException
134 * @expectedExceptionMessage Parameter type cannot be void
136 public function testVoidTypeError() {
137 $this->createParamBuilder('test')->setTypeHint('void');
141 * @expectedException \LogicException
142 * @expectedExceptionMessage Type must be a string, or an instance of Name, Identifier or NullableType
144 public function testInvalidTypeError() {
145 $this->createParamBuilder('test')->setTypeHint(new \stdClass);
148 public function testByRef() {
149 $node = $this->createParamBuilder('test')
155 new Node\Param(new Expr\Variable('test'), null, null, true),
160 public function testVariadic() {
161 $node = $this->createParamBuilder('test')
167 new Node\Param(new Expr\Variable('test'), null, null, false, true),