File: FunctionReflectionTest.php

package info (click to toggle)
php-zend-code 4.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,648 kB
  • sloc: php: 13,824; xml: 2,327; makefile: 16
file content (295 lines) | stat: -rw-r--r-- 12,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php

namespace LaminasTest\Code\Reflection;

use Laminas\Code\Reflection\DocBlockReflection;
use Laminas\Code\Reflection\Exception\InvalidArgumentException;
use Laminas\Code\Reflection\FunctionReflection;
use Laminas\Code\Reflection\ParameterReflection;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

use function array_shift;
use function trim;
use function uniqid;

#[Group('Laminas_Reflection')]
#[Group('Laminas_Reflection_Function')]
class FunctionReflectionTest extends TestCase
{
    public function testParemeterReturn()
    {
        $function   = new FunctionReflection('array_splice');
        $parameters = $function->getParameters();
        self::assertCount(4, $parameters);
        self::assertInstanceOf(ParameterReflection::class, array_shift($parameters));
    }

    public function testFunctionDocBlockReturn()
    {
        require_once __DIR__ . '/TestAsset/functions.php';
        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function3');
        self::assertInstanceOf(DocBlockReflection::class, $function->getDocBlock());
    }

    public function testGetPrototypeMethod()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function  = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function2');
        $prototype = [
            'namespace' => 'LaminasTest\Code\Reflection\TestAsset',
            'name'      => 'function2',
            'return'    => 'string',
            'arguments' => [
                'one' => [
                    'type'     => 'string',
                    'required' => true,
                    'by_ref'   => false,
                    'default'  => null,
                ],
                'two' => [
                    'type'     => 'string',
                    'required' => false,
                    'by_ref'   => false,
                    'default'  => 'two',
                ],
            ],
        ];
        self::assertEquals($prototype, $function->getPrototype());
        self::assertEquals(
            'string function2(string $one, string $two = \'two\')',
            $function->getPrototype(FunctionReflection::PROTOTYPE_AS_STRING)
        );
    }

    public function testInternalFunctionBodyReturn()
    {
        $function = new FunctionReflection('array_splice');
        $this->expectException(InvalidArgumentException::class);
        $function->getBody();
    }

    public function testFunctionBodyReturn()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function1');
        $body     = $function->getBody();
        self::assertEquals("return 'function1';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function4');
        $body     = $function->getBody();
        self::assertEquals("return 'function4';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function5');
        $body     = $function->getBody();
        self::assertEquals("return 'function5';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function6');
        $body     = $function->getBody();
        self::assertEquals("\$closure = function() { return 'bar'; };\n    return 'function6';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function7');
        $body     = $function->getBody();
        self::assertEquals("return 'function7';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function8');
        $body     = $function->getBody();
        self::assertEquals("return 'function8';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function9');
        $body     = $function->getBody();
        self::assertEquals("return 'function9';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function10');
        $body     = $function->getBody();
        self::assertEquals("\$closure = function() { return 'function10'; }; return \$closure();", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function11');
        $body     = $function->getBody();
        self::assertEquals("return 'function11';", trim($body));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function12');
        $body     = $function->getBody() ?: '';
        self::assertEquals('', trim($body));
    }

    public function testFunctionClosureBodyReturn()
    {
        $function1  = null;
        $function2  = null;
        $function3  = null;
        $function4  = null;
        $list1      = [];
        $list2      = [];
        $list3      = [];
        $function8  = null;
        $function9  = null;
        $function10 = null;
        require __DIR__ . '/TestAsset/closures.php';

        $function = new FunctionReflection($function1);
        $body     = $function->getBody();
        self::assertEquals("return 'function1';", trim($body));

        $function = new FunctionReflection($function2);
        $body     = $function->getBody();
        self::assertEquals("return 'function2';", trim($body));

        $function = new FunctionReflection($function3);
        $body     = $function->getBody();
        self::assertEquals("return 'function3';", trim($body));

        $function = new FunctionReflection($function4);
        $body     = $function->getBody();
        self::assertEquals("\$closure = function() { return 'bar'; };\n    return 'function4';", trim($body));

        $function5 = $list1['closure'];
        $function  = new FunctionReflection($function5);
        $body      = $function->getBody();
        self::assertEquals("return 'function5';", trim($body));

        $function6 = $list2[0];
        $function  = new FunctionReflection($function6);
        $body      = $function->getBody();
        self::assertEquals("return 'function6';", trim($body));

        $function7 = $list3[0];
        $function  = new FunctionReflection($function7);
        $body      = $function->getBody();
        self::assertEquals("return \$c = function() { return 'function7'; }; return \$c();", trim($body));

        $function = new FunctionReflection($function8);
        $body     = $function->getBody();
        self::assertEquals("return 'function 8';", trim($body));

        $function = new FunctionReflection($function9);
        $body     = $function->getBody() ?: '';
        self::assertEquals('', trim($body));

        $function = new FunctionReflection($function10);
        $body     = $function->getBody();
        self::assertEquals("return 'function10';", trim($body));
    }

    public function testInternalFunctionContentsReturn()
    {
        $function = new FunctionReflection('array_splice');

        self::assertEmpty($function->getContents());
    }

    public function testFunctionContentsReturnWithoutDocBlock()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function1');
        $content  = $function->getContents(false);
        self::assertEquals("function function1()\n{\n    return 'function1';\n}", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function4');
        $content  = $function->getContents(false);
        self::assertEquals("function function4(\$arg) {\n    return 'function4';\n}", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function5');
        $content  = $function->getContents(false);
        self::assertEquals("function function5() { return 'function5'; }", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function6');
        $content  = $function->getContents(false);
        self::assertEquals(
            "function function6()\n{\n    \$closure = function() { return 'bar'; };\n    return 'function6';\n}",
            trim($content)
        );

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function7');
        $content  = $function->getContents(false);
        self::assertEquals("function function7() { return 'function7'; }", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function8');
        $content  = $function->getContents(false);
        self::assertEquals("function function8() { return 'function8'; }", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function9');
        $content  = $function->getContents(false);
        self::assertEquals("function function9() { return 'function9'; }", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function10');
        $content  = $function->getContents(false);
        self::assertEquals(
            "function function10() { \$closure = function() { return 'function10'; }; return \$closure(); }",
            trim($content)
        );

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function11');
        $content  = $function->getContents(false);
        self::assertEquals("function function11() { return 'function11'; }", trim($content));

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function12');
        $content  = $function->getContents(false);
        self::assertEquals('function function12() {}', trim($content));
    }

    #[Group('fail')]
    public function testFunctionClosureContentsReturnWithoutDocBlock()
    {
        $function2  = null;
        $function9  = null;
        $function10 = null;
        require __DIR__ . '/TestAsset/closures.php';

        $function = new FunctionReflection($function2);
        $content  = $function->getContents(false);
        self::assertEquals("function() { return 'function2'; }", trim($content));

        $function = new FunctionReflection($function9);
        $content  = $function->getContents(false);
        self::assertEquals('function() {}', trim($content));

        $function = new FunctionReflection($function10);
        $content  = $function->getContents(false);
        self::assertEquals("function() { return 'function10'; }", trim($content));
    }

    public function testFunctionContentsReturnWithDocBlock()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function = new FunctionReflection('LaminasTest\Code\Reflection\TestAsset\function3');
        $content  = $function->getContents();
        self::assertEquals(
            "/**\n * Enter description here...\n *\n * @param string \$one\n * @param int \$two"
            . "\n * @return true\n */\nfunction function3(\$one, \$two = 2)\n{\n    return true;\n}",
            trim($content)
        );
    }

    public function testFunctionClosureContentsReturnWithDocBlock()
    {
        $function9 = null;
        require __DIR__ . '/TestAsset/closures.php';

        $function = new FunctionReflection($function9);
        $content  = $function->getContents();
        self::assertEquals("/**\n * closure doc block\n */\nfunction() {}", trim($content));
    }

    public function testGetContentsReturnsEmptyContentsOnEvaldCode()
    {
        $functionName = uniqid('generatedFunction');

        eval('namespace ' . __NAMESPACE__ . '; function ' . $functionName . '(){}');

        $reflectionFunction = new FunctionReflection(__NAMESPACE__ . '\\' . $functionName);

        self::assertSame('', $reflectionFunction->getContents());
    }

    public function testGetContentsReturnsEmptyContentsOnInternalCode()
    {
        $reflectionFunction = new FunctionReflection('max');

        self::assertSame('', $reflectionFunction->getContents());
    }
}