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

85 lines
2.6 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;
use Amp\Promise;
use Amp\TimeoutCancellationToken;
2017-06-15 05:46:25 +02:00
use PHPUnit\Framework\TestCase;
abstract class AbstractConnectTest extends TestCase {
/**
* @param string $connectionString
* @param \Amp\CancellationToken|null $token
*
* @return \Amp\Promise
*/
abstract public function connect(string $connectionString, CancellationToken $token = null): Promise;
public function testConnect() {
Loop::run(function () {
$connection = yield $this->connect('host=localhost user=postgres', new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
$this->assertInstanceOf(Connection::class, $connection);
});
}
/**
* @depends testConnect
* @expectedException \Amp\CancelledException
*/
public function testConnectCancellationBeforeConnect() {
Loop::run(function () {
$source = new CancellationTokenSource;
$token = $source->getToken();
$source->cancel();
$connection = yield $this->connect('host=localhost user=postgres', $token);
});
}
/**
* @depends testConnectCancellationBeforeConnect
*/
public function testConnectCancellationAfterConnect() {
Loop::run(function () {
$source = new CancellationTokenSource;
$token = $source->getToken();
$connection = yield $this->connect('host=localhost user=postgres', $token);
$this->assertInstanceOf(Connection::class, $connection);
$source->cancel();
});
}
/**
* @depends testConnectCancellationBeforeConnect
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidUser() {
Loop::run(function () {
$connection = yield $this->connect('host=localhost user=invalid', new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
});
}
/**
* @depends testConnectCancellationBeforeConnect
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidConnectionString() {
Loop::run(function () {
$connection = yield $this->connect('invalid connection string', new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
});
}
/**
* @depends testConnectCancellationBeforeConnect
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidHost() {
Loop::run(function () {
$connection = yield $this->connect('hostaddr=invalid.host user=postgres', new TimeoutCancellationToken(100));
2017-06-15 05:46:25 +02:00
});
}
}