Skip to content

Commit 9ae4ea4

Browse files
authored
Allow amphp/postgres to provide an existing connection (#16)
1 parent 747dc17 commit 9ae4ea4

File tree

4 files changed

+37
-37
lines changed

4 files changed

+37
-37
lines changed

phpunit.xml

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4-
bootstrap="vendor/autoload.php"
5-
cacheDirectory=".phpunit.cache"
6-
executionOrder="depends,defects"
7-
requireCoverageMetadata="true"
8-
beStrictAboutCoverageMetadata="true"
9-
beStrictAboutOutputDuringTests="true"
10-
failOnRisky="true"
11-
failOnWarning="true">
12-
<testsuites>
13-
<testsuite name="unit">
14-
<directory>tests/Unit</directory>
15-
</testsuite>
16-
<testsuite name="integration">
17-
<directory>tests/Integration</directory>
18-
</testsuite>
19-
</testsuites>
20-
21-
<coverage>
22-
<include>
23-
<directory suffix=".php">src</directory>
24-
</include>
25-
</coverage>
2+
<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">
3+
<testsuites>
4+
<testsuite name="unit">
5+
<directory>tests/Unit</directory>
6+
</testsuite>
7+
<testsuite name="integration">
8+
<directory>tests/Integration</directory>
9+
</testsuite>
10+
</testsuites>
11+
<coverage/>
12+
<source>
13+
<include>
14+
<directory suffix=".php">src</directory>
15+
</include>
16+
</source>
2617
</phpunit>

src/AmpPostgresConnectionAdapter.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Amp\Postgres\PostgresConfig;
66
use Amp\Postgres\PostgresLink;
7+
use Closure;
78
use Cspray\DatabaseTestCase\Exception\MissingRequiredComposerPackage;
89
use 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 {

tests/Integration/AmpPostgresConnectionAdapterTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ protected function executeCountSql(string $table) : int {
2222
}
2323

2424
protected static function getConnectionAdapter() : ConnectionAdapter {
25-
return new AmpPostgresConnectionAdapter(
26-
new PostgresConnectionConfig()
27-
);
25+
return AmpPostgresConnectionAdapter::newConnectionFromConfig(new PostgresConnectionConfig());
2826
}
2927
}

tests/Unit/Helper/StubDatabaseTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Cspray\DatabaseTestCase\DatabaseTestCase;
77
use Cspray\DatabaseTestCase\LoadFixture;
88
use Cspray\DatabaseTestCase\SingleRecordFixture;
9+
use PHPUnit\Framework\Attributes\CoversClass;
910

11+
#[CoversClass(DatabaseTestCase::class)]
1012
class StubDatabaseTestCase extends DatabaseTestCase {
1113

1214
private static ConnectionAdapter $connectionAdapter;

0 commit comments

Comments
 (0)