Skip to content

Commit 00ad705

Browse files
authored
Merge pull request php-amqplib#612 from cezarystepkowski/error_handler_restore
Fixed restoring previous error handler in StreamIO
2 parents 7771047 + 1ab8cc2 commit 00ad705

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

PhpAmqpLib/Wire/IO/StreamIO.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ public function connect()
147147
throw $e;
148148
}
149149

150-
restore_error_handler();
151-
152150
if (false === $this->sock) {
153151
throw new AMQPRuntimeException(
154152
sprintf(
@@ -300,7 +298,6 @@ public function write($data)
300298
restore_error_handler();
301299
throw new AMQPRuntimeException($e->getMessage());
302300
}
303-
restore_error_handler();
304301

305302
if ($buffer === false) {
306303
throw new AMQPRuntimeException('Error sending data');
@@ -464,7 +461,6 @@ public function select($sec, $usec)
464461
restore_error_handler();
465462
throw $e;
466463
}
467-
restore_error_handler();
468464

469465
return $result;
470466
}

tests/Functional/StreamIOTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class StreamIOTest extends TestCase
1212
*/
1313
public function error_handler_is_restored_on_failed_connection()
1414
{
15+
set_error_handler(array($this, 'custom_error_handler'));
16+
1517
error_reporting(~E_NOTICE);
1618

1719
$exceptionThrown = false;
@@ -33,5 +35,26 @@ public function error_handler_is_restored_on_failed_connection()
3335
$this->assertFalse($exceptionThrown, 'Default error handler was not restored.');
3436

3537
error_reporting(E_ALL);
38+
39+
$previousErrorHandler = set_error_handler(array($this, 'custom_error_handler'));
40+
$this->assertSame('custom_error_handler', $previousErrorHandler[1]);
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function error_handler_is_restored_on_success()
47+
{
48+
set_error_handler(array($this, 'custom_error_handler'));
49+
50+
new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
51+
52+
$previousErrorHandler = set_error_handler(array($this, 'custom_error_handler'));
53+
54+
$this->assertSame('custom_error_handler', $previousErrorHandler[1]);
55+
}
56+
57+
public function custom_error_handler($errno, $errstr, $errfile, $errline, $errcontext = null)
58+
{
3659
}
3760
}

0 commit comments

Comments
 (0)