ConnectionTest.php 1.8 KB
Newer Older
till's avatar
till committed
1
<?php
Sergei Morozov's avatar
Sergei Morozov committed
2

3
namespace Doctrine\Tests\DBAL\Functional\Driver\Mysqli;
Sergei Morozov's avatar
Sergei Morozov committed
4 5 6

use Doctrine\DBAL\Driver\Mysqli\Driver;
use Doctrine\DBAL\Driver\Mysqli\MysqliConnection;
7
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
8
use Doctrine\Tests\DBAL\FunctionalTestCase;
Sergei Morozov's avatar
Sergei Morozov committed
9
use const MYSQLI_OPT_CONNECT_TIMEOUT;
10
use function extension_loaded;
till's avatar
till committed
11

12
class ConnectionTest extends FunctionalTestCase
till's avatar
till committed
13
{
14
    protected function setUp() : void
till's avatar
till committed
15
    {
Sergei Morozov's avatar
Sergei Morozov committed
16
        if (! extension_loaded('mysqli')) {
till's avatar
till committed
17 18 19 20
            $this->markTestSkipped('mysqli is not installed.');
        }

        parent::setUp();
21

Sergei Morozov's avatar
Sergei Morozov committed
22
        if ($this->connection->getDriver() instanceof Driver) {
Sergei Morozov's avatar
Sergei Morozov committed
23
            return;
24
        }
Sergei Morozov's avatar
Sergei Morozov committed
25 26

        $this->markTestSkipped('MySQLi only test.');
till's avatar
till committed
27 28
    }

29
    protected function tearDown() : void
till's avatar
till committed
30 31 32 33
    {
        parent::tearDown();
    }

34
    public function testDriverOptions() : void
till's avatar
till committed
35
    {
Sergei Morozov's avatar
Sergei Morozov committed
36
        $driverOptions = [MYSQLI_OPT_CONNECT_TIMEOUT => 1];
till's avatar
till committed
37 38

        $connection = $this->getConnection($driverOptions);
Sergei Morozov's avatar
Sergei Morozov committed
39
        self::assertInstanceOf(MysqliConnection::class, $connection);
till's avatar
till committed
40 41
    }

42
    public function testUnsupportedDriverOption() : void
till's avatar
till committed
43
    {
44 45
        $this->expectException(MysqliException::class);

Sergei Morozov's avatar
Sergei Morozov committed
46
        $this->getConnection(['hello' => 'world']); // use local infile
till's avatar
till committed
47 48
    }

49
    public function testPing() : void
50
    {
Sergei Morozov's avatar
Sergei Morozov committed
51
        $conn = $this->getConnection([]);
52
        self::assertTrue($conn->ping());
53 54
    }

Sergei Morozov's avatar
Sergei Morozov committed
55 56 57
    /**
     * @param mixed[] $driverOptions
     */
58
    private function getConnection(array $driverOptions) : MysqliConnection
till's avatar
till committed
59
    {
Sergei Morozov's avatar
Sergei Morozov committed
60 61 62 63
        return new MysqliConnection(
            [
                'host' => $GLOBALS['db_host'],
                'dbname' => $GLOBALS['db_name'],
64
                'port' => $GLOBALS['db_port'],
Sergei Morozov's avatar
Sergei Morozov committed
65
            ],
till's avatar
till committed
66 67 68 69 70
            $GLOBALS['db_username'],
            $GLOBALS['db_password'],
            $driverOptions
        );
    }
71
}