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

3
namespace Doctrine\DBAL\Tests\Driver\Mysqli;
4

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

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

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

30 31
        parent::setUp();

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

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

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

46
    public function testRestoresErrorHandlerOnException() : void
47
    {
48
        $handler         = static function () : bool {
Sergei Morozov's avatar
Sergei Morozov committed
49 50
            self::fail('Never expected this to be called');
        };
51 52 53 54 55 56 57 58 59 60
        $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.');
61 62
        restore_error_handler();
        restore_error_handler();
63
    }
64 65 66 67 68 69

    public function testHostnameIsRequiredForPersistentConnection() : void
    {
        $this->expectException(HostRequired::class);
        new MysqliConnection(['persistent' => 'true'], '', '');
    }
70
}