Skip to content

Commit 198a391

Browse files
Test returning objects
1 parent 47c9836 commit 198a391

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

tests/output/ob_start_callback_bad_return/exception_handler.phpt

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ ob_start(): Check behaviour with deprecation converted to exception
33
--FILE--
44
<?php
55

6+
class NotStringable {
7+
public function __construct(public string $val) {}
8+
}
9+
class IsStringable {
10+
public function __construct(public string $val) {}
11+
public function __toString() {
12+
return __CLASS__ . ": " . $this->val;
13+
}
14+
}
15+
616
$log = [];
717

818
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
@@ -33,7 +43,26 @@ function return_zero($string) {
3343
return 0;
3444
}
3545

36-
$cases = ['return_null', 'return_false', 'return_true', 'return_zero'];
46+
function return_non_stringable($string) {
47+
global $log;
48+
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
49+
return new NotStringable($string);
50+
}
51+
52+
function return_stringable($string) {
53+
global $log;
54+
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
55+
return new IsStringable($string);
56+
}
57+
58+
$cases = [
59+
'return_null',
60+
'return_false',
61+
'return_true',
62+
'return_zero',
63+
'return_non_stringable',
64+
'return_stringable',
65+
];
3766
foreach ($cases as $case) {
3867
$log = [];
3968
echo "\n\nTesting: $case\n";
@@ -94,3 +123,25 @@ Stack trace:
94123
End of return_zero, log was:
95124
return_zero: <<<Inside of return_zero
96125
>>>
126+
127+
Testing: return_non_stringable
128+
ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_non_stringable is deprecated in %s:%d
129+
Stack trace:
130+
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', '/usr/src/php/te...', 69)
131+
#1 %s(%d): ob_end_flush()
132+
#2 {main}
133+
134+
End of return_non_stringable, log was:
135+
return_non_stringable: <<<Inside of return_non_stringable
136+
>>>
137+
138+
Testing: return_stringable
139+
ErrorException: ob_end_flush(): Returning a non-string result from user output handler return_stringable is deprecated in %s:%d
140+
Stack trace:
141+
#0 [internal function]: {closure:%s:%d}(8192, 'ob_end_flush():...', '/usr/src/php/te...', 69)
142+
#1 %s(%d): ob_end_flush()
143+
#2 {main}
144+
145+
End of return_stringable, log was:
146+
return_stringable: <<<Inside of return_stringable
147+
>>>

tests/output/ob_start_callback_bad_return/exception_handler_nested.phpt

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ ob_start(): Check behaviour with deprecation converted to exception
33
--FILE--
44
<?php
55

6+
class NotStringable {
7+
public function __construct(public string $val) {}
8+
}
9+
class IsStringable {
10+
public function __construct(public string $val) {}
11+
public function __toString() {
12+
return __CLASS__ . ": " . $this->val;
13+
}
14+
}
15+
616
$log = [];
717

818
set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
@@ -33,12 +43,40 @@ function return_zero($string) {
3343
return 0;
3444
}
3545

46+
function return_non_stringable($string) {
47+
global $log;
48+
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
49+
return new NotStringable($string);
50+
}
51+
52+
function return_stringable($string) {
53+
global $log;
54+
$log[] = __FUNCTION__ . ": <<<" . $string . ">>>";
55+
return new IsStringable($string);
56+
}
57+
3658
ob_start('return_null');
3759
ob_start('return_false');
3860
ob_start('return_true');
3961
ob_start('return_zero');
62+
ob_start('return_non_stringable');
63+
ob_start('return_stringable');
4064

4165
echo "In all of them\n\n";
66+
try {
67+
ob_end_flush();
68+
} catch (\ErrorException $e) {
69+
echo $e->getMessage() . "\n";
70+
}
71+
echo "Ended return_stringable handler\n\n";
72+
73+
try {
74+
ob_end_flush();
75+
} catch (\ErrorException $e) {
76+
echo $e->getMessage() . "\n";
77+
}
78+
echo "Ended return_non_stringable handler\n\n";
79+
4280
try {
4381
ob_end_flush();
4482
} catch (\ErrorException $e) {
@@ -77,7 +115,15 @@ Ended return_null handler
77115

78116
All handlers are over
79117

80-
return_zero: <<<In all of them
118+
return_stringable: <<<In all of them
119+
120+
>>>
121+
return_non_stringable: <<<ob_end_flush(): Returning a non-string result from user output handler return_stringable is deprecated
122+
Ended return_stringable handler
123+
124+
>>>
125+
return_zero: <<<ob_end_flush(): Returning a non-string result from user output handler return_non_stringable is deprecated
126+
Ended return_non_stringable handler
81127

82128
>>>
83129
return_true: <<<0ob_end_flush(): Returning a non-string result from user output handler return_zero is deprecated
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns stringable object)
3+
--INI--
4+
memory_limit=2M
5+
--FILE--
6+
<?php
7+
8+
class IsStringable {
9+
public function __construct(public string $val) {}
10+
public function __toString() {
11+
return __CLASS__ . ": " . $this->val;
12+
}
13+
}
14+
15+
ob_start(function() {
16+
// We are out of memory, now trigger a deprecation
17+
return new IsStringable("");
18+
});
19+
20+
$a = [];
21+
// trigger OOM in a resize operation
22+
while (1) {
23+
$a[] = 1;
24+
}
25+
26+
?>
27+
--EXPECTF--
28+
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
29+
30+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
ob_start(): Check behaviour with deprecation when OOM triggers handler removal (handler returns non-stringable object)
3+
--INI--
4+
memory_limit=2M
5+
--FILE--
6+
<?php
7+
8+
class NotStringable {
9+
public function __construct(public string $val) {}
10+
}
11+
12+
ob_start(function() {
13+
// We are out of memory, now trigger a deprecation
14+
return new NotStringable("");
15+
});
16+
17+
$a = [];
18+
// trigger OOM in a resize operation
19+
while (1) {
20+
$a[] = 1;
21+
}
22+
23+
?>
24+
--EXPECTF--
25+
Deprecated: main(): Returning a non-string result from user output handler {closure:%s:%d} is deprecated in %s on line %d
26+
27+
Fatal error: Allowed memory size of %d bytes exhausted%s(tried to allocate %d bytes) in %s on line %d
28+
29+
Fatal error: Uncaught Error: Object of class NotStringable could not be converted to string in %s:%d
30+
Stack trace:
31+
#0 {main}
32+
thrown in %s on line %d

0 commit comments

Comments
 (0)