MysqliConnectionTest.php 1.94 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Driver\Mysqli;

5 6
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
7 8
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\Tests\DbalFunctionalTestCase;
9
use PHPUnit\Framework\MockObject\MockObject;
10 11 12
use function extension_loaded;
use function restore_error_handler;
use function set_error_handler;
13

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

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

29 30
        parent::setUp();

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

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

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

45
    public function testRestoresErrorHandlerOnException() : void
46
    {
47
        $handler         = static function () : void {
Sergei Morozov's avatar
Sergei Morozov committed
48 49
            self::fail('Never expected this to be called');
        };
50 51 52 53 54 55 56 57 58 59
        $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');
        } catch (MysqliException $e) {
            self::assertSame('Network is unreachable', $e->getMessage());
        }

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