1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-03 09:57:48 +01:00
postgres/test/AbstractConnectTest.php

70 lines
2.1 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;
2019-09-27 05:41:47 +02:00
use Amp\CancelledException;
use Amp\PHPUnit\AsyncTestCase;
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;
2019-09-27 05:41:47 +02:00
use Amp\Sql\FailureException;
use Amp\TimeoutCancellationToken;
2017-06-15 05:46:25 +02:00
2019-09-27 05:41:47 +02:00
abstract class AbstractConnectTest extends AsyncTestCase
2018-07-01 19:33:12 +02:00
{
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
2019-09-27 05:41:47 +02:00
public function testConnect(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$connection = yield $this->connect(
PostgresConnectionConfig::fromString('host=localhost user=postgres'),
new TimeoutCancellationToken(100)
);
$this->assertInstanceOf(Connection::class, $connection);
2017-06-15 05:46:25 +02:00
}
/**
* @depends testConnect
*/
2019-09-27 05:41:47 +02:00
public function testConnectCancellationBeforeConnect(): Promise
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(CancelledException::class);
$source = new CancellationTokenSource;
$token = $source->getToken();
$source->cancel();
return $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
2017-06-15 05:46:25 +02:00
}
/**
* @depends testConnectCancellationBeforeConnect
*/
2019-09-27 05:41:47 +02:00
public function testConnectCancellationAfterConnect(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$source = new CancellationTokenSource;
$token = $source->getToken();
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
$this->assertInstanceOf(Connection::class, $connection);
$source->cancel();
2017-06-15 05:46:25 +02:00
}
/**
* @depends testConnectCancellationBeforeConnect
*/
2019-09-27 05:41:47 +02:00
public function testConnectInvalidUser(): Promise
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(FailureException::class);
return $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
}
}