Skip to content

Commit 8766b4c

Browse files
committed
ext/pcre: Add trampoline tests for preg_replace_callback(_array)()
1 parent f3c981a commit 8766b4c

File tree

3 files changed

+244
-28
lines changed

3 files changed

+244
-28
lines changed

ext/pcre/tests/preg_replace_callback3.phpt

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
--TEST--
2+
preg_replace_callback_array() with a trampoline
3+
--FILE--
4+
<?php
5+
6+
class TrampolineTest {
7+
public function __call(string $name, array $arguments): string {
8+
echo 'Trampoline for ', $name, PHP_EOL;
9+
if ($name === 'trampolineThrow') {
10+
throw new Exception('boo');
11+
}
12+
return "'" . $arguments[0][0] . "'";
13+
}
14+
}
15+
$o = new TrampolineTest();
16+
$callback = [$o, 'trampoline'];
17+
$callbackThrow = [$o, 'trampolineThrow'];
18+
19+
$regexesToTest = [
20+
[
21+
'@\b\w{1,2}\b@' => $callback,
22+
'~\A.~' => $callback,
23+
],
24+
[
25+
'@\b\w{1,2}\b@' => $callback,
26+
'~\A.~' => $trampolineThrow,
27+
],
28+
[
29+
'@\b\w{1,2}\b@' => $callback,
30+
'~\A.~' => new stdClass(),
31+
],
32+
];
33+
34+
$subjectsToTest = [
35+
[
36+
'a b3 bcd',
37+
'v' => 'aksfjk',
38+
12 => 'aa bb',
39+
['xyz'],
40+
],
41+
'a b3 bcd',
42+
[
43+
new stdClass(),
44+
],
45+
new stdClass(),
46+
];
47+
48+
foreach ($regexesToTest as $regex) {
49+
foreach ($subjectsToTest as $subject) {
50+
try {
51+
$matches = preg_replace_callback_array($regex, $subject);
52+
var_dump($matches);
53+
} catch (Throwable $e) {
54+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
55+
}
56+
}
57+
}
58+
59+
?>
60+
--EXPECTF--
61+
Warning: Undefined variable $trampolineThrow in %s on line %d
62+
Trampoline for trampoline
63+
Trampoline for trampoline
64+
Trampoline for trampoline
65+
Trampoline for trampoline
66+
67+
Warning: Array to string conversion in %s on line %d
68+
Trampoline for trampoline
69+
Trampoline for trampoline
70+
Trampoline for trampoline
71+
Trampoline for trampoline
72+
array(4) {
73+
[0]=>
74+
string(14) "'''a' 'b3' bcd"
75+
["v"]=>
76+
string(8) "'a'ksfjk"
77+
[12]=>
78+
string(11) "'''aa' 'bb'"
79+
[13]=>
80+
string(7) "'A'rray"
81+
}
82+
Trampoline for trampoline
83+
Trampoline for trampoline
84+
Trampoline for trampoline
85+
string(14) "'''a' 'b3' bcd"
86+
Error: Object of class stdClass could not be converted to string
87+
TypeError: preg_replace_callback_array(): Argument #2 ($subject) must be of type array|string, stdClass given
88+
Trampoline for trampoline
89+
Trampoline for trampoline
90+
Trampoline for trampoline
91+
Trampoline for trampoline
92+
93+
Warning: Array to string conversion in %s on line %d
94+
TypeError: preg_replace_callback_array(): Argument #1 ($pattern) must contain only valid callbacks
95+
Trampoline for trampoline
96+
Trampoline for trampoline
97+
TypeError: preg_replace_callback_array(): Argument #1 ($pattern) must contain only valid callbacks
98+
Error: Object of class stdClass could not be converted to string
99+
TypeError: preg_replace_callback_array(): Argument #2 ($subject) must be of type array|string, stdClass given
100+
Trampoline for trampoline
101+
Trampoline for trampoline
102+
Trampoline for trampoline
103+
Trampoline for trampoline
104+
105+
Warning: Array to string conversion in %s on line %d
106+
TypeError: preg_replace_callback_array(): Argument #1 ($pattern) must contain only valid callbacks
107+
Trampoline for trampoline
108+
Trampoline for trampoline
109+
TypeError: preg_replace_callback_array(): Argument #1 ($pattern) must contain only valid callbacks
110+
Error: Object of class stdClass could not be converted to string
111+
TypeError: preg_replace_callback_array(): Argument #2 ($subject) must be of type array|string, stdClass given
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
--TEST--
2+
preg_replace_callback() with a trampoline
3+
--FILE--
4+
<?php
5+
6+
class TrampolineTest {
7+
public function __call(string $name, array $arguments): string {
8+
echo 'Trampoline for ', $name, PHP_EOL;
9+
if ($name === 'trampolineThrow') {
10+
throw new Exception('boo');
11+
}
12+
return "'" . $arguments[0][0] . "'";
13+
}
14+
}
15+
$o = new TrampolineTest();
16+
$callback = [$o, 'trampoline'];
17+
$callbackThrow = [$o, 'trampolineThrow'];
18+
19+
$regexesToTest = [
20+
[
21+
'@\b\w{1,2}\b@',
22+
'~\A.~',
23+
],
24+
'@\b\w{1,2}\b@',
25+
[
26+
new stdClass(),
27+
],
28+
];
29+
30+
$subjectsToTest = [
31+
[
32+
'a b3 bcd',
33+
'v' => 'aksfjk',
34+
12 => 'aa bb',
35+
['xyz'],
36+
],
37+
'a b3 bcd',
38+
[
39+
new stdClass(),
40+
],
41+
new stdClass(),
42+
];
43+
44+
foreach ([$callback, $callbackThrow] as $fn) {
45+
foreach ($regexesToTest as $regex) {
46+
foreach ($subjectsToTest as $subject) {
47+
try {
48+
$matches = preg_replace_callback($regex, $fn, $subject);
49+
var_dump($matches);
50+
} catch (Throwable $e) {
51+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
52+
}
53+
}
54+
}
55+
}
56+
57+
?>
58+
--EXPECTF--
59+
Trampoline for trampoline
60+
Trampoline for trampoline
61+
Trampoline for trampoline
62+
Trampoline for trampoline
63+
Trampoline for trampoline
64+
Trampoline for trampoline
65+
Trampoline for trampoline
66+
67+
Warning: Array to string conversion in %s on line %d
68+
Trampoline for trampoline
69+
array(4) {
70+
[0]=>
71+
string(14) "'''a' 'b3' bcd"
72+
["v"]=>
73+
string(8) "'a'ksfjk"
74+
[12]=>
75+
string(11) "'''aa' 'bb'"
76+
[13]=>
77+
string(7) "'A'rray"
78+
}
79+
Trampoline for trampoline
80+
Trampoline for trampoline
81+
Trampoline for trampoline
82+
string(14) "'''a' 'b3' bcd"
83+
Error: Object of class stdClass could not be converted to string
84+
TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given
85+
Trampoline for trampoline
86+
Trampoline for trampoline
87+
Trampoline for trampoline
88+
Trampoline for trampoline
89+
90+
Warning: Array to string conversion in %s on line %d
91+
array(4) {
92+
[0]=>
93+
string(12) "'a' 'b3' bcd"
94+
["v"]=>
95+
string(6) "aksfjk"
96+
[12]=>
97+
string(9) "'aa' 'bb'"
98+
[13]=>
99+
string(5) "Array"
100+
}
101+
Trampoline for trampoline
102+
Trampoline for trampoline
103+
string(12) "'a' 'b3' bcd"
104+
Error: Object of class stdClass could not be converted to string
105+
TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given
106+
107+
Warning: Array to string conversion in %s on line %d
108+
Error: Object of class stdClass could not be converted to string
109+
Error: Object of class stdClass could not be converted to string
110+
Error: Object of class stdClass could not be converted to string
111+
TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given
112+
Trampoline for trampolineThrow
113+
114+
Warning: Array to string conversion in %s on line %d
115+
Exception: boo
116+
Trampoline for trampolineThrow
117+
Exception: boo
118+
Error: Object of class stdClass could not be converted to string
119+
TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given
120+
Trampoline for trampolineThrow
121+
122+
Warning: Array to string conversion in %s on line %d
123+
Exception: boo
124+
Trampoline for trampolineThrow
125+
Exception: boo
126+
Error: Object of class stdClass could not be converted to string
127+
TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given
128+
129+
Warning: Array to string conversion in %s on line %d
130+
Error: Object of class stdClass could not be converted to string
131+
Error: Object of class stdClass could not be converted to string
132+
Error: Object of class stdClass could not be converted to string
133+
TypeError: preg_replace_callback(): Argument #3 ($subject) must be of type array|string, stdClass given

0 commit comments

Comments
 (0)