File: ExportClosureTest.php

package info (click to toggle)
php-brick-varexporter 0.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: php: 1,913; xml: 31; makefile: 18
file content (316 lines) | stat: -rw-r--r-- 9,161 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php

declare(strict_types=1);

namespace Brick\VarExporter\Tests;

use Brick\VarExporter\Tests\Classes\Enum;
use Brick\VarExporter\Tests\Classes\NoProperties;
use Brick\VarExporter\Tests\Classes\PublicPropertiesOnly;
use Brick\VarExporter\Tests\Classes\SetState;
use Brick\VarExporter\VarExporter;
use PHPUnit\Framework\Attributes\RequiresPhpunit;

/**
 * The function & the const below do not exist, but their namespace should be taken into account by the exporter.
 */
use function Brick\VarExporter\Dummy\Functions\imported_function;
use const Brick\VarExporter\Dummy\Constants\IMPORTED_CONSTANT;

/**
 * Tests exporting closures.
 */
class ExportClosureTest extends AbstractTestCase
{
    #[RequiresPhpunit('< 12')]
    public function testExportSimpleClosure(): void
    {
        $var = function() {
            return 'Hello, world!';
        };

        $expected = <<<'PHP'
            function () {
                return 'Hello, world!';
            }
            PHP;

        $this->assertExportEquals($expected, $var);
    }

    #[RequiresPhpunit('< 12')]
    public function testExportNestedComplexClosure(): void
    {
        $var = [
            (object) [
                'callback' => function() {
                    return function (PublicPropertiesOnly $a, int $b, string & $c, string ...$d) : ?string {
                        $a->foo += $b;
                        $c = $a->bar;
                        $a->bar = implode('', $d);

                        $this->someProp = [
                            $a->foo,
                            $a->bar,
                            $c
                        ];

                        return $this->someProp['c'];
                    };
                }
            ]
        ];

        $expected = <<<'PHP'
            [
                (object) [
                    'callback' => function () {
                        return function (\Brick\VarExporter\Tests\Classes\PublicPropertiesOnly $a, int $b, string &$c, string ...$d): ?string {
                            $a->foo += $b;
                            $c = $a->bar;
                            $a->bar = implode('', $d);
                            $this->someProp = [$a->foo, $a->bar, $c];
                            return $this->someProp['c'];
                        };
                    }
                ]
            ]
            PHP;

        $this->assertExportEquals($expected, $var);
    }

    #[RequiresPhpunit('< 12')]
    public function testExportNamespacedCode(): void
    {
        $var = function(SetState $a) : array {
            return [
                'callback' => function(SetState $a) : NoProperties {
                    strlen(PHP_VERSION);
                    imported_function(IMPORTED_CONSTANT);
                    \Brick\VarExporter\Dummy\Functions\explicitly_namespaced_function(\Brick\VarExporter\Dummy\Constants\EXPLICITLY_NAMESPACED_CONSTANT);

                    return new NoProperties();
                }
            ];
        };

        $expected = <<<'PHP'
            function (\Brick\VarExporter\Tests\Classes\SetState $a): array {
                return ['callback' => function (\Brick\VarExporter\Tests\Classes\SetState $a): \Brick\VarExporter\Tests\Classes\NoProperties {
                    strlen(PHP_VERSION);
                    \Brick\VarExporter\Dummy\Functions\imported_function(\Brick\VarExporter\Dummy\Constants\IMPORTED_CONSTANT);
                    \Brick\VarExporter\Dummy\Functions\explicitly_namespaced_function(\Brick\VarExporter\Dummy\Constants\EXPLICITLY_NAMESPACED_CONSTANT);
                    return new \Brick\VarExporter\Tests\Classes\NoProperties();
                }];
            }
            PHP;

        $this->assertExportEquals($expected, $var);
    }

