Skip to content
25 changes: 11 additions & 14 deletions Zend/tests/bug26166.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ echo $o;

echo "===NONE===\n";

function my_error_handler($errno, $errstr, $errfile, $errline) {
var_dump($errstr);
}

set_error_handler('my_error_handler');

class NoneTest
{
function __toString() {
}
}

$o = new NoneTest;
echo $o;
try {
echo $o;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

echo "===THROW===\n";

Expand All @@ -58,17 +56,16 @@ class ErrorTest
$o = new ErrorTest;
try {
echo $o;
}
catch (Exception $e) {
echo "Got the exception\n";
} catch (Exception $e) {
echo $e->getMessage(), "\n";
}

?>
===DONE===
--EXPECTF--
--EXPECT--
Hello World!
===NONE===
string(%d) "Method NoneTest::__toString() must return a string value"
Method NoneTest::__toString() must return a string value
===THROW===

Fatal error: Method ErrorTest::__toString() must not throw an exception, caught Exception: This is an error! in %sbug26166.php on line %d
This is an error!
===DONE===
19 changes: 8 additions & 11 deletions Zend/tests/bug28444.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ Bug #28444 (Cannot access undefined property for object with overloaded property
--FILE--
<?php

function my_error_handler($errno, $errstr, $errfile, $errline) {
var_dump($errstr);
}

set_error_handler('my_error_handler');

class ObjectOne
{
public $x;
Expand All @@ -17,6 +11,10 @@ class ObjectOne
{
$this->x = $x;
}

function __toString() {
return "Object";
}
}

class Overloaded
Expand Down Expand Up @@ -55,8 +53,8 @@ var_dump($y->z->x = 6);

?>
===DONE===
--EXPECTF--
object(ObjectOne)#%d (1) {
--EXPECT--
object(ObjectOne)#2 (1) {
["x"]=>
int(2)
}
Expand All @@ -66,9 +64,8 @@ Overloaded::__set(y,3)
int(3)
Overloaded::__get(y)
int(3)
string(58) "Object of class ObjectOne could not be converted to string"
Overloaded::__set(z,)
object(ObjectOne)#%d (1) {
Overloaded::__set(z,Object)
object(ObjectOne)#3 (1) {
["x"]=>
int(4)
}
Expand Down
25 changes: 9 additions & 16 deletions Zend/tests/bug30791.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@ Bug #30791 (magic methods (__sleep/__wakeup/__toString) call __call if object is
--FILE--
<?php

function my_error_handler($errno, $errstr, $errfile, $errline) {
var_dump($errstr);
}

set_error_handler('my_error_handler');

class a
{
public $a = 4;
function __call($a,$b) {
return "unknown method";
}
public $a = 4;
function __call($name, $args) {
echo __METHOD__, "\n";
}
}

$b = new a;
echo $b,"\n";
var_dump($b);
$c = unserialize(serialize($b));
echo $c,"\n";
var_dump($c);

?>
--EXPECT--
string(50) "Object of class a could not be converted to string"

string(50) "Object of class a could not be converted to string"

object(a)#1 (1) {
["a"]=>
int(4)
}
object(a)#2 (1) {
["a"]=>
int(4)
Expand Down
9 changes: 6 additions & 3 deletions Zend/tests/bug60909_2.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ Bug #60909 (custom error handler throwing Exception + fatal error = no shutdown
--FILE--
<?php
register_shutdown_function(function(){echo("\n\n!!!shutdown!!!\n\n");});
set_error_handler(function($errno, $errstr, $errfile, $errline){throw new Exception("Foo");});

class Bad {
public function __toString() {
throw new Exception('Oops, I cannot do this');
throw new Exception('I CAN DO THIS');
}
}

$bad = new Bad();
echo "$bad";
--EXPECTF--
Fatal error: Method Bad::__toString() must not throw an exception, caught Exception: Oops, I cannot do this in %sbug60909_2.php on line %d
Fatal error: Uncaught Exception: I CAN DO THIS in %s:%d
Stack trace:
#0 %s(%d): Bad->__toString()
#1 {main}
thrown in %s on line %d


