summaryrefslogtreecommitdiff
path: root/ext/pdo_pgsql/tests
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2010-10-21 08:52:46 +0200
committerOndřej Surý <ondrej@sury.org>2010-10-21 08:52:46 +0200
commit01fcdff3849c3691d9aaeaab735846ab6d8895ca (patch)
tree6460876d356113fa7053df36f2aa00baa7db24a9 /ext/pdo_pgsql/tests
parent855a09f4eded707941180c9d90acd17c25e29447 (diff)
downloadphp-upstream/5.3.3.tar.gz
Imported Upstream version 5.3.3upstream/5.3.3
Diffstat (limited to 'ext/pdo_pgsql/tests')
-rw-r--r--ext/pdo_pgsql/tests/copy_from.phpt386
-rw-r--r--ext/pdo_pgsql/tests/copy_to.phpt129
-rw-r--r--ext/pdo_pgsql/tests/is_in_transaction.phpt66
3 files changed, 581 insertions, 0 deletions
diff --git a/ext/pdo_pgsql/tests/copy_from.phpt b/ext/pdo_pgsql/tests/copy_from.phpt
new file mode 100644
index 000000000..2858905d0
--- /dev/null
+++ b/ext/pdo_pgsql/tests/copy_from.phpt
@@ -0,0 +1,386 @@
+--TEST--
+PDO PgSQL pgsqlCopyFromArray and pgsqlCopyFromFile
+--SKIPIF--
+<?php # vim:se ft=php:
+if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
+require dirname(__FILE__) . '/config.inc';
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
+$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
+
+$db->exec('CREATE TABLE test (a integer not null primary key, b text, c integer)');
+
+try {
+
+echo "Preparing test file and array for CopyFrom tests\n";
+
+$tableRows = array();
+$tableRowsWithDifferentNullValues = array();
+
+for($i=0;$i<3;$i++) {
+ $firstParameter = $i;
+ $secondParameter = "test insert {$i}";
+ $tableRows[] = "{$firstParameter}\t{$secondParameter}\t\\N";
+ $tableRowsWithDifferentNullValues[] = "{$firstParameter};{$secondParameter};NULL";
+ $tableRowsWithDifferentNullValuesAndSelectedFields[] = "{$firstParameter};NULL";
+}
+$filename = 'test_pgsqlCopyFromFile.csv';
+$filenameWithDifferentNullValues = 'test_pgsqlCopyFromFileWithDifferentNullValues.csv';
+$filenameWithDifferentNullValuesAndSelectedFields = 'test_pgsqlCopyFromFileWithDifferentNullValuesAndSelectedFields.csv';
+
+file_put_contents($filename, implode("\n",$tableRows));
+file_put_contents($filenameWithDifferentNullValues, implode("\n",$tableRowsWithDifferentNullValues));
+file_put_contents($filenameWithDifferentNullValuesAndSelectedFields, implode("\n",$tableRowsWithDifferentNullValuesAndSelectedFields));
+
+echo "Testing pgsqlCopyFromArray() with default parameters\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromArray('test',$tableRows));
+
+$stmt = $db->query("select * from test");
+foreach($stmt as $r) {
+ var_dump($r);
+}
+$db->rollback();
+
+echo "Testing pgsqlCopyFromArray() with different field separator and not null indicator\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValues,";","NULL"));
+$stmt = $db->query("select * from test");
+foreach($stmt as $r) {
+ var_dump($r);
+}
+$db->rollback();
+
+echo "Testing pgsqlCopyFromArray() with only selected fields\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromArray('test',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
+$stmt = $db->query("select * from test");
+foreach($stmt as $r) {
+ var_dump($r);
+}
+$db->rollback();
+
+echo "Testing pgsqlCopyFromArray() with error\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromArray('test_error',$tableRowsWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
+$db->rollback();
+
+
+echo "Testing pgsqlCopyFromFile() with default parameters\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromFile('test',$filename));
+
+$stmt = $db->query("select * from test");
+foreach($stmt as $r) {
+ var_dump($r);
+}
+$db->rollback();
+
+echo "Testing pgsqlCopyFromFile() with different field separator and not null indicator\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValues,";","NULL"));
+$stmt = $db->query("select * from test");
+foreach($stmt as $r) {
+ var_dump($r);
+}
+$db->rollback();
+
+echo "Testing pgsqlCopyFromFile() with only selected fields\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromFile('test',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
+$stmt = $db->query("select * from test");
+foreach($stmt as $r) {
+ var_dump($r);
+}
+$db->rollback();
+
+echo "Testing pgsqlCopyFromFile() with error\n";
+$db->beginTransaction();
+var_dump($db->pgsqlCopyFromFile('test_error',$filenameWithDifferentNullValuesAndSelectedFields,";","NULL",'a,c'));
+$db->rollback();
+
+} catch (Exception $e) {
+ /* catch exceptions so that we can show the relative error */
+ echo "Exception! at line ", $e->getLine(), "\n";
+ var_dump($e->getMessage());
+}
+if(isset($filename)) {
+ @unlink($filename);
+}
+?>
+--EXPECT--
+Preparing test file and array for CopyFrom tests
+Testing pgsqlCopyFromArray() with default parameters
+bool(true)
+array(6) {
+ ["a"]=>
+ int(0)
+ [0]=>
+ int(0)
+ ["b"]=>
+ string(13) "test insert 0"
+ [1]=>
+ string(13) "test insert 0"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(1)
+ [0]=>
+ int(1)
+ ["b"]=>
+ string(13) "test insert 1"
+ [1]=>
+ string(13) "test insert 1"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(2)
+ [0]=>
+ int(2)
+ ["b"]=>
+ string(13) "test insert 2"
+ [1]=>
+ string(13) "test insert 2"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+Testing pgsqlCopyFromArray() with different field separator and not null indicator
+bool(true)
+array(6) {
+ ["a"]=>
+ int(0)
+ [0]=>
+ int(0)
+ ["b"]=>
+ string(13) "test insert 0"
+ [1]=>
+ string(13) "test insert 0"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(1)
+ [0]=>
+ int(1)
+ ["b"]=>
+ string(13) "test insert 1"
+ [1]=>
+ string(13) "test insert 1"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(2)
+ [0]=>
+ int(2)
+ ["b"]=>
+ string(13) "test insert 2"
+ [1]=>
+ string(13) "test insert 2"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+Testing pgsqlCopyFromArray() with only selected fields
+bool(true)
+array(6) {
+ ["a"]=>
+ int(0)
+ [0]=>
+ int(0)
+ ["b"]=>
+ NULL
+ [1]=>
+ NULL
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(1)
+ [0]=>
+ int(1)
+ ["b"]=>
+ NULL
+ [1]=>
+ NULL
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(2)
+ [0]=>
+ int(2)
+ ["b"]=>
+ NULL
+ [1]=>
+ NULL
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+Testing pgsqlCopyFromArray() with error
+bool(false)
+Testing pgsqlCopyFromFile() with default parameters
+bool(true)
+array(6) {
+ ["a"]=>
+ int(0)
+ [0]=>
+ int(0)
+ ["b"]=>
+ string(13) "test insert 0"
+ [1]=>
+ string(13) "test insert 0"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(1)
+ [0]=>
+ int(1)
+ ["b"]=>
+ string(13) "test insert 1"
+ [1]=>
+ string(13) "test insert 1"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(2)
+ [0]=>
+ int(2)
+ ["b"]=>
+ string(13) "test insert 2"
+ [1]=>
+ string(13) "test insert 2"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+Testing pgsqlCopyFromFile() with different field separator and not null indicator
+bool(true)
+array(6) {
+ ["a"]=>
+ int(0)
+ [0]=>
+ int(0)
+ ["b"]=>
+ string(13) "test insert 0"
+ [1]=>
+ string(13) "test insert 0"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(1)
+ [0]=>
+ int(1)
+ ["b"]=>
+ string(13) "test insert 1"
+ [1]=>
+ string(13) "test insert 1"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(2)
+ [0]=>
+ int(2)
+ ["b"]=>
+ string(13) "test insert 2"
+ [1]=>
+ string(13) "test insert 2"
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+Testing pgsqlCopyFromFile() with only selected fields
+bool(true)
+array(6) {
+ ["a"]=>
+ int(0)
+ [0]=>
+ int(0)
+ ["b"]=>
+ NULL
+ [1]=>
+ NULL
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(1)
+ [0]=>
+ int(1)
+ ["b"]=>
+ NULL
+ [1]=>
+ NULL
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+array(6) {
+ ["a"]=>
+ int(2)
+ [0]=>
+ int(2)
+ ["b"]=>
+ NULL
+ [1]=>
+ NULL
+ ["c"]=>
+ NULL
+ [2]=>
+ NULL
+}
+Testing pgsqlCopyFromFile() with error
+bool(false) \ No newline at end of file
diff --git a/ext/pdo_pgsql/tests/copy_to.phpt b/ext/pdo_pgsql/tests/copy_to.phpt
new file mode 100644
index 000000000..1dc7d1de3
--- /dev/null
+++ b/ext/pdo_pgsql/tests/copy_to.phpt
@@ -0,0 +1,129 @@
+--TEST--
+PDO PgSQL pgsqlCopyToArray and pgsqlCopyToFile
+--SKIPIF--
+<?php # vim:se ft=php:
+if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
+require dirname(__FILE__) . '/config.inc';
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
+$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
+
+$db->exec('CREATE TABLE test (a integer not null primary key, b text, c integer)');
+
+$db->beginTransaction();
+try {
+
+echo "Preparing test table for CopyTo tests\n";
+$stmt = $db->prepare("INSERT INTO test (a, b, c) values (?, ?, ?)");
+
+for($i=0;$i<3;$i++) {
+ $firstParameter = $i;
+ $secondParameter = "test insert {$i}";
+ $thirdParameter = NULL;
+ $stmt->bindValue(1, $firstParameter);
+ $stmt->bindValue(2, $secondParameter);
+ $stmt->bindValue(3, $thirdParameter);
+ $stmt->execute();
+}
+
+$db->commit();
+
+echo "Testing pgsqlCopyToArray() with default parameters\n";
+var_dump($db->pgsqlCopyToArray('test'));
+echo "Testing pgsqlCopyToArray() with different field separator and not null indicator\n";
+var_dump($db->pgsqlCopyToArray('test',";","NULL"));
+echo "Testing pgsqlCopyToArray() with only selected fields\n";
+var_dump($db->pgsqlCopyToArray('test',";","NULL",'a,c'));
+
+echo "Testing pgsqlCopyToArray() with error\n";
+var_dump($db->pgsqlCopyToArray('test_error'));
+
+
+echo "Testing pgsqlCopyToFile() with default parameters\n";
+
+$filename="test_pgsqlCopyToFile.csv";
+var_dump($db->pgsqlCopyToFile('test',$filename));
+echo file_get_contents($filename);
+echo "Testing pgsqlCopyToFile() with different field separator and not null indicator\n";
+var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL"));
+echo file_get_contents($filename);
+echo "Testing pgsqlCopyToFile() with only selected fields\n";
+var_dump($db->pgsqlCopyToFile('test',$filename,";","NULL",'a,c'));
+echo file_get_contents($filename);
+
+echo "Testing pgsqlCopyToFile() with error\n";
+var_dump($db->pgsqlCopyToFile('test_error',$filename));
+
+
+} catch (Exception $e) {
+ /* catch exceptions so that we can show the relative error */
+ echo "Exception! at line ", $e->getLine(), "\n";
+ var_dump($e->getMessage());
+}
+if(isset($filename)) {
+ @unlink($filename);
+}
+?>
+--EXPECT--
+Preparing test table for CopyTo tests
+Testing pgsqlCopyToArray() with default parameters
+array(3) {
+ [0]=>
+ string(19) "0 test insert 0 \N
+"
+ [1]=>
+ string(19) "1 test insert 1 \N
+"
+ [2]=>
+ string(19) "2 test insert 2 \N
+"
+}
+Testing pgsqlCopyToArray() with different field separator and not null indicator
+array(3) {
+ [0]=>
+ string(21) "0;test insert 0;NULL
+"
+ [1]=>
+ string(21) "1;test insert 1;NULL
+"
+ [2]=>
+ string(21) "2;test insert 2;NULL
+"
+}
+Testing pgsqlCopyToArray() with only selected fields
+array(3) {
+ [0]=>
+ string(7) "0;NULL
+"
+ [1]=>
+ string(7) "1;NULL
+"
+ [2]=>
+ string(7) "2;NULL
+"
+}
+Testing pgsqlCopyToArray() with error
+bool(false)
+Testing pgsqlCopyToFile() with default parameters
+bool(true)
+0 test insert 0 \N
+1 test insert 1 \N
+2 test insert 2 \N
+Testing pgsqlCopyToFile() with different field separator and not null indicator
+bool(true)
+0;test insert 0;NULL
+1;test insert 1;NULL
+2;test insert 2;NULL
+Testing pgsqlCopyToFile() with only selected fields
+bool(true)
+0;NULL
+1;NULL
+2;NULL
+Testing pgsqlCopyToFile() with error
+bool(false) \ No newline at end of file
diff --git a/ext/pdo_pgsql/tests/is_in_transaction.phpt b/ext/pdo_pgsql/tests/is_in_transaction.phpt
new file mode 100644
index 000000000..99ff56162
--- /dev/null
+++ b/ext/pdo_pgsql/tests/is_in_transaction.phpt
@@ -0,0 +1,66 @@
+--TEST--
+PDO PgSQL isInTransaction
+--SKIPIF--
+<?php # vim:se ft=php:
+if (!extension_loaded('pdo') || !extension_loaded('pdo_pgsql')) die('skip not loaded');
+require dirname(__FILE__) . '/config.inc';
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+require dirname(__FILE__) . '/../../../ext/pdo/tests/pdo_test.inc';
+$db = PDOTest::test_factory(dirname(__FILE__) . '/common.phpt');
+$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
+
+$db->exec('CREATE TABLE test (a integer not null primary key, b text)');
+
+$db->beginTransaction();
+try {
+echo "Test PDO::PGSQL_TRANSACTION_INTRANS\n";
+var_dump($db->inTransaction());
+
+$stmt = $db->prepare("INSERT INTO test (a, b) values (?, ?)");
+$stmt->bindValue(1, 1);
+$stmt->bindValue(2, "test insert");
+$stmt->execute();
+
+$db->commit();
+
+echo "Test PDO::PGSQL_TRANSACTION_IDLE\n";
+var_dump($db->inTransaction());
+
+$db->beginTransaction();
+
+try {
+$stmt = $db->prepare("INSERT INTO test (a, b) values (?, ?)");
+$stmt->bindValue(1, "error");
+$stmt->bindValue(2, "test insert");
+$stmt->execute();
+} catch (Exception $e) {
+ /* We catch the exception because the execute will give error and we must test the PDO::PGSQL_TRANSACTION_ERROR */
+ echo "Test PDO::PGSQL_TRANSACTION_INERROR\n";
+ var_dump($db->inTransaction());
+ $db->rollBack();
+}
+
+echo "Test PDO::PGSQL_TRANSACTION_IDLE\n";
+var_dump($db->inTransaction());
+
+} catch (Exception $e) {
+ /* catch exceptions so that we can show the relative error */
+ echo "Exception! at line ", $e->getLine(), "\n";
+ var_dump($e->getMessage());
+}
+
+?>
+--EXPECT--
+Test PDO::PGSQL_TRANSACTION_INTRANS
+int(2)
+Test PDO::PGSQL_TRANSACTION_IDLE
+int(0)
+Test PDO::PGSQL_TRANSACTION_INERROR
+int(3)
+Test PDO::PGSQL_TRANSACTION_IDLE
+int(0)