Skip to content

Commit

Permalink
Allow amphp/postgres to provide an existing connection (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspray authored May 24, 2023
1 parent 747dc17 commit 9ae4ea4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 37 deletions.
39 changes: 15 additions & 24 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache" executionOrder="depends,defects" requireCoverageMetadata="true" beStrictAboutCoverageMetadata="true" beStrictAboutOutputDuringTests="true" failOnRisky="true" failOnWarning="true">
<testsuites>
<testsuite name="unit">
<directory>tests/Unit</directory>
</testsuite>
<testsuite name="integration">
<directory>tests/Integration</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
29 changes: 19 additions & 10 deletions src/AmpPostgresConnectionAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Amp\Postgres\PostgresConfig;
use Amp\Postgres\PostgresLink;
use Closure;
use Cspray\DatabaseTestCase\Exception\MissingRequiredComposerPackage;
use function Amp\Postgres\connect;

Expand All @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions tests/Integration/AmpPostgresConnectionAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Helper/StubDatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9ae4ea4

Please sign in to comment.