Security update for Core, with self-updated composer
[yaffs-website] / vendor / nikic / php-parser / test / PhpParser / Builder / ParamTest.php
1 <?php
2
3 namespace PhpParser\Builder;
4
5 use PhpParser\Node;
6 use PhpParser\Node\Expr;
7 use PhpParser\Node\Scalar;
8
9 class ParamTest extends \PHPUnit_Framework_TestCase
10 {
11     public function createParamBuilder($name) {
12         return new Param($name);
13     }
14
15     /**
16      * @dataProvider provideTestDefaultValues
17      */
18     public function testDefaultValues($value, $expectedValueNode) {
19         $node = $this->createParamBuilder('test')
20             ->setDefault($value)
21             ->getNode()
22         ;
23
24         $this->assertEquals($expectedValueNode, $node->default);
25     }
26
27     public function provideTestDefaultValues() {
28         return array(
29             array(
30                 null,
31                 new Expr\ConstFetch(new Node\Name('null'))
32             ),
33             array(
34                 true,
35                 new Expr\ConstFetch(new Node\Name('true'))
36             ),
37             array(
38                 false,
39                 new Expr\ConstFetch(new Node\Name('false'))
40             ),
41             array(
42                 31415,
43                 new Scalar\LNumber(31415)
44             ),
45             array(
46                 3.1415,
47                 new Scalar\DNumber(3.1415)
48             ),
49             array(
50                 'Hallo World',
51                 new Scalar\String_('Hallo World')
52             ),
53             array(
54                 array(1, 2, 3),
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)),
59                 ))
60             ),
61             array(
62                 array('foo' => 'bar', 'bar' => 'foo'),
63                 new Expr\Array_(array(
64                     new Expr\ArrayItem(
65                         new Scalar\String_('bar'),
66                         new Scalar\String_('foo')
67                     ),
68                     new Expr\ArrayItem(
69                         new Scalar\String_('foo'),
70                         new Scalar\String_('bar')
71                     ),
72                 ))
73             ),
74             array(
75                 new Scalar\MagicConst\Dir,
76                 new Scalar\MagicConst\Dir
77             )
78         );
79     }
80
81     /**
82      * @dataProvider provideTestTypeHints
83      */
84     public function testTypeHints($typeHint, $expectedType) {
85         $node = $this->createParamBuilder('test')
86             ->setTypeHint($typeHint)
87             ->getNode()
88         ;
89         $type = $node->type;
90
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;
95             $type = $type->type;
96         }
97
98         if ($expectedType instanceof Node\Name) {
99             $this->assertInstanceOf(get_class($expectedType), $type);
100             $this->assertEquals($expectedType, $type);
101         } else {
102             $this->assertSame($expectedType, $type);
103         }
104     }
105
106     public function provideTestTypeHints() {
107         return array(
108             array('array', 'array'),
109             array('callable', 'callable'),
110             array('bool', 'bool'),
111             array('int', 'int'),
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')),
125             array(
126                 new Node\NullableType(new Node\Name('Some\Class')),
127                 new Node\NullableType(new Node\Name('Some\Class'))
128             ),
129         );
130     }
131
132     /**
133      * @expectedException \LogicException
134      * @expectedExceptionMessage Parameter type cannot be void
135      */
136     public function testVoidTypeError() {
137         $this->createParamBuilder('test')->setTypeHint('void');
138     }
139
140     /**
141      * @expectedException \LogicException
142      * @expectedExceptionMessage Type must be a string, or an instance of Name or NullableType
143      */
144     public function testInvalidTypeError() {
145         $this->createParamBuilder('test')->setTypeHint(new \stdClass);
146     }
147
148     public function testByRef() {
149         $node = $this->createParamBuilder('test')
150             ->makeByRef()
151             ->getNode()
152         ;
153
154         $this->assertEquals(
155             new Node\Param('test', null, null, true),
156             $node
157         );
158     }
159
160     public function testVariadic() {
161         $node = $this->createParamBuilder('test')
162             ->makeVariadic()
163             ->getNode()
164         ;
165
166         $this->assertEquals(
167             new Node\Param('test', null, null, false, true),
168             $node
169         );
170     }
171 }