1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-26 20:15:02 +01:00

Separate connect tests

This commit is contained in:
Aaron Piotrowski 2017-06-14 22:46:25 -05:00
parent fe659ea5e6
commit b9daaed1d9
6 changed files with 116 additions and 76 deletions

View File

@ -0,0 +1,80 @@
<?php
namespace Amp\Postgres\Test;
use Amp\{ CancellationToken, CancellationTokenSource, Loop, Promise };
use Amp\Postgres\Connection;
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');
$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');
});
}
/**
* @depends testConnectCancellationBeforeConnect
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidConnectionString() {
Loop::run(function () {
$connection = yield $this->connect('invalid connection string');
});
}
/**
* @depends testConnectCancellationBeforeConnect
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidHost() {
Loop::run(function () {
$connection = yield $this->connect('hostaddr=invalid.host user=postgres');
});
}
}

View File

@ -2,11 +2,8 @@
namespace Amp\Postgres\Test;
use Amp\CancellationTokenSource;
use Amp\Loop;
use Amp\Postgres\{
CommandResult, Connection, Listener, Transaction, TransactionError, TupleResult
};
use Amp\Postgres\{ CommandResult, Connection, Listener, Transaction, TransactionError, TupleResult };
use PHPUnit\Framework\TestCase;
abstract class AbstractConnectionTest extends TestCase {
@ -25,10 +22,13 @@ abstract class AbstractConnectionTest extends TestCase {
];
}
/**
* @param string $connectionString
*
* @return \Amp\Postgres\Connection Connection object to be tested.
*/
abstract public function createConnection(string $connectionString): Connection;
abstract public function getConnectCallable(): callable;
public function setUp() {
$this->connection = $this->createConnection('host=localhost user=postgres');
}
@ -237,66 +237,4 @@ abstract class AbstractConnectionTest extends TestCase {
$listener = yield $this->connection->listen($channel);
});
}
public function testConnect() {
Loop::run(function () {
$connect = $this->getConnectCallable();
$connection = yield $connect('host=localhost user=postgres');
$this->assertInstanceOf(Connection::class, $connection);
});
}
/**
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidUser() {
Loop::run(function () {
$connect = $this->getConnectCallable();
$connection = yield $connect('host=localhost user=invalid');
});
}
/**
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidConnectionString() {
Loop::run(function () {
$connect = $this->getConnectCallable();
$connection = yield $connect('invalid connection string');
});
}
/**
* @expectedException \Amp\Postgres\FailureException
*/
public function testConnectInvalidHost() {
Loop::run(function () {
$connect = $this->getConnectCallable();
$connection = yield $connect('hostaddr=invalid.host user=postgres');
});
}
/**
* @expectedException \Amp\CancelledException
*/
public function testConnectCancellationBeforeConnect() {
Loop::run(function () {
$connect = $this->getConnectCallable();
$source = new CancellationTokenSource;
$token = $source->getToken();
$source->cancel();
$connection = yield $connect('host=localhost user=postgres', $token);
});
}
public function testConnectCancellationAfterConnect() {
Loop::run(function () {
$connect = $this->getConnectCallable();
$source = new CancellationTokenSource;
$token = $source->getToken();
$connection = yield $connect('host=localhost user=postgres', $token);
$this->assertInstanceOf(Connection::class, $connection);
$source->cancel();
});
}
}

15
test/PgSqlConnectTest.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace Amp\Postgres\Test;
use Amp\{ CancellationToken, Promise };
use Amp\Postgres\PgSqlConnection;
/**
* @requires extension pgsql
*/
class PgSqlConnectTest extends AbstractConnectTest {
public function connect(string $connectionString, CancellationToken $token = null): Promise {
return PgSqlConnection::connect($connectionString, $token);
}
}

View File

@ -34,10 +34,6 @@ class PgSqlConnectionTest extends AbstractConnectionTest {
return new PgSqlConnection($this->handle, $socket);
}
public function getConnectCallable(): callable {
return [PgSqlConnection::class, 'connect'];
}
public function tearDown() {
\pg_query($this->handle, "ROLLBACK");
\pg_query($this->handle, "DROP TABLE test");

15
test/PqConnectTest.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace Amp\Postgres\Test;
use Amp\{ CancellationToken, Promise };
use Amp\Postgres\PqConnection;
/**
* @requires extension pq
*/
class PqConnectTest extends AbstractConnectTest {
public function connect(string $connectionString, CancellationToken $token = null): Promise {
return PqConnection::connect($connectionString, $token);
}
}

View File

@ -33,10 +33,6 @@ class PqConnectionTest extends AbstractConnectionTest {
return new PqConnection($this->handle);
}
public function getConnectCallable(): callable {
return [PqConnection::class, 'connect'];
}
public function tearDown() {
$this->handle->exec("ROLLBACK");
$this->handle->exec("DROP TABLE test");