1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-11 08:59:42 +01:00
postgres/test/AbstractConnectTest.php

75 lines
2.3 KiB
PHP
Raw Normal View History

2017-06-15 05:46:25 +02:00
<?php
namespace Amp\Postgres\Test;
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;
use Amp\Promise;
2018-07-01 19:33:12 +02:00
use Amp\Sql\ConnectionConfig;
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(
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();
$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();
$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 () {
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
});
}
}