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

760 lines
23 KiB
PHP
Raw Normal View History

<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres\Test;
use Amp\Coroutine;
use Amp\Delayed;
2017-05-26 17:47:44 +02:00
use Amp\Loop;
2019-09-27 05:41:47 +02:00
use Amp\PHPUnit\AsyncTestCase;
2017-11-06 01:12:12 +01:00
use Amp\Postgres\Link;
use Amp\Postgres\Listener;
use Amp\Postgres\QueryExecutionError;
use Amp\Postgres\Transaction;
2019-09-27 05:41:47 +02:00
use Amp\Promise;
2018-07-01 19:33:12 +02:00
use Amp\Sql\QueryError;
2020-05-23 00:53:14 +02:00
use Amp\Sql\Result;
2018-07-01 19:33:12 +02:00
use Amp\Sql\Statement;
use Amp\Sql\Transaction as SqlTransaction;
2018-07-03 01:45:09 +02:00
use Amp\Sql\TransactionError;
2020-05-21 22:44:56 +02:00
use Amp\Stream;
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
abstract class AbstractLinkTest extends AsyncTestCase
2018-07-01 19:33:12 +02:00
{
2016-09-14 16:27:39 +02:00
/** @var \Amp\Postgres\Connection */
protected $connection;
/**
* @return array Start test data for database.
*/
2020-02-06 23:34:49 +01:00
public function getData(): array
2018-07-01 19:33:12 +02:00
{
2016-09-14 16:27:39 +02:00
return [
['amphp', 'org'],
['github', 'com'],
['google', 'com'],
['php', 'net'],
];
}
2017-05-16 06:28:37 +02:00
2017-06-15 05:46:25 +02:00
/**
* @param string $connectionString
*
2017-11-06 01:12:12 +01:00
* @return \Amp\Postgres\Link Connection or Link object to be tested.
2017-06-15 05:46:25 +02:00
*/
2017-11-06 01:12:12 +01:00
abstract public function createLink(string $connectionString): Link;
2016-09-14 16:27:39 +02:00
2020-02-06 23:34:49 +01:00
public function setUp(): void
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
parent::setUp();
$this->connection = $this->createLink('host=localhost user=postgres');
}
2019-09-27 05:41:47 +02:00
public function testQueryWithTupleResult(): \Generator
2018-07-01 19:33:12 +02:00
{
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $this->connection->query("SELECT * FROM test");
2016-09-14 16:27:39 +02:00
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2017-05-16 06:28:37 +02:00
2019-09-27 05:41:47 +02:00
$data = $this->getData();
2016-09-14 16:27:39 +02:00
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
2020-05-23 00:53:14 +02:00
$this->assertNull(yield $result->getNextResult());
2020-05-21 22:44:56 +02:00
}
public function testMultipleQueryWithTupleResult(): \Generator
{
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2020-05-21 22:44:56 +02:00
$result = yield $this->connection->query("SELECT * FROM test; SELECT * FROM test");
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
$data = $this->getData();
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
$result = yield $result->getNextResult();
$this->assertInstanceOf(Result::class, $result);
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
$this->assertNull(yield $result->getNextResult());
}
public function testMultipleQueryWithCommandResultFirst(): \Generator
{
/** @var Result $result */
$result = yield $this->connection->query("INSERT INTO test (domain, tld) VALUES ('gitlab', 'com'); SELECT * FROM test");
$this->assertInstanceOf(Result::class, $result);
$this->assertNull(yield $result->continue());
2020-05-21 22:44:56 +02:00
2020-05-23 00:53:14 +02:00
$this->assertSame(1, $result->getRowCount());
$result = yield $result->getNextResult();
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
$data = $this->getData();
2020-05-23 00:53:14 +02:00
$data[] = ['gitlab', 'com']; // Add inserted row to expected data.
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
2020-05-23 00:53:14 +02:00
$this->assertNull(yield $result->getNextResult());
}
2020-05-21 22:44:56 +02:00
2020-05-23 00:53:14 +02:00
public function testMultipleQueryWithCommandResultSecond(): \Generator
{
/** @var Result $result */
$result = yield $this->connection->query("SELECT * FROM test; INSERT INTO test (domain, tld) VALUES ('gitlab', 'com')");
$this->assertInstanceOf(Result::class, $result);
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
2020-05-23 00:53:14 +02:00
$result = yield $result->getNextResult();
$this->assertNull(yield $result->continue());
$this->assertSame(1, $result->getRowCount());
$this->assertNull(yield $result->getNextResult());
2016-09-14 16:27:39 +02:00
}
2019-09-27 05:41:47 +02:00
public function testQueryWithUnconsumedTupleResult(): \Generator
2018-07-01 19:33:12 +02:00
{
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $this->connection->query("SELECT * FROM test");
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2019-09-27 05:41:47 +02:00
unset($result); // Force destruction of result object.
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $this->connection->query("SELECT * FROM test");
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2019-09-27 05:41:47 +02:00
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
}
2020-05-23 00:53:14 +02:00
public function testMultipleQueries(): \Generator
{
/** @var Result $result */
$result = yield $this->connection->query("SELECT * FROM test; INSERT INTO test (domain, tld) VALUES ('gitlab', 'com'); SELECT * FROM test");
$this->assertInstanceOf(Result::class, $result);
$data = $this->getData();
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
$result = yield $result->getNextResult();
$this->assertNull(yield $result->continue());
$this->assertSame(1, $result->getRowCount());
$result = yield $result->getNextResult();
$this->assertInstanceOf(Result::class, $result);
$data = $this->getData();
$data[] = ['gitlab', 'com']; // Add inserted row to expected data.
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
$this->assertNull(yield $result->getNextResult());
$this->assertNull(yield $result->getNextResult());
}
2019-09-27 05:41:47 +02:00
public function testQueryWithCommandResult(): \Generator
2018-07-01 19:33:12 +02:00
{
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $this->connection->query("INSERT INTO test VALUES ('canon', 'jp')");
2016-09-14 16:27:39 +02:00
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
$this->assertSame(1, $result->getRowCount());
2016-09-14 16:27:39 +02:00
}
2019-09-27 05:41:47 +02:00
public function testQueryWithEmptyQuery(): Promise
2018-07-01 19:33:12 +02:00
{
2020-02-06 23:34:49 +01:00
$this->expectException(QueryError::class);
2019-09-27 05:41:47 +02:00
return $this->connection->query('');
2016-09-14 16:27:39 +02:00
}
2019-09-27 05:41:47 +02:00
public function testQueryWithSyntaxError(): \Generator
2018-07-01 19:33:12 +02:00
{
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
try {
$result = yield $this->connection->query("SELECT & FROM test");
$this->fail(\sprintf("An instance of %s was expected to be thrown", QueryExecutionError::class));
} catch (QueryExecutionError $exception) {
$diagnostics = $exception->getDiagnostics();
$this->assertArrayHasKey("sqlstate", $diagnostics);
}
2016-09-14 16:27:39 +02:00
}
2019-09-27 05:41:47 +02:00
public function testPrepare(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$query = "SELECT * FROM test WHERE domain=\$1";
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
/** @var Statement $statement */
$statement = yield $this->connection->prepare($query);
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$this->assertSame($query, $statement->getQuery());
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2016-09-14 16:27:39 +02:00
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement->execute([$data[0]]);
2016-09-14 16:27:39 +02:00
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2017-05-16 06:14:02 +02:00
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
2016-09-14 16:27:39 +02:00
}
/**
* @depends testPrepare
*/
2019-09-27 05:41:47 +02:00
public function testPrepareWithNamedParams(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$query = "SELECT * FROM test WHERE domain=:domain AND tld=:tld";
2019-09-27 05:41:47 +02:00
/** @var Statement $statement */
$statement = yield $this->connection->prepare($query);
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2019-09-27 05:41:47 +02:00
$this->assertSame($query, $statement->getQuery());
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement->execute(['domain' => $data[0], 'tld' => $data[1]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
/**
* @depends testPrepare
*/
2019-09-27 05:41:47 +02:00
public function testPrepareWithUnnamedParams(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$query = "SELECT * FROM test WHERE domain=? AND tld=?";
2019-09-27 05:41:47 +02:00
/** @var Statement $statement */
$statement = yield $this->connection->prepare($query);
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2019-09-27 05:41:47 +02:00
$this->assertSame($query, $statement->getQuery());
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement->execute([$data[0], $data[1]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
/**
* @depends testPrepare
*/
2019-09-27 05:41:47 +02:00
public function testPrepareWithNamedParamsWithDataAppearingAsNamedParam(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$query = "SELECT * FROM test WHERE domain=:domain OR domain=':domain'";
2019-09-27 05:41:47 +02:00
/** @var Statement $statement */
$statement = yield $this->connection->prepare($query);
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2019-09-27 05:41:47 +02:00
$this->assertSame($query, $statement->getQuery());
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement->execute(['domain' => $data[0]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
/**
* @depends testPrepare
*/
2019-09-27 05:41:47 +02:00
public function testPrepareInvalidQuery(): Promise
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(QueryExecutionError::class);
$this->expectExceptionMessage('column "invalid" does not exist');
2019-09-27 05:41:47 +02:00
$query = "SELECT * FROM test WHERE invalid=\$1";
/** @var Statement $statement */
return $this->connection->prepare($query);
}
/**
* @depends testPrepare
*/
2019-09-27 05:41:47 +02:00
public function testPrepareSameQuery(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$sql = "SELECT * FROM test WHERE domain=\$1";
2019-09-27 05:41:47 +02:00
/** @var Statement $statement1 */
$statement1 = yield $this->connection->prepare($sql);
2019-09-27 05:41:47 +02:00
/** @var Statement $statement2 */
$statement2 = yield $this->connection->prepare($sql);
2019-09-27 05:41:47 +02:00
$this->assertInstanceOf(Statement::class, $statement1);
$this->assertInstanceOf(Statement::class, $statement2);
2019-09-27 05:41:47 +02:00
unset($statement1);
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement2->execute([$data[0]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
/**
* @depends testPrepareSameQuery
*/
2019-09-27 05:41:47 +02:00
public function testSimultaneousPrepareSameQuery(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$sql = "SELECT * FROM test WHERE domain=\$1";
2019-09-27 05:41:47 +02:00
$statement1 = $this->connection->prepare($sql);
$statement2 = $this->connection->prepare($sql);
2019-09-27 05:41:47 +02:00
/**
* @var Statement $statement1
* @var Statement $statement2
*/
list($statement1, $statement2) = yield [$statement1, $statement2];
2019-09-27 05:41:47 +02:00
$this->assertInstanceOf(Statement::class, $statement1);
$this->assertInstanceOf(Statement::class, $statement2);
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement1->execute([$data[0]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
2019-09-27 05:41:47 +02:00
unset($statement1);
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement2->execute([$data[0]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
2019-09-27 05:41:47 +02:00
public function testPrepareSimilarQueryReturnsDifferentStatements(): \Generator
{
2019-09-27 05:41:47 +02:00
/** @var Statement $statement1 */
$statement1 = $this->connection->prepare("SELECT * FROM test WHERE domain=\$1");
2019-09-27 05:41:47 +02:00
/** @var Statement $statement2 */
$statement2 = $this->connection->prepare("SELECT * FROM test WHERE domain=:domain");
2019-09-27 05:41:47 +02:00
list($statement1, $statement2) = yield [$statement1, $statement2];
2019-09-27 05:41:47 +02:00
$this->assertInstanceOf(Statement::class, $statement1);
$this->assertInstanceOf(Statement::class, $statement2);
2019-09-27 05:41:47 +02:00
$this->assertNotSame($statement1, $statement2);
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2019-09-27 05:41:47 +02:00
$results = [];
2020-05-21 22:44:56 +02:00
$results[] = yield Stream\toArray(yield $statement1->execute([$data[0]]));
$results[] = yield Stream\toArray(yield $statement2->execute(['domain' => $data[0]]));
2019-09-27 05:41:47 +02:00
foreach ($results as $result) {
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
foreach ($result as $row) {
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
2019-09-27 05:41:47 +02:00
}
}
2019-09-27 05:41:47 +02:00
public function testPrepareThenExecuteWithUnconsumedTupleResult(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
/** @var Statement $statement */
$statement = yield $this->connection->prepare("SELECT * FROM test");
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement->execute();
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2019-09-27 05:41:47 +02:00
unset($result); // Force destruction of result object.
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $statement->execute();
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2019-09-27 05:41:47 +02:00
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
}
2019-09-27 05:41:47 +02:00
public function testExecute(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2016-09-14 16:27:39 +02:00
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $this->connection->execute("SELECT * FROM test WHERE domain=\$1", [$data[0]]);
2016-09-14 16:27:39 +02:00
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2016-09-14 16:27:39 +02:00
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
2016-09-14 16:27:39 +02:00
}
2017-11-06 06:06:17 +01:00
/**
* @depends testExecute
*/
2019-09-27 05:41:47 +02:00
public function testExecuteWithNamedParams(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2020-05-23 00:53:14 +02:00
/** @var Result $result */
2019-09-27 05:41:47 +02:00
$result = yield $this->connection->execute(
"SELECT * FROM test WHERE domain=:domain",
['domain' => $data[0]]
);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
2019-09-27 05:41:47 +02:00
$this->assertSame($data[0], $row['domain']);
$this->assertSame($data[1], $row['tld']);
}
}
/**
* @depends testExecute
2017-11-06 06:06:17 +01:00
*/
2019-09-27 05:41:47 +02:00
public function testExecuteWithInvalidParams(): Promise
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(\Error::class);
$this->expectExceptionMessage("Value for unnamed parameter at position 0 missing");
return $this->connection->execute("SELECT * FROM test WHERE domain=\$1");
2017-11-06 06:06:17 +01:00
}
/**
* @depends testExecute
*/
2019-09-27 05:41:47 +02:00
public function testExecuteWithInvalidNamedParams(): Promise
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(\Error::class);
$this->expectExceptionMessage("Value for named parameter 'domain' missing");
return $this->connection->execute("SELECT * FROM test WHERE domain=:domain", ['tld' => 'com']);
}
2016-09-14 16:27:39 +02:00
/**
* @depends testQueryWithTupleResult
2016-09-14 16:27:39 +02:00
*/
2019-09-27 05:41:47 +02:00
public function testSimultaneousQuery(): Promise
2018-07-01 19:33:12 +02:00
{
$callback = \Amp\coroutine(function ($value) {
2020-05-23 00:53:14 +02:00
/** @var Result $result */
$result = yield $this->connection->query("SELECT {$value} as value");
if ($value) {
yield new Delayed(100);
}
2020-05-21 22:44:56 +02:00
while ($row = yield $result->continue()) {
$this->assertEquals($value, $row['value']);
}
});
2019-09-27 05:41:47 +02:00
return Promise\all([$callback(0), $callback(1)]);
}
/**
* @depends testSimultaneousQuery
*/
2019-09-27 05:41:47 +02:00
public function testSimultaneousQueryWithOneFailing(): \Generator
2018-07-01 19:33:12 +02:00
{
$callback = \Amp\coroutine(function ($query) {
2020-05-23 00:53:14 +02:00
/** @var Result $result */
$result = yield $this->connection->query($query);
$data = $this->getData();
2017-05-16 06:14:02 +02:00
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
return $result;
});
2019-09-27 05:41:47 +02:00
$result = null;
try {
2019-09-27 05:41:47 +02:00
$successful = $callback("SELECT * FROM test");
$failing = $callback("SELECT & FROM test");
2019-09-27 05:41:47 +02:00
$result = yield $successful;
yield $failing;
} catch (QueryError $exception) {
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
return;
}
$this->fail(\sprintf("Test did not throw an instance of %s", QueryError::class));
}
2019-09-27 05:41:47 +02:00
public function testSimultaneousQueryAndPrepare(): Promise
2018-07-01 19:33:12 +02:00
{
$promises = [];
$promises[] = new Coroutine((function () {
2020-05-23 00:53:14 +02:00
/** @var Result $result */
$result = yield $this->connection->query("SELECT * FROM test");
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
})());
$promises[] = new Coroutine((function () {
2018-07-01 19:33:12 +02:00
/** @var Statement $statement */
$statement = (yield $this->connection->prepare("SELECT * FROM test"));
2020-05-23 00:53:14 +02:00
/** @var Result $result */
$result = yield $statement->execute();
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
})());
2019-09-27 05:41:47 +02:00
return Promise\all($promises);
}
2019-09-27 05:41:47 +02:00
public function testSimultaneousPrepareAndExecute(): Promise
2018-07-01 19:33:12 +02:00
{
$promises[] = new Coroutine((function () {
2018-07-01 19:33:12 +02:00
/** @var Statement $statement */
$statement = yield $this->connection->prepare("SELECT * FROM test");
2020-05-23 00:53:14 +02:00
/** @var Result $result */
$result = yield $statement->execute();
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
}
})());
$promises[] = new Coroutine((function () {
2020-05-23 00:53:14 +02:00
/** @var Result $result */
$result = yield $this->connection->execute("SELECT * FROM test");
$data = $this->getData();
2020-05-21 22:44:56 +02:00
for ($i = 0; $row = yield $result->continue(); ++$i) {
$this->assertSame($data[$i][0], $row['domain']);
$this->assertSame($data[$i][1], $row['tld']);
2016-09-14 16:27:39 +02:00
}
})());
2017-05-16 06:14:02 +02:00
2019-09-27 05:41:47 +02:00
return Promise\all($promises);
2016-09-14 16:27:39 +02:00
}
2019-09-27 05:41:47 +02:00
public function testTransaction(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$isolation = SqlTransaction::ISOLATION_COMMITTED;
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
/** @var \Amp\Postgres\Transaction $transaction */
$transaction = yield $this->connection->beginTransaction($isolation);
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$this->assertInstanceOf(Transaction::class, $transaction);
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$data = $this->getData()[0];
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$this->assertTrue($transaction->isAlive());
$this->assertTrue($transaction->isActive());
$this->assertSame($isolation, $transaction->getIsolationLevel());
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
yield $transaction->createSavepoint('test');
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$statement = yield $transaction->prepare("SELECT * FROM test WHERE domain=:domain");
$result = yield $statement->execute(['domain' => $data[0]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2019-09-27 05:41:47 +02:00
unset($result); // Force destruction of result object.
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
$result = yield $transaction->execute("SELECT * FROM test WHERE domain=\$1 FOR UPDATE", [$data[0]]);
2020-05-23 00:53:14 +02:00
$this->assertInstanceOf(Result::class, $result);
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
unset($result); // Force destruction of result object.
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
yield $transaction->rollbackTo('test');
2016-09-14 16:27:39 +02:00
2019-09-27 05:41:47 +02:00
yield $transaction->commit();
$this->assertFalse($transaction->isAlive());
$this->assertFalse($transaction->isActive());
try {
$result = yield $transaction->execute("SELECT * FROM test");
$this->fail('Query should fail after transaction commit');
} catch (TransactionError $exception) {
// Exception expected.
}
2016-09-14 16:27:39 +02:00
}
2017-05-16 06:14:02 +02:00
2019-09-27 05:41:47 +02:00
public function testListen(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$channel = "test";
/** @var \Amp\Postgres\Listener $listener */
$listener = yield $this->connection->listen($channel);
2019-09-27 05:41:47 +02:00
$this->assertInstanceOf(Listener::class, $listener);
$this->assertSame($channel, $listener->getChannel());
Loop::delay(100, function () use ($channel) {
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '1'));
});
$count = 0;
Loop::delay(200, function () use ($listener) {
$listener->unlisten();
});
2019-09-27 05:41:47 +02:00
2020-05-21 22:44:56 +02:00
while ($notification = yield $listener->continue()) {
$this->assertSame($notification->payload, (string) $count++);
2019-09-27 05:41:47 +02:00
}
$this->assertSame(2, $count);
}
/**
* @depends testListen
*/
2019-09-27 05:41:47 +02:00
public function testNotify(): \Generator
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$channel = "test";
/** @var \Amp\Postgres\Listener $listener */
$listener = yield $this->connection->listen($channel);
2019-09-27 05:41:47 +02:00
Loop::delay(100, function () use ($channel) {
yield $this->connection->notify($channel, '0');
yield $this->connection->notify($channel, '1');
});
2019-09-27 05:41:47 +02:00
$count = 0;
Loop::delay(200, function () use ($listener) {
$listener->unlisten();
});
2020-05-21 22:44:56 +02:00
while ($notification = yield $listener->continue()) {
$this->assertSame($notification->payload, (string) $count++);
2019-09-27 05:41:47 +02:00
}
$this->assertSame(2, $count);
}
/**
* @depends testListen
*/
2019-09-27 05:41:47 +02:00
public function testListenOnSameChannel(): Promise
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(QueryError::class);
$this->expectExceptionMessage('Already listening on channel');
$channel = "test";
return Promise\all([$this->connection->listen($channel), $this->connection->listen($channel)]);
}
2016-09-14 16:27:39 +02:00
}