PDOConnectionTest.php 3.01 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\DBAL\Functional\Driver;

use Doctrine\DBAL\Driver\PDOConnection;
6
use Doctrine\DBAL\Driver\PDOException;
7 8 9
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver;
use Doctrine\DBAL\Driver\PDOPgSql\Driver as PDOPgSQLDriver;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSRVDriver;
10
use Doctrine\Tests\DbalFunctionalTestCase;
Sergei Morozov's avatar
Sergei Morozov committed
11
use PDO;
12
use function extension_loaded;
13
use function get_class;
14
use function sprintf;
15 16 17 18 19 20

class PDOConnectionTest extends DbalFunctionalTestCase
{
    /**
     * The PDO driver connection under test.
     *
Sergei Morozov's avatar
Sergei Morozov committed
21
     * @var PDOConnection
22 23 24
     */
    protected $driverConnection;

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

        parent::setUp();

Sergei Morozov's avatar
Sergei Morozov committed
33
        $this->driverConnection = $this->connection->getWrappedConnection();
34

Sergei Morozov's avatar
Sergei Morozov committed
35 36
        if ($this->driverConnection instanceof PDOConnection) {
            return;
37
        }
Sergei Morozov's avatar
Sergei Morozov committed
38 39

        $this->markTestSkipped('PDO connection only test.');
40 41
    }

42
    protected function tearDown() : void
43 44 45 46 47 48
    {
        $this->resetSharedConn();

        parent::tearDown();
    }

49
    public function testDoesNotRequireQueryForServerVersion() : void
50
    {
51
        self::assertFalse($this->driverConnection->requiresQueryForServerVersion());
52
    }
53

54
    public function testThrowsWrappedExceptionOnConstruct() : void
55
    {
56 57
        $this->expectException(PDOException::class);

58 59 60 61 62 63
        new PDOConnection('foo');
    }

    /**
     * @group DBAL-1022
     */
64
    public function testThrowsWrappedExceptionOnExec() : void
65
    {
66 67
        $this->expectException(PDOException::class);

68 69 70
        $this->driverConnection->exec('foo');
    }

71
    public function testThrowsWrappedExceptionOnPrepare() : void
72
    {
73 74 75
        $driver = $this->connection->getDriver();

        if ($driver instanceof PDOSQLSRVDriver) {
76 77 78
            $this->markTestSkipped('pdo_sqlsrv does not allow setting PDO::ATTR_EMULATE_PREPARES at connection level.');
        }

79 80 81 82 83 84 85 86 87 88 89 90
        // Some PDO adapters do not check the query server-side
        // even though emulated prepared statements are disabled,
        // so an exception is thrown only eventually.
        if ($driver instanceof PDOOracleDriver
            || $driver instanceof PDOPgSQLDriver
        ) {
            self::markTestSkipped(sprintf(
                'The underlying implementation of the %s driver does not check the query to be prepared server-side.',
                get_class($driver)
            ));
        }

91 92
        // Emulated prepared statements have to be disabled for this test
        // so that PDO actually communicates with the database server to check the query.
Sergei Morozov's avatar
Sergei Morozov committed
93
        $this->driverConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
94

95 96
        $this->expectException(PDOException::class);

97 98 99
        $this->driverConnection->prepare('foo');
    }

100
    public function testThrowsWrappedExceptionOnQuery() : void
101
    {
102 103
        $this->expectException(PDOException::class);

104 105
        $this->driverConnection->query('foo');
    }
106
}