Skip to content

Commit a5922ee

Browse files
committed
Zend: Add missing ZPP specifier tests
Add tests for remaining Zend parameter parsing (ZPP) specifiers including arrays, strings, paths, callables, objects, enums, variadics, and zvals in both strict and weak type modes. Also add corresponding test helper functions in ext/zend_test. Closes GH-23280.
1 parent c621cbe commit a5922ee

33 files changed

Lines changed: 4527 additions & 6 deletions
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
Test array_ht_or_long ZPP specifier (strict_mode)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
declare(strict_types=1);
9+
10+
$types = require 'types.inc';
11+
$functions = [
12+
'zend_array_ht_or_long',
13+
'zend_array_ht_or_long_or_null',
14+
];
15+
16+
foreach ($functions as $i => $function) {
17+
echo "Using $function:\n\n";
18+
19+
foreach ($types as $name => $type) {
20+
printf(" %-16s ", "$name:");
21+
22+
try {
23+
$result = $function($type);
24+
25+
$result === []
26+
? printf("array(0) {}\n")
27+
: var_dump($result);
28+
29+
} catch (Throwable $e) {
30+
echo $e::class, ': ', $e->getMessage(), "\n";
31+
}
32+
}
33+
34+
if ($i !== array_key_last($functions)) {
35+
echo "\n";
36+
}
37+
}
38+
39+
?>
40+
--EXPECT--
41+
Using zend_array_ht_or_long:
42+
43+
null: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, null given
44+
false: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, false given
45+
true: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, true given
46+
42: int(42)
47+
73.5: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, float given
48+
'string': TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
49+
'15': TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
50+
'56.7': TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
51+
'stdClass': TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
52+
anon class name: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
53+
[]: array(0) {}
54+
new stdClass(): TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, stdClass given
55+
new S(): TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, S given
56+
STDOUT: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, resource given
57+
58+
Using zend_array_ht_or_long_or_null:
59+
60+
null: NULL
61+
false: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, false given
62+
true: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, true given
63+
42: int(42)
64+
73.5: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, float given
65+
'string': TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
66+
'15': TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
67+
'56.7': TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
68+
'stdClass': TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
69+
anon class name: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
70+
[]: array(0) {}
71+
new stdClass(): TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, stdClass given
72+
new S(): TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, S given
73+
STDOUT: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, resource given
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--TEST--
2+
Test array_ht_or_long ZPP specifier (weak_mode)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$types = require 'types.inc';
9+
$functions = [
10+
'zend_array_ht_or_long',
11+
'zend_array_ht_or_long_or_null',
12+
];
13+
14+
foreach ($functions as $i => $function) {
15+
echo "Using $function:\n\n";
16+
17+
foreach ($types as $name => $type) {
18+
printf(" %-16s ", "$name:");
19+
20+
try {
21+
$result = $function($type);
22+
23+
$result === []
24+
? printf("array(0) {}\n")
25+
: var_dump($result);
26+
27+
} catch (Throwable $e) {
28+
echo $e::class, ': ', $e->getMessage(), "\n";
29+
}
30+
}
31+
32+
if ($i !== array_key_last($functions)) {
33+
echo "\n";
34+
}
35+
}
36+
37+
?>
38+
--EXPECTF--
39+
Using zend_array_ht_or_long:
40+
41+
null:
42+
Deprecated: zend_array_ht_or_long(): Passing null to parameter #1 ($param) of type array|int is deprecated in %s on line %d
43+
int(0)
44+
false: int(0)
45+
true: int(1)
46+
42: int(42)
47+
73.5:
48+
Deprecated: Implicit conversion from float 73.5 to int loses precision in %s on line %d
49+
int(73)
50+
'string': TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
51+
'15': int(15)
52+
'56.7':
53+
Deprecated: Implicit conversion from float-string "56.7" to int loses precision in %s on line %d
54+
int(56)
55+
'stdClass': TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
56+
anon class name: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, string given
57+
[]: array(0) {}
58+
new stdClass(): TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, stdClass given
59+
new S(): TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, S given
60+
STDOUT: TypeError: zend_array_ht_or_long(): Argument #1 ($param) must be of type array|int, resource given
61+
62+
Using zend_array_ht_or_long_or_null:
63+
64+
null: NULL
65+
false: int(0)
66+
true: int(1)
67+
42: int(42)
68+
73.5:
69+
Deprecated: Implicit conversion from float 73.5 to int loses precision in %s on line %d
70+
int(73)
71+
'string': TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
72+
'15': int(15)
73+
'56.7':
74+
Deprecated: Implicit conversion from float-string "56.7" to int loses precision in %s on line %d
75+
int(56)
76+
'stdClass': TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
77+
anon class name: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, string given
78+
[]: array(0) {}
79+
new stdClass(): TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, stdClass given
80+
new S(): TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, S given
81+
STDOUT: TypeError: zend_array_ht_or_long_or_null(): Argument #1 ($param) must be of type array|int|null, resource given
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
Test array_ht_or_str ZPP specifier (strict_mode)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
declare(strict_types=1);
9+
10+
$types = require 'types.inc';
11+
$functions = [
12+
'zend_array_ht_or_str',
13+
'zend_array_ht_or_str_or_null',
14+
];
15+
16+
foreach ($functions as $i => $function) {
17+
echo "Using $function:\n\n";
18+
19+
foreach ($types as $name => $type) {
20+
printf(" %-16s ", "$name:");
21+
22+
try {
23+
$result = $function($type);
24+
25+
$result === []
26+
? printf("array(0) {}\n")
27+
: var_dump($result);
28+
29+
} catch (Throwable $e) {
30+
echo $e::class, ': ', $e->getMessage(), "\n";
31+
}
32+
}
33+
34+
if ($i !== array_key_last($functions)) {
35+
echo "\n";
36+
}
37+
}
38+
39+
?>
40+
--EXPECTF--
41+
Using zend_array_ht_or_str:
42+
43+
null: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, null given
44+
false: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, false given
45+
true: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, true given
46+
42: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, int given
47+
73.5: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, float given
48+
'string': string(6) "string"
49+
'15': string(2) "15"
50+
'56.7': string(4) "56.7"
51+
'stdClass': string(8) "stdClass"
52+
anon class name: string(%d) "class@anonymous%s"
53+
[]: array(0) {}
54+
new stdClass(): TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, stdClass given
55+
new S(): TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, S given
56+
STDOUT: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, resource given
57+
58+
Using zend_array_ht_or_str_or_null:
59+
60+
null: NULL
61+
false: TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, false given
62+
true: TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, true given
63+
42: TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, int given
64+
73.5: TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, float given
65+
'string': string(6) "string"
66+
'15': string(2) "15"
67+
'56.7': string(4) "56.7"
68+
'stdClass': string(8) "stdClass"
69+
anon class name: string(%d) "class@anonymous%s"
70+
[]: array(0) {}
71+
new stdClass(): TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, stdClass given
72+
new S(): TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, S given
73+
STDOUT: TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, resource given
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--TEST--
2+
Test array_ht_or_str ZPP specifier (weak_mode)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$types = require 'types.inc';
9+
$functions = [
10+
'zend_array_ht_or_str',
11+
'zend_array_ht_or_str_or_null',
12+
];
13+
14+
foreach ($functions as $i => $function) {
15+
echo "Using $function:\n\n";
16+
17+
foreach ($types as $name => $type) {
18+
printf(" %-16s ", "$name:");
19+
20+
try {
21+
$result = $function($type);
22+
23+
$result === []
24+
? printf("array(0) {}\n")
25+
: var_dump($result);
26+
27+
} catch (Throwable $e) {
28+
echo $e::class, ': ', $e->getMessage(), "\n";
29+
}
30+
}
31+
32+
if ($i !== array_key_last($functions)) {
33+
echo "\n";
34+
}
35+
}
36+
37+
?>
38+
--EXPECTF--
39+
Using zend_array_ht_or_str:
40+
41+
null:
42+
Deprecated: zend_array_ht_or_str(): Passing null to parameter #1 ($param) of type array|string is deprecated in %s on line %d
43+
string(0) ""
44+
false: string(0) ""
45+
true: string(1) "1"
46+
42: string(2) "42"
47+
73.5: string(4) "73.5"
48+
'string': string(6) "string"
49+
'15': string(2) "15"
50+
'56.7': string(4) "56.7"
51+
'stdClass': string(8) "stdClass"
52+
anon class name: string(%d) "class@anonymous%s"
53+
[]: array(0) {}
54+
new stdClass(): TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, stdClass given
55+
new S(): string(7) "S class"
56+
STDOUT: TypeError: zend_array_ht_or_str(): Argument #1 ($param) must be of type array|string, resource given
57+
58+
Using zend_array_ht_or_str_or_null:
59+
60+
null: NULL
61+
false: string(0) ""
62+
true: string(1) "1"
63+
42: string(2) "42"
64+
73.5: string(4) "73.5"
65+
'string': string(6) "string"
66+
'15': string(2) "15"
67+
'56.7': string(4) "56.7"
68+
'stdClass': string(8) "stdClass"
69+
anon class name: string(%d) "class@anonymous%s"
70+
[]: array(0) {}
71+
new stdClass(): TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, stdClass given
72+
new S(): string(7) "S class"
73+
STDOUT: TypeError: zend_array_ht_or_str_or_null(): Argument #1 ($param) must be of type array|string|null, resource given

0 commit comments

Comments
 (0)