Skip to content

Commit

Permalink
[doctrineGH-4645] Use error suppression instead of an error handler i…
Browse files Browse the repository at this point in the history
…n MySQLi Connection
  • Loading branch information
morozov committed May 12, 2021
1 parent b728198 commit b9b30b2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 35 deletions.
13 changes: 2 additions & 11 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
use function mysqli_error;
use function mysqli_init;
use function mysqli_options;
use function restore_error_handler;
use function set_error_handler;
use function sprintf;
use function stripos;

Expand Down Expand Up @@ -79,15 +77,8 @@ public function __construct(array $params, $username, $password, array $driverOp
$this->setSecureConnection($params);
$this->setDriverOptions($driverOptions);

set_error_handler(static function (): bool {
return false;
});
try {
if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
throw ConnectionFailed::new($this->conn);
}
} finally {
restore_error_handler();
if (! @$this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
throw ConnectionFailed::new($this->conn);
}

if (! isset($params['charset'])) {
Expand Down
24 changes: 0 additions & 24 deletions tests/Doctrine/Tests/DBAL/Driver/Mysqli/MysqliConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
namespace Doctrine\Tests\DBAL\Driver\Mysqli;

use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\Tests\DbalFunctionalTestCase;
use PHPUnit\Framework\MockObject\MockObject;

use function restore_error_handler;
use function set_error_handler;

/**
* @requires extension mysqli
*/
Expand Down Expand Up @@ -40,24 +36,4 @@ public function testDoesNotRequireQueryForServerVersion(): void
{
self::assertFalse($this->connectionMock->requiresQueryForServerVersion());
}

public function testRestoresErrorHandlerOnException(): void
{
$handler = static function (): bool {
self::fail('Never expected this to be called');
};

$defaultHandler = set_error_handler($handler);

try {
new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass');
self::fail('An exception was supposed to be raised');
} catch (MysqliException $e) {
self::assertSame('Network is unreachable', $e->getMessage());
}

self::assertSame($handler, set_error_handler($defaultHandler), 'Restoring error handler failed.');
restore_error_handler();
restore_error_handler();
}
}

0 comments on commit b9b30b2

Please sign in to comment.