3 namespace PhpParser\Builder;
6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Scalar;
9 class ParamTest extends \PHPUnit_Framework_TestCase
11 public function createParamBuilder($name) {
12 return new Param($name);
16 * @dataProvider provideTestDefaultValues
18 public function testDefaultValues($value, $expectedValueNode) {
19 $node = $this->createParamBuilder('test')
24 $this->assertEquals($expectedValueNode, $node->default);
27 public function provideTestDefaultValues() {
31 new Expr\ConstFetch(new Node\Name('null'))
35 new Expr\ConstFetch(new Node\Name('true'))
39 new Expr\ConstFetch(new Node\Name('false'))
43 new Scalar\LNumber(31415)
47 new Scalar\DNumber(3.1415)
51 new Scalar\String_('Hallo World')
55 new Expr\Array_(array(
56 new Expr\ArrayItem(new Scalar\LNumber(1)),
57 new Expr\ArrayItem(new Scalar\LNumber(2)),
58 new Expr\ArrayItem(new Scalar\LNumber(3)),
62 array('foo' => 'bar', 'bar' => 'foo'),
63 new Expr\Array_(array(
65 new Scalar\String_('bar'),
66 new Scalar\String_('foo')
69 new Scalar\String_('foo'),
70 new Scalar\String_('bar')
75 new Scalar\MagicConst\Dir,
76 new Scalar\MagicConst\Dir
82 * @dataProvider provideTestTypeHints
84 public function testTypeHints($typeHint, $expectedType) {
85 $node = $this->createParamBuilder('test')
86 ->setTypeHint($typeHint)
91 /* Manually implement comparison to avoid __toString stupidity */
92 if ($expectedType instanceof Node\NullableType) {
93 $this->assertInstanceOf(get_class($expectedType), $type);
94 $expectedType = $expectedType->type;
98 if ($expectedType instanceof Node\Name) {
99 $this->assertInstanceOf(get_class($expectedType), $type);
100 $this->assertEquals($expectedType, $type);
102 $this->assertSame($expectedType, $type);
106 public function provideTestTypeHints() {
108 array('array', 'array'),
109 array('callable', 'callable'),
110 array('bool', 'bool'),
112 array('float', 'float'),
113 array('string', 'string'),
114 array('iterable', 'iterable'),
115 array('object', 'object'),
116 array('Array', 'array'),
117 array('CALLABLE', 'callable'),
118 array('Some\Class', new Node\Name('Some\Class')),
119 array('\Foo', new Node\Name\FullyQualified('Foo')),
120 array('self', new Node\Name('self')),
121 array('?array', new Node\NullableType('array')),
122 array('?Some\Class', new Node\NullableType(new Node\Name('Some\Class'))),
123 array(new Node\Name('Some\Class'), new Node\Name('Some\Class')),
124 array(new Node\NullableType('int'), new Node\NullableType('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 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('test', null, null, true),
160 public function testVariadic() {
161 $node = $this->createParamBuilder('test')
167 new Node\Param('test', null, null, false, true),