File: Php82Test.php

package info (click to toggle)
php-symfony-polyfill 1.33.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,520 kB
  • sloc: php: 127,232; makefile: 69
file content (413 lines) | stat: -rw-r--r-- 18,644 bytes parent folder | download | duplicates (2)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Polyfill\Tests\Php82;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class Php82Test extends TestCase
{
    /**
     * @dataProvider provideConnectionStringValuesFromUpstream
     * @dataProvider provideMoreConnectionStringValues
     *
     * @requires extension odbc
     */
    #[DataProvider('provideConnectionStringValuesFromUpstream')]
    #[DataProvider('provideMoreConnectionStringValues')]
    public function testConnectionStringIsQuoted(string $value, bool $isQuoted, bool $shouldQuote, string $quoted)
    {
        self::assertSame($isQuoted, odbc_connection_string_is_quoted($value));
    }

    /**
     * @dataProvider provideConnectionStringValuesFromUpstream
     * @dataProvider provideMoreConnectionStringValues
     *
     * @requires extension odbc
     */
    #[DataProvider('provideConnectionStringValuesFromUpstream')]
    #[DataProvider('provideMoreConnectionStringValues')]
    public function testConnectionStringShouldQuote(string $value, bool $isQuoted, bool $shouldQuote, string $quoted)
    {
        self::assertSame($shouldQuote, odbc_connection_string_should_quote($value));
    }

    /**
     * @dataProvider provideConnectionStringValuesFromUpstream
     * @dataProvider provideMoreConnectionStringValues
     *
     * @requires extension odbc
     */
    #[DataProvider('provideConnectionStringValuesFromUpstream')]
    #[DataProvider('provideMoreConnectionStringValues')]
    public function testConnectionStringQuote(string $value, bool $isQuoted, bool $shouldQuote, string $quoted)
    {
        self::assertSame($quoted, odbc_connection_string_quote($value));
    }

    /**
     * Test cases ported from upstream.
     *
     * @see https://github.com/php/php-src/blob/838f6bffff6363a204a2597cbfbaad1d7ee3f2b6/ext/odbc/tests/odbc_utils.phpt
     *
     * @return \Generator<string, array{string, bool, bool, string}>
     */
    public static function provideConnectionStringValuesFromUpstream(): \Generator
    {
        // 1. No, it's not quoted.
        // 2. Yes, it should be quoted because of the special character in the middle.
        yield 'with_end_curly1' => ['foo}bar', false, true, '{foo}}bar}'];

        // 1. No, the unescaped special character in the middle breaks what would be quoted.
        // 2. Yes, it should be quoted because of the special character in the middle.
        //    Note that should_quote doesn't care about if the string is already quoted.
        //    That's why you should check if it is quoted first.
        yield 'with_end_curly2' => ['{foo}bar}', false, true, '{{foo}}bar}}}'];

        // 1. Yes, the special characters are escaped, so it's quoted.
        // 2. See $with_end_curly2; should_quote doesn't care about if the string is already quoted.
        yield 'with_end_curly3' => ['{foo}}bar}', true, true, '{{foo}}}}bar}}}'];

        // 1. No, it's not quoted.
        // 2. It doesn't need to be quoted because of no s
        yield 'with_no_end_curly1' => ['foobar', false, false, '{foobar}'];

        // 1. Yes, it is quoted and any characters are properly escaped.
        // 2. See $with_end_curly2.
        yield 'with_no_end_curly2' => ['{foobar}', true, true, '{{foobar}}}'];
    }

    /**
     * @return \Generator<string, array{string, bool, bool, string}>
     */
    public static function provideMoreConnectionStringValues(): \Generator
    {
        yield 'double curly at the end' => ['foo}}', false, true, '{foo}}}}}'];
    }

    public function testIniParseQuantity()
    {
        $this->assertSame(0, ini_parse_quantity(''));
        $this->assertSame(0, ini_parse_quantity('0'));
        $this->assertSame(0, ini_parse_quantity(' 0'));
        $this->assertSame(0, ini_parse_quantity('0 '));
        $this->assertSame(0, ini_parse_quantity(' +0 '));
        $this->assertSame(0, ini_parse_quantity(' -0 '));

        $this->assertSame(0, ini_parse_quantity('00'));
        $this->assertSame(0, ini_parse_quantity(' 00'));
        $this->assertSame(0, ini_parse_quantity('00 '));
        $this->assertSame(0, ini_parse_quantity(' +00 '));
        $this->assertSame(0, ini_parse_quantity(' -00 '));

        $this->assertSame(1, ini_parse_quantity('1'));
        $this->assertSame(-1, ini_parse_quantity('-1'));
        $this->assertSame(1, ini_parse_quantity('+1'));

        $this->assertSame(0, ini_parse_quantity('0b0'));
        $this->assertSame(2, ini_parse_quantity('0b10'));
        $this->assertSame(-2, ini_parse_quantity('-0b10'));
        $this->assertSame(2, ini_parse_quantity('+0b10'));

        $this->assertSame(0, ini_parse_quantity('0B0'));
        $this->assertSame(2, ini_parse_quantity('0B10'));
        $this->assertSame(-2, ini_parse_quantity('-0B10'));
        $this->assertSame(2, ini_parse_quantity('+0B10'));

        $this->assertSame(8, ini_parse_quantity('0o10'));
        $this->assertSame(-8, ini_parse_quantity('-0o10'));
        $this->assertSame(8, ini_parse_quantity('+0o10'));

        $this->assertSame(8, ini_parse_quantity('0O10'));
        $this->assertSame(-8, ini_parse_quantity('-0O10'));
        $this->assertSame(8, ini_parse_quantity('+0O10'));

        $this->assertSame(16, ini_parse_quantity('0x10'));
        $this->assertSame(-16, ini_parse_quantity('-0x10'));
        $this->assertSame(16, ini_parse_quantity('+0x10'));

        $this->assertSame(16, ini_parse_quantity('0X10'));
        $this->assertSame(-16, ini_parse_quantity('-0X10'));
        $this->assertSame(16, ini_parse_quantity('+0X10'));

        $this->assertSame(0, ini_parse_quantity('0k'));
        $this->assertSame(0, ini_parse_quantity(' 0K'));
        $this->assertSame(0, ini_parse_quantity('0k '));

        $this->assertSame(1024, ini_parse_quantity('1k'));
        $this->assertSame(1024, ini_parse_quantity('1 K'));
        $this->assertSame(1024, ini_parse_quantity('+1k'));
        $this->assertSame(-1024, ini_parse_quantity('-1K'));

        $this->assertSame(2048, ini_parse_quantity('0b10k'));
        $this->assertSame(2048, ini_parse_quantity('0b10 K'));
        $this->assertSame(2048, ini_parse_quantity('+0b10k'));
        $this->assertSame(-2048, ini_parse_quantity('-0b10K'));

        $this->assertSame(2048, ini_parse_quantity('0B10k'));
        $this->assertSame(2048, ini_parse_quantity('0B10 K'));
        $this->assertSame(2048, ini_parse_quantity('+0B10k'));
        $this->assertSame(-2048, ini_parse_quantity('-0B10K'));

        $this->assertSame(8192, ini_parse_quantity('0o10k'));
        $this->assertSame(8192, ini_parse_quantity('0o10 K'));
        $this->assertSame(8192, ini_parse_quantity('+0o10k'));
        $this->assertSame(-8192, ini_parse_quantity('-0o10K'));

        $this->assertSame(8192, ini_parse_quantity('0O10k'));
        $this->assertSame(8192, ini_parse_quantity('0O10 K'));
        $this->assertSame(8192, ini_parse_quantity('+0O10k'));
        $this->assertSame(-8192, ini_parse_quantity('-0O10K'));

        $this->assertSame(16384, ini_parse_quantity('0x10k'));
        $this->assertSame(16384, ini_parse_quantity('0x10 K'));
        $this->assertSame(16384, ini_parse_quantity('+0x10k'));
        $this->assertSame(-16384, ini_parse_quantity('-0x10K'));

        $this->assertSame(16384, ini_parse_quantity('0X10k'));
        $this->assertSame(16384, ini_parse_quantity('0X10 K'));
        $this->assertSame(16384, ini_parse_quantity('+0X10k'));
        $this->assertSame(-16384, ini_parse_quantity('-0X10K'));

        $this->assertSame(1048576, ini_parse_quantity('1m'));
        $this->assertSame(1048576, ini_parse_quantity('1 M'));
        $this->assertSame(1048576, ini_parse_quantity('+1m'));
        $this->assertSame(-1048576, ini_parse_quantity('-1M'));

        $this->assertSame(2097152, ini_parse_quantity('0b10m'));
        $this->assertSame(2097152, ini_parse_quantity('0b10 M'));
        $this->assertSame(2097152, ini_parse_quantity('+0b10m'));
        $this->assertSame(-2097152, ini_parse_quantity('-0b10M'));

        $this->assertSame(2097152, ini_parse_quantity('0B10m'));
        $this->assertSame(2097152, ini_parse_quantity('0B10 M'));
        $this->assertSame(2097152, ini_parse_quantity('+0B10m'));
        $this->assertSame(-2097152, ini_parse_quantity('-0B10M'));

        $this->assertSame(8388608, ini_parse_quantity('0o10m'));
        $this->assertSame(8388608, ini_parse_quantity('0o10 M'));
        $this->assertSame(8388608, ini_parse_quantity('+0o10m'));
        $this->assertSame(-8388608, ini_parse_quantity('-0o10M'));

        $this->assertSame(8388608, ini_parse_quantity('0O10m'));
        $this->assertSame(8388608, ini_parse_quantity('0O10 M'));
        $this->assertSame(8388608, ini_parse_quantity('+0O10m'));
        $this->assertSame(-8388608, ini_parse_quantity('-0O10M'));

        $this->assertSame(16777216, ini_parse_quantity('0x10m'));
        $this->assertSame(16777216, ini_parse_quantity('0x10 M'));
        $this->assertSame(16777216, ini_parse_quantity('+0x10m'));
        $this->assertSame(-16777216, ini_parse_quantity('-0x10M'));

        $this->assertSame(16777216, ini_parse_quantity('0X10m'));
        $this->assertSame(16777216, ini_parse_quantity('0X10 M'));
        $this->assertSame(16777216, ini_parse_quantity('+0X10m'));
        $this->assertSame(-16777216, ini_parse_quantity('-0X10M'));

        $this->assertSame(1073741824, ini_parse_quantity('1g'));
        $this->assertSame(1073741824, ini_parse_quantity('1 G'));
        $this->assertSame(1073741824, ini_parse_quantity('+1g'));
        $this->assertSame(-1073741824, ini_parse_quantity('-1G'));

        $this->assertSame(2147483648, ini_parse_quantity('0b10g'));
        $this->assertSame(2147483648, ini_parse_quantity('0b10 G'));
        $this->assertSame(2147483648, ini_parse_quantity('+0b10g'));
        $this->assertSame(-2147483648, ini_parse_quantity('-0b10G'));

        $this->assertSame(2147483648, ini_parse_quantity('0B10g'));
        $this->assertSame(2147483648, ini_parse_quantity('0B10 G'));
        $this->assertSame(2147483648, ini_parse_quantity('+0B10g'));
        $this->assertSame(-2147483648, ini_parse_quantity('-0B10G'));

        $this->assertSame(8589934592, ini_parse_quantity('0o10g'));
        $this->assertSame(8589934592, ini_parse_quantity('0o10 G'));
        $this->assertSame(8589934592, ini_parse_quantity('+0o10g'));
        $this->assertSame(-8589934592, ini_parse_quantity('-0o10G'));

        $this->assertSame(8589934592, ini_parse_quantity('0O10g'));
        $this->assertSame(8589934592, ini_parse_quantity('0O10 G'));
        $this->assertSame(8589934592, ini_parse_quantity('+0O10g'));
        $this->assertSame(-8589934592, ini_parse_quantity('-0O10G'));

        $this->assertSame(17179869184, ini_parse_quantity('0x10g'));
        $this->assertSame(17179869184, ini_parse_quantity('0x10 G'));
        $this->assertSame(17179869184, ini_parse_quantity('+0x10g'));
        $this->assertSame(-17179869184, ini_parse_quantity('-0x10G'));

        $this->assertSame(17179869184, ini_parse_quantity('0X10g'));
        $this->assertSame(17179869184, ini_parse_quantity('0X10 G'));
        $this->assertSame(17179869184, ini_parse_quantity('+0X10g'));
        $this->assertSame(-17179869184, ini_parse_quantity('-0X10G'));
    }

    public function testIniParseQuantityZeroWithMultiplier()
    {
        // Note that "1 K" is valid
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity(' 0 K '));
        $this->assertSame('Invalid prefix "0 ", interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityNoValidLeadingDigits()
    {
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity(' foo '));
        $this->assertSame('Invalid quantity " foo ": no valid leading digits, interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityUnknownMultiplier()
    {
        error_clear_last();
        $this->assertSame(2, @ini_parse_quantity(' 0b102 '));
        $this->assertSame('Invalid quantity " 0b102 ": unknown multiplier "2", interpreting as " 0b10" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityNoDigitsAfterBasePrefix()
    {
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity(' 0x '));
        $this->assertSame('Invalid quantity " 0x ": no digits after base prefix, interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityInvalidPrefix()
    {
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity('0q12'));
        $this->assertSame('Invalid prefix "0q", interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityInvalidQuantity()
    {
        error_clear_last();
        $this->assertSame(10240, @ini_parse_quantity(' 10 kk '));
        $this->assertSame('Invalid quantity " 10 kk ", interpreting as " 10 k" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityNoLeadingDigits()
    {
        // There are two paths to generate this error
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity(' K '));
        $this->assertSame('Invalid quantity " K ": no valid leading digits, interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityOutOfRange()
    {
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity(' 0x-4K '));
        $this->assertSame('Invalid quantity " 0x-4K ": no digits after base prefix, interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityOverflowTooManyDigits()
    {
        error_clear_last();
        $this->assertSame(-1, @ini_parse_quantity(' 99999999999999999999 '));
        $this->assertSame('Invalid quantity " 99999999999999999999 ": value is out of range, using overflow result for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantityOverflowWithMultiplier()
    {
        error_clear_last();
        $this->assertSame(-7709325833709551616, @ini_parse_quantity(' 10000000000G '));
        $this->assertSame('Invalid quantity " 10000000000G ": value is out of range, using overflow result for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantitySignAfterPrefixButNoDigits()
    {
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity(' 0b- '));
        $this->assertSame('Invalid quantity " 0b- ": no digits after base prefix, interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    public function testIniParseQuantitySpecialCharactersAreEscaped()
    {
        error_clear_last();
        $this->assertSame(0, @ini_parse_quantity("w-\n-\r-\t-\v-\f-\\-\x1B-\xCC-"));
        $this->assertSame('Invalid quantity "w-\\n-\\r-\\t-\\v-\\f-\\\\-\\e-\\xCC-": no valid leading digits, interpreting as "0" for backwards compatibility', error_get_last()['message']);
        $this->assertContains(error_get_last()['type'], [\E_WARNING, \E_USER_WARNING]);
    }

    /**
     * This is a brute-force test that should only be used during local development.
     * It takes approximately 4 hours to run.  It is good at picking up edge cases.
     */
    public function testIniParseQuantityUsingBruteForce()
    {
        if (\PHP_VERSION_ID < 80200) {
            $this->markTestSkipped('This test requires the PHP function as a reference.');

            return;
        }

        // Comment these lines to run the tests.
        $this->markTestSkipped('This test is slow and should only be used for local development.');

        return;

        $fn = function (string $test): void {
            error_clear_last();
            $x = @ini_parse_quantity($test);
            $err_x = error_get_last()['message'] ?? '';

            error_clear_last();
            $y = @ini_parse_quantity($test);
            $err_y = error_get_last()['message'] ?? '';

            $this->assertSame($x, $y, 'Testing "'.$test.'"');
            $this->assertSame($err_x, $err_y, 'Testing "'.$test.'"');
        };

        $chars = [' ', '-', '+', '0', '1', '7', '9', 'a', 'b', 'o', 'f', 'g', 'k', 'm', 'x', 'z', '\\'];

        $fn('');

        foreach ($chars as $char1) {
            $fn($char1);

            foreach ($chars as $char2) {
                $fn($char1.$char2);

                foreach ($chars as $char3) {
                    $fn($char1.$char2.$char3);

                    foreach ($chars as $char4) {
                        $fn($char1.$char2.$char3.$char4);

                        foreach ($chars as $char5) {
                            $fn($char1.$char2.$char3.$char4.$char5);

                            foreach ($chars as $char6) {
                                $fn($char1.$char2.$char3.$char4.$char5.$char6);

                                foreach ($chars as $char7) {
                                    $fn($char1.$char2.$char3.$char4.$char5.$char6.$char7);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}