44
55use Amp \Postgres \PostgresConfig ;
66use Amp \Postgres \PostgresLink ;
7+ use Closure ;
78use Cspray \DatabaseTestCase \Exception \MissingRequiredComposerPackage ;
89use function Amp \Postgres \connect ;
910
@@ -15,21 +16,29 @@ class AmpPostgresConnectionAdapter extends AbstractConnectionAdapter {
1516
1617 private ?PostgresLink $ connection = null ;
1718
18- public function __construct (
19- private readonly ConnectionAdapterConfig $ adapterConfig
19+ private function __construct (
20+ private readonly Closure $ connectionFactory
2021 ) {}
2122
22- public function establishConnection ( ) : void {
23- $ this -> connection = connect (
23+ public static function newConnectionFromConfig ( ConnectionAdapterConfig $ config ) : self {
24+ return new self ( fn () => connect (
2425 PostgresConfig::fromString (sprintf (
2526 'db=%s host=%s port=%d user=%s pass=%s ' ,
26- $ this -> adapterConfig ->database ,
27- $ this -> adapterConfig ->host ,
28- $ this -> adapterConfig ->port ,
29- $ this -> adapterConfig ->user ,
30- $ this -> adapterConfig ->password
27+ $ config ->database ,
28+ $ config ->host ,
29+ $ config ->port ,
30+ $ config ->user ,
31+ $ config ->password
3132 ))
32- );
33+ ));
34+ }
35+
36+ public static function existingConnection (PostgresLink $ link ) : self {
37+ return new self (fn () => $ link );
38+ }
39+
40+ public function establishConnection () : void {
41+ $ this ->connection = ($ this ->connectionFactory )();
3342 }
3443
3544 public function onTestStart () : void {
0 commit comments