Skip to content

Commit 8034458

Browse files
committed
DRAFT: add static connect method
1 parent 15a7a4b commit 8034458

File tree

7 files changed

+99
-0
lines changed

7 files changed

+99
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
3333
throw new PDOException("Pdo\Dblib::__construct() cannot be used for connecting to the \"$driver\" driver");
3434
}
3535
}
36+
37+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
38+
{
39+
return new static($dsn, $username, $password, $options);
40+
}
3641
}
3742
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
2929
throw new PDOException("Pdo\Firebird::__construct() cannot be used for connecting to the \"$driver\" driver");
3030
}
3131
}
32+
33+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
34+
{
35+
return new static($dsn, $username, $password, $options);
36+
}
3237
}
3338
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
4848
throw new PDOException("Pdo\Mysql::__construct() cannot be used for connecting to the \"$driver\" driver");
4949
}
5050
}
51+
52+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
53+
{
54+
return new static($dsn, $username, $password, $options);
55+
}
5156
}
5257
} else {
5358
class Mysql extends PDO
@@ -81,6 +86,11 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
8186
throw new PDOException("Pdo\Mysql::__construct() cannot be used for connecting to the \"$driver\" driver");
8287
}
8388
}
89+
90+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
91+
{
92+
return new static($dsn, $username, $password, $options);
93+
}
8494
}
8595
}
8696
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
3131
throw new PDOException("Pdo\Odbc::__construct() cannot be used for connecting to the \"$driver\" driver");
3232
}
3333
}
34+
35+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
36+
{
37+
return new static($dsn, $username, $password, $options);
38+
}
3439
}
3540
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
2828
}
2929
}
3030

31+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
32+
{
33+
return new static($dsn, $username, $password, $options);
34+
}
35+
3136
public function copyFromArray(string $tableName, array $rows, string $separator = "\t", string $nullAs = "\\\\N", ?string $fields = null): bool
3237
{
3338
return $this->pgsqlCopyFromArray($tableName, $rows, $separator, $nullAs, $fields);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function __construct(string $dsn, ?string $username = null, ?string $pass
3434
}
3535
}
3636

37+
public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): static
38+
{
39+
return new static($dsn, $username, $password, $options);
40+
}
41+
3742
public function createAggregate(string $name, callable $step, callable $finalize, int $numArgs = -1): bool
3843
{
3944
return $this->sqliteCreateAggregate($name, $step, $finalize, $numArgs);

tests/Php84/PdoTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,70 @@ public function testSqliteConstructor()
8080
$this->assertInstanceOf(\Pdo\Sqlite::class, $sqlite);
8181
}
8282

83+
/**
84+
* @requires extension pdo_dblib
85+
* @requires extension pdo_sqlite
86+
*/
87+
public function testDblibConnect()
88+
{
89+
$this->expectException(PDOException::class);
90+
$this->expectExceptionMessage("Pdo\Dblib::__construct() cannot be used for connecting to the \"sqlite\" driver");
91+
\Pdo\Dblib::connect("sqlite:");
92+
}
93+
94+
/**
95+
* @requires extension pdo_firebird
96+
* @requires extension pdo_sqlite
97+
*/
98+
public function testFirebirdConnect()
99+
{
100+
$this->expectException(PDOException::class);
101+
$this->expectExceptionMessage("Pdo\Firebird::__construct() cannot be used for connecting to the \"sqlite\" driver");
102+
\Pdo\Firebird::connect("sqlite:");
103+
}
104+
105+
/**
106+
* @requires extension pdo_mysql
107+
* @requires extension pdo_sqlite
108+
*/
109+
public function testMysqlConnect()
110+
{
111+
$this->expectException(PDOException::class);
112+
$this->expectExceptionMessage("Pdo\Mysql::__construct() cannot be used for connecting to the \"sqlite\" driver");
113+
\Pdo\Mysql::connect("sqlite:");
114+
}
115+
116+
/**
117+
* @requires extension pdo_odbc
118+
* @requires extension pdo_sqlite
119+
*/
120+
public function testOdbcConnect()
121+
{
122+
$this->expectException(PDOException::class);
123+
$this->expectExceptionMessage("Pdo\Odbc::__construct() cannot be used for connecting to the \"sqlite\" driver");
124+
\Pdo\Odbc::connect("sqlite:");
125+
}
126+
127+
/**
128+
* @requires extension pdo_pgsql
129+
* @requires extension pdo_sqlite
130+
*/
131+
public function testPgsqlConnect()
132+
{
133+
$this->expectException(PDOException::class);
134+
$this->expectExceptionMessage("Pdo\Pgsql::__construct() cannot be used for connecting to the \"sqlite\" driver");
135+
\Pdo\Pgsql::connect("sqlite:");
136+
}
137+
138+
/**
139+
* @requires extension pdo_sqlite
140+
*/
141+
public function testSqliteConnect()
142+
{
143+
$sqlite = \Pdo\Sqlite::connect("sqlite:");
144+
$this->assertInstanceOf(\Pdo\Sqlite::class, $sqlite);
145+
}
146+
83147
/**
84148
* @requires extension pdo_dblib
85149
*/

0 commit comments

Comments
 (0)