Completed
Push — master ( 24b063...f0947c )
by Kyle
21s queued 18s
created

PHPParserVersion74Test::testSingleTypedProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of PDepend.
4
 *
5
 * Copyright (c) 2008-2017 Manuel Pichler <[email protected]>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Manuel Pichler nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @copyright 2008-2017 Manuel Pichler. All rights reserved.
38
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
39
 */
40
41
namespace PDepend\Source\Language\PHP;
42
43
use OutOfBoundsException;
44
use PDepend\AbstractTest;
45
use PDepend\Source\AST\ASTAssignmentExpression;
46
use PDepend\Source\AST\ASTClass;
47
use PDepend\Source\AST\ASTClosure;
48
use PDepend\Source\AST\ASTExpression;
49
use PDepend\Source\AST\ASTFieldDeclaration;
50
use PDepend\Source\AST\ASTFormalParameter;
51
use PDepend\Source\AST\ASTFormalParameters;
52
use PDepend\Source\AST\ASTNode;
53
use PDepend\Source\AST\ASTReturnStatement;
54
use PDepend\Source\AST\ASTVariableDeclarator;
55
56
/**
57
 * Test case for the {@link \PDepend\Source\Language\PHP\PHPParserVersion74} class.
58
 *
59
 * @copyright 2008-2017 Manuel Pichler. All rights reserved.
60
 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
61
 * @covers \PDepend\Source\Language\PHP\PHPParserVersion74
62
 * @group unittest
63
 */
64
class PHPParserVersion74Test extends AbstractTest
65
{
66
    /**
67
     * @return void
68
     */
69
    public function testTypedProperties()
70
    {
71
        /** @var ASTClass $class */
72
        $class = $this->getFirstClassForTestCase();
73
        $children = $class->getChildren();
74
        /** @var ASTFieldDeclaration $mixedDeclaration */
75
        $mixedDeclaration = array_shift($children);
76
77
        $this->assertFalse($mixedDeclaration->hasType());
78
79
        $message = null;
80
81
        try {
82
            $mixedDeclaration->getType();
83
        } catch (OutOfBoundsException $exception) {
84
            $message = $exception->getMessage();
85
        }
86
87
        $this->assertSame('The parameter does not has a type specification.', $message);
88
89
        /** @var array[] $declarations */
90
        $declarations = array_map(function (ASTFieldDeclaration $child) {
91
            $childChildren = $child->getChildren();
92
93
            return array(
94
                $child->hasType() ? $child->getType() : null,
95
                $childChildren[1],
96
            );
97
        }, $children);
98
99
        foreach (array(
100
            array('int', '$id'),
101
            array('float', '$money'),
102
            array('bool', '$active'),
103
            array('string', '$name'),
104
            array('array', '$list', 'PDepend\\Source\\AST\\ASTTypeArray'),
105
            array('self', '$parent', 'PDepend\\Source\\AST\\ASTSelfReference'),
106
            array('callable', '$event', 'PDepend\\Source\\AST\\ASTTypeCallable'),
107
            array('\Closure', '$fqn', 'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'),
108
            array('iterable', '$actions', 'PDepend\\Source\\AST\\ASTTypeIterable'),
109
            array('object', '$bag', 'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'),
110
            array('Role', '$role', 'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'),
111
            array('?int', '$idN'),
112
            array('?float', '$moneyN'),
113
            array('?bool', '$activeN'),
114
            array('?string', '$nameN'),
115
            array('?array', '$listN', 'PDepend\\Source\\AST\\ASTTypeArray'),
116
            array('?self', '$parentN', 'PDepend\\Source\\AST\\ASTSelfReference'),
117
            array('?callable', '$eventN', 'PDepend\\Source\\AST\\ASTTypeCallable'),
118
            array('?\Closure', '$fqnN', 'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'),
119
            array('?iterable', '$actionsN', 'PDepend\\Source\\AST\\ASTTypeIterable'),
120
            array('?object', '$bagN', 'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'),
121
            array('?Role', '$roleN', 'PDepend\\Source\\AST\\ASTClassOrInterfaceReference'),
122
        ) as $index => $expected) {
123
            list($expectedType, $expectedVariable) = $expected;
124
            $expectedTypeClass = isset($expected[2]) ? $expected[2] : 'PDepend\\Source\\AST\\ASTScalarType';
125
            list($type, $variable) = $declarations[$index];
126
127
            $this->assertInstanceOf(
128
                $expectedTypeClass,
129
                $type,
130
                "Wrong type for $expectedType $expectedVariable"
131
            );
132
            $this->assertSame(ltrim($expectedType, '?'), $type->getImage());
133
            $this->assertInstanceOf(
134
                'PDepend\\Source\\AST\\ASTVariableDeclarator',
135
                $variable,
136
                "Wrong variable for $expectedType $expectedVariable"
137
            );
138
            $this->assertSame($expectedVariable, $variable->getImage());
139
        }
140
    }
141
142
    public function testSingleTypedProperty()
143
    {
144
        /** @var ASTClass $class */
145
        $class = $this->getFirstClassForTestCase();
146
        /** @var ASTFieldDeclaration $field */
147
        $field = $class->getChild(0);
148
        $this->assertTrue($field->hasType());
149
        $this->assertSame('int', $field->getType()->getImage());
150
        $this->assertTrue($field->isPrivate());
151
        $this->assertFalse($field->isProtected());
152
        $this->assertFalse($field->isPublic());
153
    }
154
155
    public function testTypedPropertiesSyntaxError()
156
    {
157
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0; use expectException() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
158
            'PDepend\\Source\\Parser\\UnexpectedTokenException',
159
            'Unexpected token: string, line: 4, col: 16, file:'
160
        );
161
162
        $this->parseCodeResourceForTest();
163
    }
