2017-06-15 05:46:25 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Postgres\Test;
|
|
|
|
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\CancellationToken;
|
|
|
|
use Amp\CancellationTokenSource;
|
|
|
|
use Amp\Loop;
|
2017-06-15 05:46:25 +02:00
|
|
|
use Amp\Postgres\Connection;
|
2018-07-01 19:33:12 +02:00
|
|
|
use Amp\Postgres\ConnectionConfig as PostgresConnectionConfig;
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Promise;
|
2018-07-01 19:33:12 +02:00
|
|
|
use Amp\Sql\ConnectionConfig;
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\TimeoutCancellationToken;
|
2017-06-15 05:46:25 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
2018-07-01 19:33:12 +02:00
|
|
|
abstract class AbstractConnectTest extends TestCase
|
|
|
|
{
|
2017-06-15 05:46:25 +02:00
|
|
|
/**
|
2018-07-01 19:33:12 +02:00
|
|
|
* @param ConnectionConfig $connectionConfig
|
|
|
|
* @param CancellationToken|null $token
|
2017-06-15 05:46:25 +02:00
|
|
|
*
|
2018-07-01 19:33:12 +02:00
|
|
|
* @return Promise
|
2017-06-15 05:46:25 +02:00
|
|
|
*/
|
2018-07-01 19:33:12 +02:00
|
|
|
abstract public function connect(ConnectionConfig $connectionConfig, CancellationToken $token = null): Promise;
|
2017-06-15 05:46:25 +02:00
|
|
|
|
2018-07-01 19:33:12 +02:00
|
|
|
public function testConnect()
|
|
|
|
{
|
2017-06-15 05:46:25 +02:00
|
|
|
Loop::run(function () {
|
2018-07-01 19:33:12 +02:00
|
|
|
$connection = yield $this->connect(
|
2018-07-17 18:20:07 +02:00
|
|
|
PostgresConnectionConfig::fromString('host=localhost user=postgres'),
|
2018-07-01 19:33:12 +02:00
|
|
|
new TimeoutCancellationToken(100)
|
|
|
|
);
|
2017-06-15 05:46:25 +02:00
|
|
|
$this->assertInstanceOf(Connection::class, $connection);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testConnect
|
|
|
|
* @expectedException \Amp\CancelledException
|
|
|
|
*/
|
2018-07-01 19:33:12 +02:00
|
|
|
public function testConnectCancellationBeforeConnect()
|
|
|
|
{
|
2017-06-15 05:46:25 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$source = new CancellationTokenSource;
|
|
|
|
$token = $source->getToken();
|
|
|
|
$source->cancel();
|
2018-07-17 18:20:07 +02:00
|
|
|
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
|
2017-06-15 05:46:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testConnectCancellationBeforeConnect
|
|
|
|
*/
|
2018-07-01 19:33:12 +02:00
|
|
|
public function testConnectCancellationAfterConnect()
|
|
|
|
{
|
2017-06-15 05:46:25 +02:00
|
|
|
Loop::run(function () {
|
|
|
|
$source = new CancellationTokenSource;
|
|
|
|
$token = $source->getToken();
|
2018-07-17 18:20:07 +02:00
|
|
|
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
|
2017-06-15 05:46:25 +02:00
|
|
|
$this->assertInstanceOf(Connection::class, $connection);
|
|
|
|
$source->cancel();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testConnectCancellationBeforeConnect
|
2018-07-01 19:33:12 +02:00
|
|
|
* @expectedException \Amp\Sql\FailureException
|
2017-06-15 05:46:25 +02:00
|
|
|
*/
|
2018-07-01 19:33:12 +02:00
|
|
|
public function testConnectInvalidUser()
|
|
|
|
{
|
2017-06-15 05:46:25 +02:00
|
|
|
Loop::run(function () {
|
2018-07-17 18:20:07 +02:00
|
|
|
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
|
2017-06-15 05:46:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|