Port TestExtPdo to php

Leaves out the mysql test, which was disabled.
Esse commit está contido em:
Jordan DeLong
2013-06-18 15:37:59 -07:00
commit de Sara Golemon
commit efb4b89252
5 arquivos alterados com 101 adições e 205 exclusões
-1
Ver Arquivo
@@ -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"
-167
Ver Arquivo
@@ -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<c_PDOStatement>();
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<c_PDOStatement>();
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<c_PDOStatement>();
Variant ret = stmt->t_fetch();
VS(ret[s_id], "1");
} catch (Object &e) {
VS(e, uninit_null());
}
CleanupSqliteTestTable();
return Count(true);
}
-37
Ver Arquivo
@@ -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_
+79
Ver Arquivo
@@ -0,0 +1,79 @@
<?php
function VS($x, $y) {
var_dump($x === $y);
if ($x !== $y) { echo "Failed: $y\n"; echo "Got: $x\n";
var_dump(debug_backtrace()); }
}
function VERIFY($x) { VS($x != false, true); }
//////////////////////////////////////////////////////////////////////
$tmp_sqllite = tempnam('/tmp', 'vmpdotest');
function createSqliteTestTable($tmp_sqllite) {
unlink($tmp_sqllite);
$db = new SQLite3($tmp_sqllite);
$db->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);
+22
Ver Arquivo
@@ -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"
}