Skip to content

Commit fde14c5

Browse files
committed
Turn Pdo subclasses into functional polyfills
1 parent edce4e3 commit fde14c5

File tree

7 files changed

+162
-6
lines changed

7 files changed

+162
-6
lines changed

src/Php84/Resources/stubs/Pdo/Dblib.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
namespace Pdo;
1212

1313
use PDO;
14+
use PDOException;
1415

1516
if (\PHP_VERSION_ID < 80400) {
16-
class Dblib
17+
class Dblib extends PDO
1718
{
1819
public const ATTR_CONNECTION_TIMEOUT = PDO::DBLIB_ATTR_CONNECTION_TIMEOUT;
1920
public const ATTR_QUERY_TIMEOUT = PDO::DBLIB_ATTR_QUERY_TIMEOUT;
@@ -22,5 +23,15 @@ class Dblib
2223
public const ATTR_TDS_VERSION = \PHP_VERSION_ID >= 70300 ? PDO::DBLIB_ATTR_TDS_VERSION : 1004;
2324
public const ATTR_SKIP_EMPTY_ROWSETS = \PHP_VERSION_ID >= 70300 ? PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS : 1005;
2425
public const ATTR_DATETIME_CONVERT = \PHP_VERSION_ID >= 70300 ? \PDO::DBLIB_ATTR_DATETIME_CONVERT : 1006;
26+
27+
public function __construct()
28+
{
29+
parent::__construct();
30+
31+
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'dblib')
32+
{
33+
throw new PDOException("Pdo\Dblib::__construct() cannot be used for connecting to the \"$driver\" driver");
34+
}
35+
}
2536
}
2637
}

src/Php84/Resources/stubs/Pdo/Firebird.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,28 @@
1111
namespace Pdo;
1212

1313
use PDO;
14+
use PDOException;
1415

1516
if (\PHP_VERSION_ID < 80400) {
16-
class Firebird
17+
class Firebird extends PDO
1718
{
1819
public const ATTR_DATE_FORMAT = PDO::FB_ATTR_DATE_FORMAT;
1920
public const ATTR_TIME_FORMAT = PDO::FB_ATTR_TIME_FORMAT;
2021
public const ATTR_TIMESTAMP_FORMAT = PDO::FB_ATTR_TIMESTAMP_FORMAT;
22+
23+
public function __construct(
24+
string $dsn,
25+
?string $username = null,
26+
?string $password = null,
27+
?array $options = null
28+
)
29+
{
30+
parent::__construct($dsn, $username, $password, $options);
31+
32+
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'firebird')
33+
{
34+
throw new PDOException("Pdo\Firebird::__construct() cannot be used for connecting to the \"$driver\" driver");
35+
}
36+
}
2137
}
2238
}

src/Php84/Resources/stubs/Pdo/Mysql.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
namespace Pdo;
1212

1313
use PDO;
14+
use PDOException;
1415

1516
if (\PHP_VERSION_ID < 80400) {
16-
class Mysql
17+
class Mysql extends PDO
1718
{
1819
public const ATTR_COMPRESS = PDO::MYSQL_ATTR_COMPRESS;
1920
public const ATTR_DIRECT_QUERY = PDO::MYSQL_ATTR_DIRECT_QUERY;
@@ -34,5 +35,20 @@ class Mysql
3435
public const ATTR_SSL_KEY = PDO::MYSQL_ATTR_SSL_KEY;
3536
public const ATTR_SSL_VERIFY_SERVER_CERT = PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT;
3637
public const ATTR_USE_BUFFERED_QUERY = PDO::MYSQL_ATTR_USE_BUFFERED_QUERY;
38+
39+
public function __construct(
40+
string $dsn,
41+
?string $username = null,
42+
?string $password = null,
43+
?array $options = null
44+
)
45+
{
46+
parent::__construct($dsn, $username, $password, $options);
47+
48+
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'mysql')
49+
{
50+
throw new PDOException("Pdo\Mysql::__construct() cannot be used for connecting to the \"$driver\" driver");
51+
}
52+
}
3753
}
3854
}

src/Php84/Resources/stubs/Pdo/Odbc.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,30 @@
1111
namespace Pdo;
1212

1313
use PDO;
14+
use PDOException;
1415

1516
if (\PHP_VERSION_ID < 80400) {
16-
class Odbc
17+
class Odbc extends PDO
1718
{
1819
public const ATTR_USE_CURSOR_LIBRARY = PDO::ODBC_ATTR_USE_CURSOR_LIBRARY;
1920
public const ATTR_ASSUME_UTF8 = PDO::ODBC_ATTR_ASSUME_UTF8;
2021
public const SQL_USE_IF_NEEDED = PDO::ODBC_SQL_USE_IF_NEEDED;
2122
public const SQL_USE_DRIVER = PDO::ODBC_SQL_USE_DRIVER;
2223
public const SQL_USE_ODBC = PDO::ODBC_SQL_USE_ODBC;
24+
25+
public function __construct(
26+
string $dsn,
27+
?string $username = null,
28+
?string $password = null,
29+
?array $options = null
30+
)
31+
{
32+
parent::__construct($dsn, $username, $password, $options);
33+
34+
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'odbc')
35+
{
36+
throw new PDOException("Pdo\Odbc::__construct() cannot be used for connecting to the \"$driver\" driver");
37+
}
38+
}
2339
}
2440
}