!!!shutdown!!!
6 changes: 5 additions & 1 deletion Zend/tests/bug70967.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ class A {
echo (new A);
?>
--EXPECTF--
Fatal error: Method A::__toString() must not throw an exception, caught Error: Call to undefined function undefined_function() in %sbug70967.php on line %d
Fatal error: Uncaught Error: Call to undefined function undefined_function() in %s:%d
Stack trace:
#0 %s(%d): A->__toString()
#1 {main}
thrown in %s on line %d
6 changes: 5 additions & 1 deletion Zend/tests/bug72162.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ $var11 = new StdClass();
$var16 = error_reporting($var11);
?>
--EXPECTF--
Recoverable fatal error: Object of class stdClass could not be converted to string in %sbug72162.php on line %d
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in %s:%d
Stack trace:
#0 %s(%d): error_reporting(Object(stdClass))
#1 {main}
thrown in %s on line %d
17 changes: 9 additions & 8 deletions Zend/tests/call_with_refs.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
Check call to non-ref function with call-time refs
--FILE--
<?php
function my_errorhandler($errno,$errormsg) {
global $my_var;
$my_var=0x12345;
echo $errormsg."\n";
return true;
class Test {
public function __toString() {
global $my_var;
$my_var=0x12345;
return "";
}
}
$oldhandler = set_error_handler("my_errorhandler");

$my_var = str_repeat("A",64);
$data = call_user_func_array("substr_replace",array(&$my_var, new StdClass(),1));
$data = call_user_func_array("substr_replace",array(&$my_var, new Test(), 1));
echo "OK!";
?>
--EXPECT--
Object of class stdClass could not be converted to string
OK!
5 changes: 4 additions & 1 deletion Zend/tests/class_properties_const.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ NULL
Notice: Undefined property: A::$1 in %sclass_properties_const.php on line %d
NULL

Recoverable fatal error: Object of class Closure could not be converted to string in %sclass_properties_const.php on line %d
Fatal error: Uncaught Error: Object of class Closure could not be converted to string in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
28 changes: 15 additions & 13 deletions Zend/tests/closure_015.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
Closure 015: converting to string/unicode
--FILE--
<?php
set_error_handler('myErrorHandler', E_RECOVERABLE_ERROR);
function myErrorHandler($errno, $errstr, $errfile, $errline) {
echo "Error: $errstr at $errfile($errline)\n";
return true;
}

$x = function() { return 1; };
print (string) $x;
print "\n";
print $x;
print "\n";
?>
--EXPECTF--
Error: Object of class Closure could not be converted to string at %sclosure_015.php(8)
try {
print (string) $x;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
try {
print $x;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}

Error: Object of class Closure could not be converted to string at %sclosure_015.php(10)
?>
--EXPECT--
Object of class Closure could not be converted to string
Object of class Closure could not be converted to string
6 changes: 5 additions & 1 deletion Zend/tests/exception_009.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ throw new my_exception;

?>
--EXPECT--
Recoverable fatal error: Object of class stdClass could not be converted to string in Unknown on line 0
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in [no active file]:0
Stack trace:
#0 [internal function]: Exception->__toString()
#1 {main}
thrown in [no active file] on line 0
135 changes: 135 additions & 0 deletions Zend/tests/exception_from_toString.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
--TEST--
Test exceptions thrown from __toString() in various contexts
--FILE--
<?php

class BadStr {
public function __toString() {
throw new Exception("Exception");
}
}

$str = "a";
$num = 42;
$badStr = new BadStr;

try { $x = $str . $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = $badStr . $str; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = $str .= $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump($str);
try { $x = $num . $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = $badStr . $num; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = $num .= $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump($num);

try { $x = $badStr .= $str; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump($badStr);
try { $x = $badStr .= $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump($badStr);

try { $x = "x$badStr"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = "{$badStr}x"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = "$str$badStr"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = "$badStr$str"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

try { $x = "x$badStr$str"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = "x$str$badStr"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = "{$str}x$badStr"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
try { $x = "{$badStr}x$str"; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

try { $x = (string) $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

try { $x = include $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

try { echo $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

${""} = 42;
try { unset(${$badStr}); }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump(${""});

unset(${""});
try { $x = ${$badStr}; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

try { $x = isset(${$badStr}); }
catch (Exception $e) { echo $e->getMessage(), "\n"; }

$obj = new stdClass;
try { $x = $obj->{$badStr} = $str; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump($obj);

try { $str[0] = $badStr; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump($str);

$obj = new DateInterval('P1D');
try { $x = $obj->{$badStr} = $str; }
catch (Exception $e) { echo $e->getMessage(), "\n"; }
var_dump(!isset($obj->{""}));

try { strlen($badStr); } catch (Exception $e) { echo "Exception\n"; }
try { substr($badStr, 0); } catch (Exception $e) { echo "Exception\n"; }
try { new ArrayObject([], 0, $badStr); } catch (Exception $e) { echo "Exception\n"; }

?>
--EXPECT--
Exception
Exception
Exception
string(1) "a"
Exception
Exception
Exception
int(42)
Exception
object(BadStr)#1 (0) {
}
Exception
object(BadStr)#1 (0) {
}
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
Exception
int(42)
Exception
Exception
Exception
object(stdClass)#2 (0) {
}
Exception
string(1) "a"
Exception
bool(true)
Exception
Exception
Exception
Loading