summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2019-02-26 15:32:18 +0100
committerNikita Popov <nikita.ppv@gmail.com>2019-06-05 14:25:07 +0200
commita31f46421d7bf6f55dd9ac5876b8e2eacf7e0708 (patch)
tree24ffd7c5ae5e321c3994048fdd0fd9f68ae7457c /tests
parent528aa7932a839fc6319979c34aa372805d8dc41c (diff)
downloadphp-git-a31f46421d7bf6f55dd9ac5876b8e2eacf7e0708.tar.gz
Allow exceptions in __toString()
RFC: https://wiki.php.net/rfc/tostring_exceptions And convert some object to string conversion related recoverable fatal errors into Error exceptions. Improve exception safety of internal code performing string conversions.
Diffstat (limited to 'tests')
-rw-r--r--tests/classes/tostring_001.phpt32
-rw-r--r--tests/classes/tostring_003.phpt5
-rw-r--r--tests/classes/tostring_004.phpt37
3 files changed, 46 insertions, 28 deletions
diff --git a/tests/classes/tostring_001.phpt b/tests/classes/tostring_001.phpt
index 53144ca207..418aa1fc25 100644
--- a/tests/classes/tostring_001.phpt
+++ b/tests/classes/tostring_001.phpt
@@ -3,12 +3,6 @@ ZE2 __toString()
--FILE--
<?php
-function my_error_handler($errno, $errstr, $errfile, $errline) {
- var_dump($errstr);
-}
-
-set_error_handler('my_error_handler');
-
class test1
{
}
@@ -33,7 +27,11 @@ class test3
echo "====test1====\n";
$o = new test1;
print_r($o);
-var_dump((string)$o);
+try {
+ var_dump((string)$o);
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
var_dump($o);
echo "====test2====\n";
@@ -70,7 +68,11 @@ echo sprintf("%s", $o);
echo "====test10====\n";
$o = new test3;
var_dump($o);
-echo $o;
+try {
+ echo $o;
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
?>
====DONE====
@@ -79,9 +81,8 @@ echo $o;
test1 Object
(
)
-string(54) "Object of class test1 could not be converted to string"
-string(0) ""
-object(test1)#%d (0) {
+Object of class test1 could not be converted to string
+object(test1)#1 (0) {
}
====test2====
test2 Object
@@ -89,7 +90,7 @@ test2 Object
)
test2::__toString()
Converted
-object(test2)#%d (0) {
+object(test2)#3 (0) {
}
====test3====
test2::__toString()
@@ -113,7 +114,8 @@ test2::__toString()
Converted
====test7====
test2::__toString()
-string(19) "Illegal offset type"
+
+Warning: Illegal offset type in %s on line %d
====test8====
test2::__toString()
string(9) "Converted"
@@ -123,8 +125,8 @@ string(9) "Converted"
test2::__toString()
Converted
====test10====
-object(test3)#%d (0) {
+object(test3)#1 (0) {
}
test3::__toString()
-string(53) "Method test3::__toString() must return a string value"
+Method test3::__toString() must return a string value
====DONE====
diff --git a/tests/classes/tostring_003.phpt b/tests/classes/tostring_003.phpt
index e3bc7f8f2c..4dad51f2cf 100644
--- a/tests/classes/tostring_003.phpt
+++ b/tests/classes/tostring_003.phpt
@@ -29,5 +29,6 @@ catch(Exception $e)
?>
====DONE====
---EXPECTF--
-Fatal error: Method Test::__toString() must not throw an exception, caught Exception: Damn! in %stostring_003.php on line %d
+--EXPECT--
+string(5) "Damn!"
+====DONE====
diff --git a/tests/classes/tostring_004.phpt b/tests/classes/tostring_004.phpt
index 907f7bc306..4134c702b6 100644
--- a/tests/classes/tostring_004.phpt
+++ b/tests/classes/tostring_004.phpt
@@ -12,12 +12,19 @@ error_reporting(8191);
echo "Object with no __toString():\n";
$obj = new stdClass;
echo "Try 1:\n";
-printf($obj);
+try {
+ printf($obj);
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
printf("\n");
echo "\nTry 2:\n";
-printf($obj . "\n");
-
+try {
+ printf($obj . "\n");
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
echo "\n\nObject with bad __toString():\n";
class badToString {
@@ -25,30 +32,38 @@ class badToString {
return 0;
}
}
+
$obj = new badToString;
echo "Try 1:\n";
-printf($obj);
+try {
+ printf($obj);
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
printf("\n");
echo "\nTry 2:\n";
-printf($obj . "\n");
+try {
+ printf($obj . "\n");
+} catch (Error $e) {
+ echo $e->getMessage(), "\n";
+}
?>
--EXPECT--
Object with no __toString():
Try 1:
-Error: 4096 - Object of class stdClass could not be converted to string
-Object
+Object of class stdClass could not be converted to string
-Try 2:
-Error: 4096 - Object of class stdClass could not be converted to string
+Try 2:
+Object of class stdClass could not be converted to string
Object with bad __toString():
Try 1:
-Error: 4096 - Method badToString::__toString() must return a string value
+Method badToString::__toString() must return a string value
Try 2:
-Error: 4096 - Method badToString::__toString() must return a string value
+Method badToString::__toString() must return a string value