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

69 lines
2.0 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\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
*
2020-10-11 18:49:15 +02:00
* @return Connection
2017-06-15 05:46:25 +02:00
*/
2020-10-11 18:49:15 +02:00
abstract public function connect(ConnectionConfig $connectionConfig, CancellationToken $token = null): Connection;
2017-06-15 05:46:25 +02:00
2020-10-11 18:49:15 +02:00
public function testConnect()
2018-07-01 19:33:12 +02:00
{
2020-10-11 18:49:15 +02:00
$connection = $this->connect(
2019-09-27 05:41:47 +02:00
PostgresConnectionConfig::fromString('host=localhost user=postgres'),
new TimeoutCancellationToken(100)
);
$this->assertInstanceOf(Connection::class, $connection);
2017-06-15 05:46:25 +02:00
}
/**
* @depends testConnect
*/
2020-10-11 18:49:15 +02:00
public function testConnectCancellationBeforeConnect()
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();
2020-10-11 18:49:15 +02:00
$this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
2017-06-15 05:46:25 +02:00
}
/**
* @depends testConnectCancellationBeforeConnect
*/
2020-10-11 18:49:15 +02:00
public function testConnectCancellationAfterConnect()
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$source = new CancellationTokenSource;
$token = $source->getToken();
2020-10-11 18:49:15 +02:00
$connection = $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'), $token);
2019-09-27 05:41:47 +02:00
$this->assertInstanceOf(Connection::class, $connection);
$source->cancel();
2017-06-15 05:46:25 +02:00
}
/**
* @depends testConnectCancellationBeforeConnect
*/
2020-10-11 18:49:15 +02:00
public function testConnectInvalidUser()
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(FailureException::class);
2020-10-11 18:49:15 +02:00
$this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
}
}