public static function test_pgsqlserver() { global $configs; //-- if (SMART_FRAMEWORK_TESTUNIT_ALLOW_PGSQL_TESTS !== true) { return SmartComponents::operation_notice('Test Unit for PgSQL Server is DISABLED ...'); } //end if //-- //-- $value = date('Y-m-d H:i:s'); $comments = '"Unicode78źź:ăĂîÎâÂșȘțȚşŞţŢグッド' . '-' . Smart::random_number(1000, 9999) . "'"; //-- //-- PgSQL Tests if ((string) $configs['pgsql']['server-host'] != '' and (string) $configs['pgsql']['server-port'] != '' and (string) $configs['pgsql']['dbname'] != '' and (string) $configs['pgsql']['username'] != '') { //-- $time = microtime(true); //-- $tests = array(); $tests[] = '##### PostgreSQL / TESTS: #####'; //-- $err = ''; //-- if (Smart::random_number(1, 9) >= 5) { $tests[] = 'Random Test Dynamic Connection: Open / Drop Test Table (If Exists)'; $pgsql = new SmartPgsqlExtDb((array) $configs['pgsql']); $pgsql->write_data('DROP TABLE IF EXISTS "public"."_test_unit_db_server_tests"'); unset($pgsql); } //end if //-- $tests[] = 'PostgreSQL Server Version: ' . SmartPgsqlDb::check_server_version(); //-- $tests[] = 'Start Transaction'; SmartPgsqlDb::write_data('BEGIN'); //-- $tests[] = 'Create a Temporary Table for this Test, after transaction to test DDL'; if (SmartPgsqlDb::check_if_table_exists('_test_unit_db_server_tests', 'public') == 1) { SmartPgsqlDb::write_data('DROP TABLE "public"."_test_unit_db_server_tests"'); } //end if SmartPgsqlDb::write_data('CREATE TABLE "public"."_test_unit_db_server_tests" ( "variable" character varying(100) NOT NULL, "value" character varying(16384) DEFAULT \'\'::character varying, "comments" text DEFAULT \'\'::text NOT NULL, CONSTRAINT _test_unit_db_server_tests__check__variable CHECK ((char_length((variable)::text) >= 1)), CONSTRAINT _test_unit_db_server_tests__uniq__variable UNIQUE(variable) )'); //-- $variable = '"' . 'Ș' . "'" . substr(SmartPgsqlDb::new_safe_id('uid10seq', 'variable', '_test_unit_db_server_tests', 'public'), 3, 7); //-- if ((string) $err == '') { $tests[] = 'Check if the Test Table exists [ Positive ; Variable is: ' . $variable . ' ]'; $data = SmartPgsqlDb::check_if_table_exists('_test_unit_db_server_tests', 'public'); if ($data !== 1) { $err = 'Table Creation FAILED ... Table does not exists in the `public` schema ...'; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Write [ Insert ]'; $quer_str = 'INSERT INTO "public"."_test_unit_db_server_tests" ' . SmartPgsqlDb::prepare_write_statement(array('variable' => $variable, 'value' => $value, 'comments' => $comments), 'insert'); $data = SmartPgsqlDb::write_data($quer_str); if ($data[1] !== 1) { $err = 'Write / Insert Test Failed, should return 1 but returned: ' . $data[1]; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Write [ Insert if not exists, with Insert-SubSelect ]'; $quer_str = 'INSERT INTO "public"."_test_unit_db_server_tests" ' . SmartPgsqlDb::prepare_write_statement(array('variable' => $variable, 'value' => $value, 'comments' => $comments), 'insert-subselect'); $data = SmartPgsqlDb::write_igdata($quer_str); if ($data[1] !== 0) { $err = 'Write / Insert if not exists with insert-subselect Test Failed, should return 0 but returned: ' . $data[1]; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Write Ignore [ Update ]'; $quer_str = 'UPDATE "public"."_test_unit_db_server_tests" SET "comments" = $2 WHERE ("variable" = $1)'; $data = SmartPgsqlDb::write_igdata($quer_str, array($variable, $comments)); if ($data[1] !== 1) { $err = 'Write Ignore / Update Test Failed, should return 1 but returned: ' . $data[1]; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Count [ One Row ]'; $quer_str = 'SELECT COUNT(1) FROM "public"."_test_unit_db_server_tests" WHERE (("variable" = $1) AND ("comments" = ' . SmartPgsqlDb::escape_literal($comments) . '))'; $data = SmartPgsqlDb::count_data($quer_str, array($variable)); if ($data !== 1) { $err = 'Count Test Failed, should return 1 but returned: ' . $data; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Read [ LIKE / Literal Escape +% ]'; $data = SmartPgsqlDb::read_adata('SELECT * FROM "public"."_test_unit_db_server_tests" WHERE ("variable" LIKE ' . SmartPgsqlDb::escape_literal('%' . $variable . '_%', 'yes') . ')'); if (Smart::array_size($data) !== 0) { $err = 'Read Like / Literal-Escape +% Test Failed, should return 0 rows but returned: ' . Smart::array_size($data); } //end if } //end if if ((string) $err == '') { $tests[] = 'Read [ LIKE / Literal Escape ]'; $data = SmartPgsqlDb::read_adata('SELECT * FROM "public"."_test_unit_db_server_tests" WHERE ("variable" LIKE ' . SmartPgsqlDb::escape_literal('%' . $variable . '%') . ')'); if (Smart::array_size($data) !== 1) { $err = 'Read Like / Literal-Escape Test Failed, should return 1 row but returned: ' . Smart::array_size($data); } //end if } //end if if ((string) $err == '') { $tests[] = 'Read [ LIKE / Str Escape +% ]'; $data = SmartPgsqlDb::read_adata('SELECT * FROM "public"."_test_unit_db_server_tests" WHERE ("variable" ILIKE \'' . SmartPgsqlDb::escape_str('%' . $variable . '_%', 'yes') . '\')'); if (Smart::array_size($data) !== 0) { $err = 'Read Like / Str-Escape +% Test Failed, should return 0 rows but returned: ' . Smart::array_size($data); } //end if } //end if if ((string) $err == '') { $tests[] = 'Read [ LIKE / Str Escape ]'; $data = SmartPgsqlDb::read_adata('SELECT * FROM "public"."_test_unit_db_server_tests" WHERE ("variable" ILIKE \'' . SmartPgsqlDb::escape_str('%' . $variable . '%') . '\')'); if (Smart::array_size($data) !== 1) { $err = 'Read Like / Str-Escape Test Failed, should return 1 row but returned: ' . Smart::array_size($data); } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Read [ IN (SELECT) ]'; $quer_str = 'SELECT * FROM "public"."_test_unit_db_server_tests" WHERE ("variable" ' . SmartPgsqlDb::prepare_write_statement(array('\'a', '"b"', '3^$', $variable, '@?%'), 'in-select') . ') LIMIT 100 OFFSET 0'; $data = SmartPgsqlDb::read_adata($quer_str); if (Smart::array_size($data) !== 1) { $err = 'Read IN SELECT Test Failed, should return 1 row but returned: ' . Smart::array_size($data) . ' rows ...'; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Read [ (DATA) ARRAY[] ]'; $quer_str = 'SELECT * FROM "public"."_test_unit_db_server_tests" WHERE ("variable" = ANY(' . SmartPgsqlDb::prepare_write_statement(array('\'a', '"b"', '3^$', $variable, '@?%'), 'data-array') . ')) LIMIT 100 OFFSET 0'; $data = SmartPgsqlDb::read_adata($quer_str); if (Smart::array_size($data) !== 1) { $err = 'Read (DATA) ARRAY[] Test Failed, should return 1 row but returned: ' . Smart::array_size($data) . ' rows ...'; } //end if } //end if //-- $quer_str = 'SELECT "comments" FROM "public"."_test_unit_db_server_tests" WHERE ("variable" = $1) LIMIT 1 OFFSET 0'; //-- if ((string) $err == '') { $tests[] = 'Read [ Non-Associative + Param Query $ ]'; //$data = SmartPgsqlDb::read_data($quer_str, array($variable)); //$param_query = str_replace('$1', '?', $quer_str); // convert $1 to ? $param_query = (string) $quer_str; // no more necessary to convert $1 to ? as prepare_param_query() has been extended to support ? or $# $param_query = SmartPgsqlDb::prepare_param_query($param_query, array($variable)); $data = SmartPgsqlDb::read_data($param_query, 'Test Param Query'); if (trim($data[0]) !== (string) $comments) { $err = 'Read / Non-Associative Test Failed, should return `' . $comments . '` but returned `' . $data[0] . '`'; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Read [ Associative: One-Row ]'; $data = SmartPgsqlDb::read_asdata($quer_str, array($variable)); if (trim($data['comments']) !== (string) $comments) { $err = 'Read / Associative / One-Row Test Failed, should return `' . $comments . '` but returned `' . $data['comments'] . '`'; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Read [ Associative: Multi-Rows ]'; $data = SmartPgsqlDb::read_adata($quer_str, array($variable)); if (trim($data[0]['comments']) !== (string) $comments) { $err = 'Read / Associative / Multi-Rows Test Failed, should return `' . $comments . '` but returned `' . $data[0]['comments'] . '`'; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Write [ Delete ]'; $quer_str = 'DELETE FROM "public"."_test_unit_db_server_tests" WHERE ("variable" = \'' . SmartPgsqlDb::escape_str($variable) . '\')'; $data = SmartPgsqlDb::write_data($quer_str); if ($data[1] !== 1) { $err = 'Write / Delete Test Failed, should return 1 but returned: ' . $data[1]; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Write Ignore Duplicates [ Insert Ignore Positive ]'; $quer_str = 'INSERT INTO "public"."_test_unit_db_server_tests" ' . SmartPgsqlDb::prepare_write_statement(array('variable' => $variable, 'value' => null, 'comments' => $comments), 'insert'); $data = SmartPgsqlDb::write_igdata($quer_str); if ($data[1] !== 1) { $err = 'Write / Insert Ignore Positive Test Failed, should return 1 but returned: ' . $data[1]; } //end if } //end if //-- if ((string) $err == '') { $tests[] = 'Write Ignore Duplicates [ Insert Ignore Negative ]'; $quer_str = 'INSERT INTO "public"."_test_unit_db_server_tests" ' . SmartPgsqlDb::prepare_write_statement(array('variable' => $variable, 'value' => $value, 'comments' => $comments), 'insert'); $data = SmartPgsqlDb::write_igdata($quer_str); if ($data[1] !== 0) { $err = 'Write / Insert Ignore Negative Test Failed, should return 0 but returned: ' . $data[1]; } //end if } //end if //-- $tests[] = 'Commit Transation'; SmartPgsqlDb::write_data('COMMIT'); //-- if ((string) $err == '') { $tests[] = 'Check if the Test Table Exists (Param Query ?) after Drop [ Negative ], using a new Constructor (should be able to re-use connection)'; $pgsql2 = new SmartPgsqlExtDb((array) $configs['pgsql']); $pgsql2->write_data('DROP TABLE IF EXISTS "public"."_test_unit_db_server_tests"'); $data = $pgsql2->check_if_table_exists('_test_unit_db_server_tests', 'public'); $pgsql2->prepare_param_query('SELECT ?', array('\'1"')); unset($pgsql2); if ($data == 1) { $err = 'Table Drop FAILED ... Table still exists ...'; } //end if } //end if //-- $title = 'SmartFramework PostgreSQL Server Tests: DONE ...'; //-- $time = 'TOTAL TIME was: ' . (microtime(true) - $time); //-- $end_tests = '##### END TESTS ... ' . $time . ' sec. #####'; //-- if ((string) $err == '') { $img_sign = 'lib/core/img/sign_info.png'; $img_check = 'lib/core/img/q_completed.png'; $text_main = Smart::escape_js('<span style="color:#83B953;">Good ... Perfect :: グッド ... パーフェクト</span>'); $text_info = Smart::escape_js('<h2><span style="color:#83B953;">All</span> the SmartFramework PostgreSQL Server Operations <span style="color:#83B953;">Tests PASSED on PHP</span><hr></h2><span style="font-size:14px;">' . Smart::nl_2_br(Smart::escape_html(implode("\n" . '* ', $tests) . "\n" . $end_tests)) . '</span>'); } else { $img_sign = 'lib/core/img/sign_error.png'; $img_check = 'lib/core/img/q_warning.png'; $text_main = Smart::escape_js('<span style="color:#FF5500;">An ERROR occured ... :: エラーが発生しました ...</span>'); $text_info = Smart::escape_js('<h2><span style="color:#FF5500;">A test FAILED</span> when testing PostgreSQL Server Operations.<span style="color:#FF5500;"><hr>FAILED Test Details</span>:</h2><br><span style="font-size:14px;"><pre>' . Smart::escape_html($err) . '</pre></span>'); } //end if else //-- } else { //-- $title = 'SmartFramework PostgreSQL Server Tests - PostgreSQL Server was NOT SET ...'; //-- $img_sign = 'lib/core/img/sign_info.png'; $img_check = 'lib/core/img/q_warning.png'; $text_main = Smart::escape_js('<span style="color:#778899;">No PostgreSQL Server Tests performed ...</span>'); $text_info = '<h2>The current configuration have not set the PostgreSQL Server ...</h2>'; //-- } //end if //-- //-- $html = <<<HTML <h1>{$title}</h1> <script type="text/javascript"> \tSmartJS_BrowserUtils.alert_Dialog( \t\t'<img src="{$img_sign}" align="right"><h1>{$text_main}</h1><hr><span style="color:#333333;"><img src="{$img_check}" align="right">{$text_info}<br>', \t\t'', \t\t'PostgreSQL Server Test Suite for SmartFramework: PHP', \t\t'725', \t\t'480' \t); </script> HTML; //-- //-- return $html; //-- }
/** * Check and Return the PostgreSQL Server Version * * @access private * @internal * */ public function check_server_version($y_revalidate = false) { //-- return SmartPgsqlDb::check_server_version($this->connection, $y_revalidate); //-- }