164
165
    public function testArrowFunctions()
166
    {
167
        if (version_compare(phpversion(), '7.4.0', '<')) {
168
            $this->markTestSkipped('This test requires PHP >= 7.4');
169
        }
170
171
        /** @var ASTClosure $closure */
172
        $closure = $this->getFirstNodeOfTypeInFunction(
173
            $this->getCallingTestMethod(),
174
            'PDepend\\Source\\AST\\ASTFunctionPostfix'
175
        )->getChild(1)->getChild(0);
176
177
        $this->assertInstanceOf('PDepend\\Source\\AST\\ASTClosure', $closure);
178
        /** @var ASTFormalParameters $parameters */
179
        $parameters = $closure->getChild(0);
180
        $this->assertInstanceOf('PDepend\\Source\\AST\\ASTFormalParameters', $parameters);
181
        $this->assertCount(1, $parameters->getChildren());
182
        /** @var ASTFormalParameter $parameter */
183
        $parameter = $parameters->getChild(0);
184
        $this->assertInstanceOf('PDepend\\Source\\AST\\ASTFormalParameter', $parameter);
185
        /** @var ASTVariableDeclarator $parameter */
186
        $variableDeclarator = $parameter->getChild(0);
187
        $this->assertInstanceOf('PDepend\\Source\\AST\\ASTVariableDeclarator', $variableDeclarator);
188
        $this->assertSame('$number', $variableDeclarator->getImage());
189
        /** @var ASTReturnStatement $parameters */
190
        $return = $closure->getChild(1);
191
        $this->assertInstanceOf('PDepend\\Source\\AST\\ASTReturnStatement', $return);
192
        $this->assertSame('=>', $return->getImage());
193
        $this->assertCount(1, $return->getChildren());
194
        /** @var ASTExpression $expression */
195
        $expression = $return->getChild(0);
196
        $this->assertInstanceOf('PDepend\\Source\\AST\\ASTExpression', $expression);
197
        $this->assertSame(array(
198
            'PDepend\\Source\\AST\\ASTVariable',
199
            'PDepend\\Source\\AST\\ASTExpression',
200
            'PDepend\\Source\\AST\\ASTLiteral',
201
        ), array_map('get_class', $expression->getChildren()));
202
        $this->assertSame(array(
203
            '$number',
204
            '*',
205
            '2',
206
        ), array_map(function (ASTNode $node) {
207
            return $node->getImage();
208
        }, $expression->getChildren()));
209
    }