    #[RequiresPhpunit('< 12')]
    public function testExportClosureWithStringsContainingLikeBreaks(): void
    {
        $var = function() {
            $a = 'Hello,
World!';

            $b = <<<TXT
                Hello,
                world!
                TXT;

            $c = <<<'TXT'
                Hello,
                world!
                TXT;

            return $a . $b . $c;
        };

        $expected = <<<'PHP'
            return function () {
                $a = 'Hello,
            World!';
                $b = <<<TXT
                Hello,
                world!
                TXT;
                $c = <<<'TXT'
                Hello,
                world!
                TXT;
                return $a . $b . $c;
            };

            PHP;

        $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN);
    }

    public function testExportClosureWithUse(): void
    {
        $foo = 'bar';

        $var = function() use ($foo) {
            return $foo;
        };

        $this->assertExportThrows(
            "The closure has bound variables through 'use', this is not supported by default. " .
                "Use the CLOSURE_SNAPSHOT_USE option to export them.",
            $var
        );
    }

    #[RequiresPhpunit('< 12')]
    public function testExportClosureWithUseAsVars(): void
    {
        $foo = 'b' . 'a' . 'r';

        $var = function() use ($foo) {
            return $foo;
        };

        $expected = <<<'PHP'
            return function () {
                $foo = 'bar';
                return $foo;
            };

            PHP;

        $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES);
    }

    #[RequiresPhpunit('< 12')]
    public function testExportClosureWithUseClosure(): void
    {
        $foo = 'b' . 'a' . 'r';

        $sub = function () use ($foo) {
            return $foo;
        };

        $var = function() use ($sub) {
            return $sub();
        };

        $expected = <<<'PHP'
            return function () {
                $sub = function () {
                    $foo = 'bar';
                    return $foo;
                };
                return $sub();
            };

            PHP;

        $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES);
    }

    #[RequiresPhpunit('< 12')]
    public function testExportArrowFunction(): void
    {
        $var = [fn ($planet) => 'hello ' . $planet]; // Wrapping in array for valid syntax PHP <7.4

        $expected = <<<'PHP'
            return [
                function ($planet) {
                    return 'hello ' . $planet;
                }
            ];

            PHP;

        $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN);
    }

    public function testExportArrowFunctionWithContext(): void
    {
        $greet = 'hello';

        $var = [fn ($planet) => $greet . ' ' . $planet];

        $this->assertExportThrows(
            "The arrow function uses variables in the parent scope, this is not supported by default. " .
            "Use the CLOSURE_SNAPSHOT_USE option to export them.",
            $var
        );
    }

    #[RequiresPhpunit('< 12')]
    public function testExportArrowFunctionWithContextVarAsVar(): void
    {
        $greet = 'hello';

        $var = [fn ($planet) => $greet . ' ' . $planet];

        $expected = <<<'PHP'
            return [
                function ($planet) {
                    $greet = 'hello';
                    return $greet . ' ' . $planet;
                }
            ];

            PHP;

        $this->assertExportEquals($expected, $var, VarExporter::ADD_RETURN | VarExporter::CLOSURE_SNAPSHOT_USES);
    }

    #[RequiresPhpunit('< 12')]
    public function testExportEnumMatchFunction(): void
    {
        $var = [
            (object) [
                'callback' => static fn (Enum $enum): string => match ($enum) {
                    Enum::TEST => 'foo',
                }
            ]
        ];

        $expected = <<<'PHP'
            [
                (object) [
                    'callback' => function (\Brick\VarExporter\Tests\Classes\Enum $enum): string {
                        return match ($enum) {
                            \Brick\VarExporter\Tests\Classes\Enum::TEST => 'foo',
                        };
                    }
                ]
            ]
            PHP;

        $this->assertExportEquals($expected, $var);
    }

    public function testExportClosureDefinedInEval(): void
    {
        $var = eval(<<<PHP
            return function() {
                return 'Hello, world!';
            };
            PHP
        );
        $this->assertExportThrows("Closure defined in eval()'d code cannot be exported.", $var);
    }

    public function testExportTwoClosuresOnSameLine(): void
    {
        $var = function() { return function() {}; };

        $this->assertExportThrows("Expected exactly 1 closure in */tests/ExportClosureTest.php on line *, found 2.", $var);
    }

    public function testExportClosureDisabled(): void
    {
        $var = function() {
            return 'Hello, world!';
        };

        $this->assertExportThrows('Class "Closure" is internal, and cannot be exported.', $var, VarExporter::NO_CLOSURES);
    }
}