src/Php84/Resources/stubs/Pdo/Pgsql.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,26 @@
1111
namespace Pdo;
1212

1313
use PDO;
14+
use PDOException;
1415

1516
if (\PHP_VERSION_ID < 80400) {
16-
class Pgsql
17+
class Pgsql extends PDO
1718
{
1819
public const ATTR_DISABLE_PREPARES = PDO::PGSQL_ATTR_DISABLE_PREPARES;
20+
21+
public function __construct(
22+
string $dsn,
23+
?string $username = null,
24+
?string $password = null,
25+
?array $options = null
26+
)
27+
{
28+
parent::__construct($dsn, $username, $password, $options);
29+
30+
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'pgsql')
31+
{
32+
throw new PDOException("Pdo\Pgsql::__construct() cannot be used for connecting to the \"$driver\" driver");
33+
}
34+
}
1935
}
2036
}

src/Php84/Resources/stubs/Pdo/Sqlite.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
namespace Pdo;
1212

1313
use PDO;
14+
use PDOException;
1415

1516
if (\PHP_VERSION_ID < 80400) {
16-
class Sqlite
17+
class Sqlite extends PDO
1718
{
1819
public const ATTR_EXTENDED_RESULT_CODES = \PHP_VERSION_ID >= 70400 ? \PDO::SQLITE_ATTR_EXTENDED_RESULT_CODES : 1002;
1920
public const ATTR_OPEN_FLAGS = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_ATTR_OPEN_FLAGS : 1000;
@@ -22,5 +23,20 @@ class Sqlite
2223
public const OPEN_READONLY = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_READONLY : 1;
2324
public const OPEN_READWRITE = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_READWRITE : 2;
2425
public const OPEN_CREATE = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_CREATE : 4;
26+
27+
public function __construct(
28+
string $dsn,
29+
?string $username = null,
30+
?string $password = null,
31+
?array $options = null
32+
)
33+
{
34+
parent::__construct($dsn, $username, $password, $options);
35+
36+
if (($driver = $this->getAttribute(PDO::ATTR_DRIVER_NAME)) !== 'sqlite')
37+
{
38+
throw new PDOException("Pdo\Sqlite::__construct() cannot be used for connecting to the \"$driver\" driver");
39+
}
40+
}
2541
}
2642
}

tests/Php84/PdoTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,75 @@
1111

1212
namespace Symfony\Polyfill\Tests\Php84;
1313

14+
use PDOException;
1415
use PHPUnit\Framework\TestCase;
1516

1617
class PdoTest extends TestCase
1718
{
19+
/**
20+
* @requires extension pdo_dblib
21+
* @requires extension pdo_sqlite
22+
*/
23+
public function testDblibConstructor()
24+
{
25+
$this->expectException(PDOException::class);
26+
$this->expectExceptionMessage("Pdo\Dblib::__construct() cannot be used for connecting to the \"sqlite\" driver");
27+
new \Pdo\Dblib("sqlite:");
28+
}
29+
30+
/**
31+
* @requires extension pdo_firebird
32+
* @requires extension pdo_sqlite
33+
*/
34+
public function testFirebirdConstructor()
35+
{
36+
$this->expectException(PDOException::class);
37+
$this->expectExceptionMessage("Pdo\Firebird::__construct() cannot be used for connecting to the \"sqlite\" driver");
38+
new \Pdo\Dblib("sqlite:");
39+
}
40+
41+
/**
42+
* @requires extension pdo_mysql
43+
* @requires extension pdo_sqlite
44+
*/
45+
public function testMysqlConstructor()
46+
{
47+
$this->expectException(PDOException::class);
48+
$this->expectExceptionMessage("Pdo\Mysql::__construct() cannot be used for connecting to the \"sqlite\" driver");
49+
new \Pdo\Mysql("sqlite:");
50+
}
51+
52+
/**
53+
* @requires extension pdo_odbc
54+
* @requires extension pdo_sqlite
55+
*/
56+
public function testOdbcConstructor()
57+
{
58+
$this->expectException(PDOException::class);
59+
$this->expectExceptionMessage("Pdo\Odbc::__construct() cannot be used for connecting to the \"sqlite\" driver");
60+
new \Pdo\Odbc("sqlite:");
61+
}
62+
63+
/**
64+
* @requires extension pdo_pgsql
65+
* @requires extension pdo_sqlite
66+
*/
67+
public function testPgsqlConstructor()
68+
{
69+
$this->expectException(PDOException::class);
70+
$this->expectExceptionMessage("Pdo\Pgsql::__construct() cannot be used for connecting to the \"sqlite\" driver");
71+
new \Pdo\Pgsql("sqlite:");
72+
}
73+
74+
/**
75+
* @requires extension pdo_sqlite
76+
*/
77+
public function testSqliteConstructor()
78+
{
79+
$sqlite = new \Pdo\Sqlite("sqlite:");
80+
$this->assertInstanceOf(\Pdo\Sqlite::class, $sqlite);
81+
}
82+
1883
/**
1984
* @requires extension pdo_dblib
2085
*/

0 commit comments

Comments
 (0)