ConnectionTest.php 1.59 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
use Doctrine\DBAL\Driver\Mysqli\Connection;
Sergei Morozov's avatar
Sergei Morozov committed
6
use Doctrine\DBAL\Driver\Mysqli\Driver;
7
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
Sergei Morozov's avatar
Sergei Morozov committed
8
use Doctrine\Tests\DbalFunctionalTestCase;
9
use Doctrine\Tests\TestUtil;
10

Grégoire Paris's avatar
Grégoire Paris committed
11
use const MYSQLI_OPT_CONNECT_TIMEOUT;
till's avatar
till committed
12

13 14 15
/**
 * @requires extension mysqli
 */
Sergei Morozov's avatar
Sergei Morozov committed
16
class ConnectionTest extends DbalFunctionalTestCase
till's avatar
till committed
17
{
18
    protected function setUp(): void
till's avatar
till committed
19 20
    {
        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);
39
        self::assertInstanceOf(Connection::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): Connection
till's avatar
till committed
59
    {
60 61
        $params = TestUtil::getConnectionParams();

62
        return new Connection(
63 64 65
            $params,
            $params['user'] ?? '',
            $params['password'] ?? '',
till's avatar
till committed
66 67 68
            $driverOptions
        );
    }
69
}