1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-02 17:37:57 +01:00
postgres/test/PgSqlConnectionTest.php

53 lines
1.5 KiB
PHP
Raw Normal View History

2016-12-30 06:21:17 +01:00
<?php
2016-09-14 16:27:39 +02:00
namespace Amp\Postgres\Test;
use Amp\Loop;
2017-11-06 01:12:12 +01:00
use Amp\Postgres\Link;
use Amp\Postgres\PgSqlConnection;
2016-09-14 16:27:39 +02:00
2016-09-20 07:47:16 +02:00
/**
* @requires extension pgsql
*/
2018-07-01 19:33:12 +02:00
class PgSqlConnectionTest extends AbstractConnectionTest
{
2016-09-14 16:27:39 +02:00
/** @var resource PostgreSQL connection resource. */
protected $handle;
2018-07-01 19:33:12 +02:00
public function createLink(string $connectionString): Link
{
if (Loop::get()->getHandle() instanceof \EvLoop) {
$this->markTestSkipped("ext-pgsql is not compatible with pecl-ev");
}
2016-09-14 16:27:39 +02:00
$this->handle = \pg_connect($connectionString);
$socket = \pg_socket($this->handle);
2017-05-16 06:28:37 +02:00
2017-05-26 23:26:25 +02:00
\pg_query($this->handle, "DROP TABLE IF EXISTS test");
2016-09-14 16:27:39 +02:00
$result = \pg_query($this->handle, "CREATE TABLE test (domain VARCHAR(63), tld VARCHAR(63), PRIMARY KEY (domain, tld))");
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
if (!$result) {
$this->fail('Could not create test table.');
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
foreach ($this->getData() as $row) {
$result = \pg_query_params($this->handle, "INSERT INTO test VALUES (\$1, \$2)", $row);
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
if (!$result) {
$this->fail('Could not insert test data.');
}
}
2017-05-16 06:28:37 +02:00
2016-09-14 16:27:39 +02:00
return new PgSqlConnection($this->handle, $socket);
}
2017-05-16 06:28:37 +02:00
2020-02-06 23:34:49 +01:00
public function tearDown(): void
2018-07-01 19:33:12 +02:00
{
2017-06-21 07:32:29 +02:00
\pg_get_result($this->handle); // Consume any leftover results from test.
2016-09-14 16:27:39 +02:00
\pg_query($this->handle, "ROLLBACK");
\pg_query($this->handle, "DROP TABLE test");
2017-11-06 06:06:17 +01:00
\pg_close($this->handle);
2016-09-14 16:27:39 +02:00
}
}