Completed
Pull Request — master (#3807)
by Sergei
155:06 queued 90:16
created

()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Driver;
6
7
use Doctrine\DBAL\Driver\PDOConnection;
8
use Doctrine\DBAL\Driver\PDOException;
9
use Doctrine\DBAL\Driver\PDOOracle\Driver as PDOOracleDriver;
10
use Doctrine\DBAL\Driver\PDOPgSql\Driver as PDOPgSQLDriver;
11
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSQLSRVDriver;
12
use Doctrine\Tests\DbalFunctionalTestCase;
13
use PDO;
14
use function get_class;
15
use function sprintf;
16
17
/**
18
 * @requires extension pdo
19
 */
20
class PDOConnectionTest extends DbalFunctionalTestCase
21
{
22
    /**
23
     * The PDO driver connection under test.
24
     *
25
     * @var PDOConnection
26
     */
27
    protected $driverConnection;
28
29
    protected function setUp() : void
30
    {
31
        parent::setUp();
32
33
        $wrappedConnection = $this->connection->getWrappedConnection();
34
35
        if (! $wrappedConnection instanceof PDOConnection) {
36
            $this->markTestSkipped('PDO connection only test.');
37
        }
38
39
        $this->driverConnection = $wrappedConnection;
40
    }
41
42
    protected function tearDown() : void
43
    {
44
        $this->resetSharedConn();
45
46
        parent::tearDown();
47
    }
48
49
    public function testThrowsWrappedExceptionOnConstruct() : void
50
    {
51
        $this->expectException(PDOException::class);
52
53
        new PDOConnection('foo');
54
    }
55
56
    /**
57
     * @group DBAL-1022
58
     */
59
    public function testThrowsWrappedExceptionOnExec() : void
60
    {
61
        $this->expectException(PDOException::class);
62
63
        $this->driverConnection->exec('foo');
64
    }
65
66
    public function testThrowsWrappedExceptionOnPrepare() : void
67
    {
68
        $driver = $this->connection->getDriver();
69
70
        if ($driver instanceof PDOSQLSRVDriver) {
71
            $this->markTestSkipped('pdo_sqlsrv does not allow setting PDO::ATTR_EMULATE_PREPARES at connection level.');
72
        }
73
74
        // Some PDO adapters do not check the query server-side
75
        // even though emulated prepared statements are disabled,
76
        // so an exception is thrown only eventually.
77
        if ($driver instanceof PDOOracleDriver
78
            || $driver instanceof PDOPgSQLDriver
79
        ) {
80
            self::markTestSkipped(sprintf(
81
                'The underlying implementation of the %s driver does not check the query to be prepared server-side.',
82
                get_class($driver)
83
            ));
84
        }
85
86
        // Emulated prepared statements have to be disabled for this test
87
        // so that PDO actually communicates with the database server to check the query.
88
        $this->driverConnection
89
            ->getWrappedConnection()
90
            ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
91
92
        $this->expectException(PDOException::class);
93
94
        $this->driverConnection->prepare('foo');
95
    }
96
97
    public function testThrowsWrappedExceptionOnQuery() : void
98
    {
99
        $this->expectException(PDOException::class);
100
101
        $this->driverConnection->query('foo');
102
    }
103
}
104