Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-4645] Use error suppression instead of an error handler in MySQLi Connection #4647

Merged
merged 1 commit into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}