From efb4b89252674daf68f5ea22cfd95e6e76ba536e Mon Sep 17 00:00:00 2001 From: Jordan DeLong Date: Tue, 18 Jun 2013 15:37:59 -0700 Subject: [PATCH] Port TestExtPdo to php Leaves out the mysql test, which was disabled. --- hphp/test/ext/test_ext.h | 1 - hphp/test/ext/test_ext_pdo.cpp | 167 ---------------------- hphp/test/ext/test_ext_pdo.h | 37 ----- hphp/test/slow/ext_pdo/ext_pdo.php | 79 ++++++++++ hphp/test/slow/ext_pdo/ext_pdo.php.expect | 22 +++ 5 files changed, 101 insertions(+), 205 deletions(-) delete mode 100644 hphp/test/ext/test_ext_pdo.cpp delete mode 100644 hphp/test/ext/test_ext_pdo.h create mode 100644 hphp/test/slow/ext_pdo/ext_pdo.php create mode 100644 hphp/test/slow/ext_pdo/ext_pdo.php.expect diff --git a/hphp/test/ext/test_ext.h b/hphp/test/ext/test_ext.h index 78ca54048..ec51d94b7 100644 --- a/hphp/test/ext/test_ext.h +++ b/hphp/test/ext/test_ext.h @@ -22,7 +22,6 @@ #include "hphp/test/ext/test_ext_curl.h" #include "hphp/test/ext/test_ext_memcached.h" #include "hphp/test/ext/test_ext_mysql.h" -#include "hphp/test/ext/test_ext_pdo.h" #include "hphp/test/ext/test_ext_process.h" #include "hphp/test/ext/test_ext_server.h" diff --git a/hphp/test/ext/test_ext_pdo.cpp b/hphp/test/ext/test_ext_pdo.cpp deleted file mode 100644 index defd07cfd..000000000 --- a/hphp/test/ext/test_ext_pdo.cpp +++ /dev/null @@ -1,167 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | HipHop for PHP | - +----------------------------------------------------------------------+ - | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ -*/ - -#include "hphp/test/ext/test_ext_pdo.h" -#include "hphp/runtime/ext/ext_mysql.h" -#include "hphp/runtime/ext/ext_pdo.h" -#include "hphp/runtime/ext/ext_sqlite3.h" -#include "hphp/runtime/ext/ext_file.h" -#include "hphp/test/ext/test_mysql_info.h" - -IMPLEMENT_SEP_EXTENSION_TEST(Pdo); -/////////////////////////////////////////////////////////////////////////////// - -bool TestExtPdo::RunTests(const std::string &which) { - bool ret = true; - - DECLARE_TEST_FUNCTIONS(""); - - RUN_TEST(test_pdo_drivers); - RUN_TEST(test_pdo_mysql); - RUN_TEST(test_pdo_sqlite); - - return ret; -} - -static void CreateMySqlTestTable() { - Variant conn = f_mysql_connect(TEST_HOSTNAME, TEST_USERNAME, TEST_PASSWORD); - f_mysql_select_db(TEST_DATABASE); - f_mysql_query("drop table test"); - f_mysql_query("create table test (id int not null auto_increment," - " name varchar(255) not null, primary key (id)) " - "engine=innodb"); - f_mysql_query("insert into test (name) values ('test'),('test2')"); -} - -static void CreateSqliteTestTable() { - f_unlink("/tmp/foo.db"); - p_SQLite3 db(NEWOBJ(c_SQLite3)()); - db->t_open("/tmp/foo.db"); - db->t_exec("CREATE TABLE foo (bar STRING)"); - db->t_exec("INSERT INTO foo VALUES ('ABC')"); - db->t_exec("INSERT INTO foo VALUES ('DEF')"); -} - -static void CleanupSqliteTestTable() { - f_unlink("/tmp/foo.db"); -} - -/////////////////////////////////////////////////////////////////////////////// - -bool TestExtPdo::test_pdo_drivers() { - VERIFY(!f_pdo_drivers().empty()); - return Count(true); -} - -bool TestExtPdo::test_pdo_mysql() { - // XXX: Disabled until flakiness is resolved: t2196379 - return CountSkip(); - - CreateMySqlTestTable(); - - try { - string source = "mysql:host="; - string host = TEST_HOSTNAME; - size_t pos = host.find(':'); - if (pos != string::npos) { - host.replace(pos, 1, ";port="); - } - source += host; - source += ";dbname="; - source += TEST_DATABASE; - - p_PDO dbh(NEWOBJ(c_PDO)()); - dbh->t___construct(source.c_str(), TEST_USERNAME, TEST_PASSWORD, - CREATE_MAP1(q_PDO$$ATTR_PERSISTENT, false)); - Variant vstmt = dbh->t_prepare("select * from test"); - c_PDOStatement *stmt = vstmt.toObject().getTyped(); - VERIFY(stmt->t_execute()); - - Variant rs = stmt->t_fetch(q_PDO$$FETCH_ASSOC); - VS(rs, CREATE_MAP2("id", "1", "name", "test")); - rs = stmt->t_fetch(q_PDO$$FETCH_ASSOC); - VS(rs, CREATE_MAP2("id", "2", "name", "test2")); - - } catch (Object &e) { - VS(e, uninit_null()); - } - return Count(true); -} - -bool TestExtPdo::test_pdo_sqlite() { - static const StaticString s_id("id"); - CreateSqliteTestTable(); - - try { - string source = "sqlite:/tmp/foo.db"; - - p_PDO dbh(NEWOBJ(c_PDO)()); - dbh->t___construct(source.c_str(), TEST_USERNAME, TEST_PASSWORD, - CREATE_MAP1(q_PDO$$ATTR_PERSISTENT, false)); - Variant vstmt = dbh->t_prepare("select * from foo"); - c_PDOStatement *stmt = vstmt.toObject().getTyped(); - VERIFY(stmt->t_execute()); - - Variant rs = stmt->t_fetch(q_PDO$$FETCH_ASSOC); - VS(rs, CREATE_MAP1("bar", "ABC")); - rs = stmt->t_fetch(q_PDO$$FETCH_ASSOC); - VS(rs, CREATE_MAP1("bar", "DEF")); - - } catch (Object &e) { - VS(e, uninit_null()); - } - - try { - string source = "sqlite:/tmp/foo.db"; - - p_PDO dbh(NEWOBJ(c_PDO)()); - dbh->t___construct(source.c_str(), TEST_USERNAME, TEST_PASSWORD, - CREATE_MAP1(q_PDO$$ATTR_PERSISTENT, false)); - Variant vstmt = dbh->t_query("select * from foo"); - ArrayIter iter = vstmt.begin(); - VERIFY(!iter.end()); - VS(iter.first(), 0); - VS(iter.second(), CREATE_MAP2("bar", "ABC", 0, "ABC")); - iter.next(); - VERIFY(!iter.end()); - VS(iter.first(), 1); - VS(iter.second(), CREATE_MAP2("bar", "DEF", 0, "DEF")); - iter.next(); - VERIFY(iter.end()); - - } catch (Object &e) { - VS(e, uninit_null()); - } - - try { - string source = "sqlite:/tmp/foo.db"; - p_PDO dbh(NEWOBJ(c_PDO)()); - dbh->t___construct(source.c_str(), TEST_USERNAME, TEST_PASSWORD, - CREATE_MAP1(q_PDO$$ATTR_PERSISTENT, false)); - dbh->t_query("CREATE TABLE IF NOT EXISTS foobar (id INT)"); - dbh->t_query("INSERT INTO foobar (id) VALUES (1)"); - Variant res = dbh->t_query("SELECT id FROM foobar LIMIT 1"); - c_PDOStatement *stmt = res.toObject().getTyped(); - Variant ret = stmt->t_fetch(); - VS(ret[s_id], "1"); - - } catch (Object &e) { - VS(e, uninit_null()); - } - - CleanupSqliteTestTable(); - return Count(true); -} diff --git a/hphp/test/ext/test_ext_pdo.h b/hphp/test/ext/test_ext_pdo.h deleted file mode 100644 index b67ed2991..000000000 --- a/hphp/test/ext/test_ext_pdo.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - +----------------------------------------------------------------------+ - | HipHop for PHP | - +----------------------------------------------------------------------+ - | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) | - +----------------------------------------------------------------------+ - | This source file is subject to version 3.01 of the PHP license, | - | that is bundled with this package in the file LICENSE, and is | - | available through the world-wide-web at the following url: | - | http://www.php.net/license/3_01.txt | - | If you did not receive a copy of the PHP license and are unable to | - | obtain it through the world-wide-web, please send a note to | - | license@php.net so we can mail you a copy immediately. | - +----------------------------------------------------------------------+ -*/ - -#ifndef incl_HPHP_TEST_EXT_PDO_H_ -#define incl_HPHP_TEST_EXT_PDO_H_ - -// >>>>>> Generated by idl.php. Do NOT modify. <<<<<< - -#include "hphp/test/ext/test_cpp_ext.h" - -/////////////////////////////////////////////////////////////////////////////// - -class TestExtPdo : public TestCppExt { - public: - virtual bool RunTests(const std::string &which); - - bool test_pdo_drivers(); - bool test_pdo_mysql(); - bool test_pdo_sqlite(); -}; - -/////////////////////////////////////////////////////////////////////////////// - -#endif // incl_HPHP_TEST_EXT_PDO_H_ diff --git a/hphp/test/slow/ext_pdo/ext_pdo.php b/hphp/test/slow/ext_pdo/ext_pdo.php new file mode 100644 index 000000000..1c9dda7da --- /dev/null +++ b/hphp/test/slow/ext_pdo/ext_pdo.php @@ -0,0 +1,79 @@ +exec("CREATE TABLE foo (bar STRING)"); + $db->exec("INSERT INTO foo VALUES ('ABC')"); + $db->exec("INSERT INTO foo VALUES ('DEF')"); + VS($db->lasterrorcode(), 0); +} + +function cleanupSqliteTestTable($tmp_sqllite) { + unlink($tmp_sqllite); +} + +/////////////////////////////////////////////////////////////////////////////// + +VERIFY(count(pdo_drivers()) > 0); + +createSqliteTestTable($tmp_sqllite); + +$source = "sqlite:$tmp_sqllite"; + +try { + $dbh = new PDO($source); + $dbh->query("CREATE TABLE IF NOT EXISTS foobar (id INT)"); + $dbh->query("INSERT INTO foobar (id) VALUES (1)"); + $stmt = $dbh->query("SELECT id FROM foobar LIMIT 1"); + $ret = $stmt->fetch(); + VS($ret['id'], "1"); + +} catch (Exception $e) { + VS($e, null); +} + +try { + $dbh = new PDO($source); + VERIFY($dbh != null); + $stmt = $dbh->prepare("select * from foo"); + VERIFY($stmt != false); + VERIFY($stmt->execute()); + + $rs = $stmt->fetch(PDO::FETCH_ASSOC); + VS($rs, array("bar" => "ABC")); + $rs = $stmt->fetch(PDO::FETCH_ASSOC); + VS($rs, array("bar" => "DEF")); + +} catch (Exception $e) { + VS($e, null); +} + +try { + $dbh = new PDO($source); + $vstmt = $dbh->query("select * from foo"); + + foreach ($vstmt as $k => $v) { + var_dump($k); + var_dump($v); + } + + unset($dbh); + unset($vstmt); + +} catch (Exception $e) { + VS($e, null); +} + +cleanupSqliteTestTable($tmp_sqllite); diff --git a/hphp/test/slow/ext_pdo/ext_pdo.php.expect b/hphp/test/slow/ext_pdo/ext_pdo.php.expect new file mode 100644 index 000000000..ccacb1dbb --- /dev/null +++ b/hphp/test/slow/ext_pdo/ext_pdo.php.expect @@ -0,0 +1,22 @@ +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +int(0) +array(2) { + ["bar"]=> + string(3) "ABC" + [0]=> + string(3) "ABC" +} +int(1) +array(2) { + ["bar"]=> + string(3) "DEF" + [0]=> + string(3) "DEF" +}