diff --git a/phpunit.xml b/phpunit.xml index 5d9ff9d..fbd9f20 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,26 +1,17 @@ - - - - tests/Unit - - - tests/Integration - - - - - - src - - + + + + tests/Unit + + + tests/Integration + + + + + + src + + diff --git a/src/AmpPostgresConnectionAdapter.php b/src/AmpPostgresConnectionAdapter.php index 4f0bb6c..c37b39e 100644 --- a/src/AmpPostgresConnectionAdapter.php +++ b/src/AmpPostgresConnectionAdapter.php @@ -4,6 +4,7 @@ use Amp\Postgres\PostgresConfig; use Amp\Postgres\PostgresLink; +use Closure; use Cspray\DatabaseTestCase\Exception\MissingRequiredComposerPackage; use function Amp\Postgres\connect; @@ -15,21 +16,29 @@ class AmpPostgresConnectionAdapter extends AbstractConnectionAdapter { private ?PostgresLink $connection = null; - public function __construct( - private readonly ConnectionAdapterConfig $adapterConfig + private function __construct( + private readonly Closure $connectionFactory ) {} - public function establishConnection() : void { - $this->connection = connect( + public static function newConnectionFromConfig(ConnectionAdapterConfig $config) : self { + return new self(fn() => connect( PostgresConfig::fromString(sprintf( 'db=%s host=%s port=%d user=%s pass=%s', - $this->adapterConfig->database, - $this->adapterConfig->host, - $this->adapterConfig->port, - $this->adapterConfig->user, - $this->adapterConfig->password + $config->database, + $config->host, + $config->port, + $config->user, + $config->password )) - ); + )); + } + + public static function existingConnection(PostgresLink $link) : self { + return new self(fn() => $link); + } + + public function establishConnection() : void { + $this->connection = ($this->connectionFactory)(); } public function onTestStart() : void { diff --git a/tests/Integration/AmpPostgresConnectionAdapterTest.php b/tests/Integration/AmpPostgresConnectionAdapterTest.php index 203bd21..2adf338 100644 --- a/tests/Integration/AmpPostgresConnectionAdapterTest.php +++ b/tests/Integration/AmpPostgresConnectionAdapterTest.php @@ -22,8 +22,6 @@ protected function executeCountSql(string $table) : int { } protected static function getConnectionAdapter() : ConnectionAdapter { - return new AmpPostgresConnectionAdapter( - new PostgresConnectionConfig() - ); + return AmpPostgresConnectionAdapter::newConnectionFromConfig(new PostgresConnectionConfig()); } } \ No newline at end of file diff --git a/tests/Unit/Helper/StubDatabaseTestCase.php b/tests/Unit/Helper/StubDatabaseTestCase.php index 040cac8..68fed56 100644 --- a/tests/Unit/Helper/StubDatabaseTestCase.php +++ b/tests/Unit/Helper/StubDatabaseTestCase.php @@ -6,7 +6,9 @@ use Cspray\DatabaseTestCase\DatabaseTestCase; use Cspray\DatabaseTestCase\LoadFixture; use Cspray\DatabaseTestCase\SingleRecordFixture; +use PHPUnit\Framework\Attributes\CoversClass; +#[CoversClass(DatabaseTestCase::class)] class StubDatabaseTestCase extends DatabaseTestCase { private static ConnectionAdapter $connectionAdapter;