210
211
    public function testTypeCovarianceAndArgumentTypeContravariance()
212
    {
213
        $this->assertNotNull($this->parseCodeResourceForTest());
214
    }
215
216
    public function testNullCoalescingAssignmentOperator()
217
    {
218
        if (version_compare(phpversion(), '7.4.0', '<')) {
219
            $this->markTestSkipped('This test requires PHP >= 7.4');
220
        }
221
222
        /** @var ASTAssignmentExpression $assignment */
223
        $assignment = $this->getFirstNodeOfTypeInFunction(
224
            $this->getCallingTestMethod(),
225
            'PDepend\\Source\\AST\\ASTAssignmentExpression'
226
        );
227
228
        $this->assertSame('??=', $assignment->getImage());
229
    }
230
231
    public function testUnpackingInsideArrays()
232
    {
233
        if (version_compare(phpversion(), '7.4.0', '<')) {
234
            $this->markTestSkipped('This test requires PHP >= 7.4');
235
        }
236
237
        $expression = $this->getFirstNodeOfTypeInFunction(
238
            $this->getCallingTestMethod(),
239
            'PDepend\\Source\\AST\\ASTArray'
240
        );
241
        $this->assertSame(array(
242
            'PDepend\\Source\\AST\\ASTArrayElement',
243
            'PDepend\\Source\\AST\\ASTArrayElement',
244
            'PDepend\\Source\\AST\\ASTArrayElement',
245
            'PDepend\\Source\\AST\\ASTArrayElement',
246
            'PDepend\\Source\\AST\\ASTArrayElement',
247
        ), array_map('get_class', $expression->getChildren()));
248
        /** @var ASTNode[] $elements */
249
        $elements = array_map(function ($node) {
250
            return $node->getChild(0);
251
        }, $expression->getChildren());
252
        $this->assertSame(array(
253
            'PDepend\Source\AST\ASTLiteral',
254
            'PDepend\Source\AST\ASTLiteral',
255
            'PDepend\Source\AST\ASTExpression',
256
            'PDepend\Source\AST\ASTLiteral',
257
            'PDepend\Source\AST\ASTLiteral',
258
        ), array_map('get_class', $elements));
259
        /** @var ASTExpression $expression */
260
        $expression = $elements[2];
261
        $this->assertSame(array(
262
            '...',
263
            '$numbers',
264
        ), array_map(function (ASTNode $node) {
265
            return $node->getImage();
266
        }, $expression->getChildren()));
267
    }
268
269
    public function testNumericLiteralSeparator()
270
    {
271
        if (version_compare(phpversion(), '7.4.0', '<')) {
272
            $this->markTestSkipped('This test requires PHP >= 7.4');
273
        }
274
275
        $expression = $this->getFirstNodeOfTypeInFunction(
276
            $this->getCallingTestMethod(),
277
            'PDepend\\Source\\AST\\ASTExpression'
278
        );
279
        $this->assertSame(array(
280
            'PDepend\\Source\\AST\\ASTLiteral',
281
            'PDepend\\Source\\AST\\ASTExpression',
282
            'PDepend\\Source\\AST\\ASTLiteral',
283
            'PDepend\\Source\\AST\\ASTExpression',
284
            'PDepend\\Source\\AST\\ASTLiteral',
285
            'PDepend\\Source\\AST\\ASTExpression',
286
            'PDepend\\Source\\AST\\ASTLiteral',
287
        ), array_map('get_class', $expression->getChildren()));
288
289
        $this->assertSame('6.674_083e-11', $expression->getChild(0)->getImage());
290
        $this->assertSame('299_792_458', $expression->getChild(2)->getImage());
291
        $this->assertSame('0xCAFE_F00D', $expression->getChild(4)->getImage());
292
        $this->assertSame('0b0101_1111', $expression->getChild(6)->getImage());
293
    }
294
}
295