1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
<?php
namespace PhpAmqpLib\Tests\Functional;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
/**
* @group connection
*/
class StreamIOTest extends TestCase
{
/** @var array|null */
protected $last_error;
#[Test]
public function error_handler_is_restored_on_failed_connection()
{
$this->last_error = null;
set_error_handler(array($this, 'custom_error_handler'));
error_reporting(~E_NOTICE);
$exceptionThrown = false;
try {
new AMQPStreamConnection(HOST, 5670, USER, PASS, VHOST);
} catch (\Exception $exception) {
$exceptionThrown = true;
}
$this->assertTrue($exceptionThrown, 'Custom error handler was not set.');
$this->assertInstanceOf('PhpAmqpLib\Exception\AMQPIOException', $exception);
$this->assertNull($this->last_error);
$exceptionThrown = false;
$arr = [];
try {
$notice = $arr['second-key-that-does-not-exist-and-should-generate-a-notice'];
} catch (\Exception $exception) {
$exceptionThrown = true;
}
$this->assertFalse($exceptionThrown, 'Default error handler was not restored.');
error_reporting(E_ALL);
$previousErrorHandler = set_error_handler(array($this, 'custom_error_handler'));
$this->assertSame('custom_error_handler', $previousErrorHandler[1]);
}
#[Test]
public function error_handler_is_restored_on_success()
{
set_error_handler(array($this, 'custom_error_handler'));
new AMQPStreamConnection(HOST, PORT, USER, PASS, VHOST);
$previousErrorHandler = set_error_handler(array($this, 'custom_error_handler'));
$this->assertSame('custom_error_handler', $previousErrorHandler[1]);
}
public function custom_error_handler($errno, $errstr, $errfile, $errline, $errcontext = null)
{
$this->last_error = compact('errno', 'errstr', 'errfile', 'errline', 'errcontext');
}
}
|