22
33namespace Cspray \DatabaseTestCase ;
44
5- use Cspray \DatabaseTestCase \DatabaseRepresentation \Row ;
6- use Cspray \DatabaseTestCase \DatabaseRepresentation \Table ;
7- use Cspray \DatabaseTestCase \Exception \UnableToGetTable ;
5+ use Closure ;
86use Cspray \DatabaseTestCase \Exception \MissingRequiredExtension ;
97use PDO ;
10- use PDOException ;
118
129if (! extension_loaded ('pdo ' )) {
1310 throw new MissingRequiredExtension ('You must enable ext-pdo to use the ' . PdoConnectionAdapter::class);
@@ -17,27 +14,25 @@ final class PdoConnectionAdapter extends AbstractConnectionAdapter {
1714
1815 private ?PDO $ connection = null ;
1916
20- public function __construct (
21- private readonly ConnectionAdapterConfig $ adapterConfig ,
17+ private function __construct (
18+ private readonly Closure $ pdoSupplier ,
2219 private readonly PdoDriver $ pdoDriver
2320 ) {}
2421
22+ public static function fromConnectionConfig (ConnectionAdapterConfig $ adapterConfig , PdoDriver $ pdoDriver ) : self {
23+ return self ::fromExistingConnection (new PDO ($ pdoDriver ->dsn ($ adapterConfig )), $ pdoDriver );
24+ }
25+
26+ public static function fromExistingConnection (PDO $ pdo , PdoDriver $ pdoDriver ) : self {
27+ return new self (static fn () => $ pdo , $ pdoDriver );
28+ }
29+
2530 public function establishConnection () : void {
26- $ this ->connection = new PDO (
27- sprintf (
28- '%s:host=%s;port=%d;dbname=%s;user=%s;password=%s ' ,
29- $ this ->pdoDriver ->getDsnIdentifier (),
30- $ this ->adapterConfig ->host ,
31- $ this ->adapterConfig ->port ,
32- $ this ->adapterConfig ->database ,
33- $ this ->adapterConfig ->user ,
34- $ this ->adapterConfig ->password
35- )
36- );
31+ $ this ->connection = ($ this ->pdoSupplier )();
3732 }
3833
3934 public function onTestStart () : void {
40- $ this ->connection ->query (' START TRANSACTION ' );
35+ $ this ->connection ->query ($ this -> pdoDriver -> startTransactionSql () );
4136 }
4237
4338 public function onTestStop () : void {
0 commit comments