MysqliConnectionTest.php 1.93 KB
Newer Older
1 2
<?php

Michael Moravec's avatar
Michael Moravec committed
3 4
declare(strict_types=1);

5 6
namespace Doctrine\Tests\DBAL\Driver\Mysqli;

7
use Doctrine\DBAL\Driver\Mysqli\Exception\ConnectionError;
8
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
9 10
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\Tests\DbalFunctionalTestCase;
11
use PHPUnit\Framework\MockObject\MockObject;
12 13 14
use function extension_loaded;
use function restore_error_handler;
use function set_error_handler;
15

16
class MysqliConnectionTest extends DbalFunctionalTestCase
17 18 19 20
{
    /**
     * The mysqli driver connection mock under test.
     *
21
     * @var MysqliConnection|MockObject
22 23 24
     */
    private $connectionMock;

25
    protected function setUp() : void
26
    {
Sergei Morozov's avatar
Sergei Morozov committed
27
        if (! extension_loaded('mysqli')) {
28 29 30
            $this->markTestSkipped('mysqli is not installed.');
        }

31 32
        parent::setUp();

Sergei Morozov's avatar
Sergei Morozov committed
33
        if (! $this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
34
            $this->markTestSkipped('MySQL only test.');
35 36
        }

37
        $this->connectionMock = $this->getMockBuilder(MysqliConnection::class)
38 39 40 41
            ->disableOriginalConstructor()
            ->getMockForAbstractClass();
    }

42
    public function testDoesNotRequireQueryForServerVersion() : void
43
    {
44
        self::assertFalse($this->connectionMock->requiresQueryForServerVersion());
45
    }
46

47
    public function testRestoresErrorHandlerOnException() : void
48
    {
49
        $handler         = static function () : bool {
Sergei Morozov's avatar
Sergei Morozov committed
50 51
            self::fail('Never expected this to be called');
        };
52 53 54 55 56
        $default_handler = set_error_handler($handler);

        try {
            new MysqliConnection(['host' => '255.255.255.255'], 'user', 'pass');
            self::fail('An exception was supposed to be raised');
57
        } catch (ConnectionError $e) {
58
            // Do nothing
59 60 61
        }

        self::assertSame($handler, set_error_handler($default_handler), 'Restoring error handler failed.');
62 63
        restore_error_handler();
        restore_error_handler();
64
    }
65
}