2016-12-30 06:21:17 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres\Test;
|
|
|
|
|
2017-05-17 18:14:12 +02:00
|
|
|
use Amp\Postgres\{ Connection, PgSqlConnection };
|
2016-09-14 16:27:39 +02:00
|
|
|
|
2016-09-20 07:47:16 +02:00
|
|
|
/**
|
|
|
|
* @requires extension pgsql
|
|
|
|
*/
|
2016-09-14 16:27:39 +02:00
|
|
|
class PgSqlConnectionTest extends AbstractConnectionTest {
|
|
|
|
/** @var resource PostgreSQL connection resource. */
|
|
|
|
protected $handle;
|
|
|
|
|
|
|
|
public function createConnection(string $connectionString): Connection {
|
|
|
|
$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
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function tearDown() {
|
|
|
|
\pg_query($this->handle, "ROLLBACK");
|
|
|
|
\pg_query($this->handle, "DROP TABLE test");
|
|
|
|
}
|
|
|
|
}
|