Exemple #1
0
xmlDocPtr soap_xmlParseFile(const char *filename) {
  String cache_key("HPHP.SOAP.WSDL.");
  cache_key += filename;

  Variant content = f_apc_fetch(cache_key);
  if (same(content, false)) {
    Variant stream = File::Open(filename, "rb", CREATE_MAP1
                                ("http", CREATE_MAP1("timeout", 1000)));
    if (!same(stream, false)) {
      content = f_stream_get_contents(stream);
      if (!same(content, false)) {
        f_apc_store(cache_key, content);
      }
    }
  }

  if (!same(content, false)) {
    String scontent = content.toString();
    xmlDocPtr ret = soap_xmlParseMemory(scontent.data(), scontent.size());
    if (ret) {
      ret->URL = xmlCharStrdup(filename);
    }
    return ret;
  }
  return NULL;
}
Exemple #2
0
bool TestExtFb::test_fb_compact_serialize() {
    fb_cs_test(null);
    fb_cs_test(true);
    fb_cs_test(false);
    fb_cs_test(1234.5678);
    fb_cs_test("");
    fb_cs_test("a");
    fb_cs_test("\0");
    fb_cs_test("\0 a");
    fb_cs_test("0123012301230123");
    fb_cs_test("0123012301230123a");
    fb_cs_test("012301230123012");
    fb_cs_test(Array());
    fb_cs_test(CREATE_VECTOR1(12345));
    fb_cs_test(CREATE_VECTOR3(12345,"abc",0.1234));
    fb_cs_test(CREATE_MAP1(1, 12345));
    fb_cs_test(CREATE_MAP3(1, 12345, "a", 123124, "sdf", 0.1234));
    fb_cs_test(CREATE_VECTOR1(CREATE_VECTOR1("a")));
    fb_cs_test(CREATE_VECTOR2(1, CREATE_VECTOR1("a")));
    fb_cs_test(CREATE_VECTOR2(CREATE_VECTOR1("a"), 1));
    fb_cs_test(CREATE_VECTOR2(CREATE_VECTOR1("a"), CREATE_VECTOR1(1)));

    // Test skips
    fb_cs_test(CREATE_MAP3(0, "a", 1, "b", 3, "c"));
    fb_cs_test(CREATE_MAP3(1, "a", 2, "b", 3, "c"));
    fb_cs_test(CREATE_MAP3(0, "a", 2, "b", 3, "c"));
    fb_cs_test(CREATE_MAP1(3, "a"));
    // Test for overflow
    fb_cs_test(CREATE_MAP1((int64_t)((1ULL << 63) - 1), "a"));

    // Test each power of two, +/- 1 and the negatives of them
    // Test a single number and packed inside an array
    for (int i = 0; i < 64; ++i) {
        int64_t n = (1ULL << i);
        fb_cs_test(n);
        fb_cs_test(CREATE_VECTOR1(n));
        fb_cs_test(n-1);
        fb_cs_test(CREATE_VECTOR1(n-1));
        fb_cs_test(n+1);
        fb_cs_test(CREATE_VECTOR1(n+1));
        fb_cs_test(-n);
        fb_cs_test(CREATE_VECTOR1(-n));
        fb_cs_test(-n-1);
        fb_cs_test(CREATE_VECTOR1(-n-1));
        fb_cs_test(-n+1);
        fb_cs_test(CREATE_VECTOR1(-n+1));
    }

    // Test vector code (PHP can't create those, but they might come form
    // C++ code in serialized strings)
    String s("\xfe\x01\x02\x03\xfc");  // VECTOR, 1, 2, 3, STOP
    Variant ret;
    VS(f_fb_compact_unserialize(s, ref(ret)), CREATE_VECTOR3(1, 2, 3));

    return Count(true);
}
bool TestExtJson::test_json_encode() {
  VS(f_json_encode(CREATE_MAP3("a", 1, "b", 2.3, 3, "test")),
     "{\"a\":1,\"b\":2.3,\"3\":\"test\"}");
  VS(f_json_encode(CREATE_VECTOR5("a", 1, true, false, uninit_null())),
     "[\"a\",1,true,false,null]");

  VS(f_json_encode("a\xE0"), "null");
  VS(f_json_encode("a\xE0", k_JSON_FB_LOOSE), "\"a?\"");

  VS(f_json_encode(CREATE_MAP2("0", "apple", "1", "banana")),
     "[\"apple\",\"banana\"]");

  VS(f_json_encode(CREATE_VECTOR1(CREATE_MAP1("a", "apple"))),
     "[{\"a\":\"apple\"}]");

  VS(f_json_encode(CREATE_VECTOR1(CREATE_MAP1("a", "apple")),
                   k_JSON_PRETTY_PRINT),
    "[\n    {\n        \"a\": \"apple\"\n    }\n]");

  VS(f_json_encode(CREATE_VECTOR4(1, 2, 3, CREATE_VECTOR3(1, 2, 3)),
                   k_JSON_PRETTY_PRINT),
    "[\n"
    "    1,\n"
    "    2,\n"
    "    3,\n"
    "    [\n"
    "        1,\n"
    "        2,\n"
    "        3\n"
    "    ]\n"
    "]");

  Array arr = CREATE_MAP3(
    "a", 1,
    "b", CREATE_VECTOR2(1, 2),
    "c", CREATE_MAP1("d", 42)
  );
  VS(f_json_encode(arr, k_JSON_PRETTY_PRINT),
    "{\n"
    "    \"a\": 1,\n"
    "    \"b\": [\n"
    "        1,\n"
    "        2\n"
    "    ],\n"
    "    \"c\": {\n"
    "        \"d\": 42\n"
    "    }\n"
    "}");

  return Count(true);
}
Exemple #4
0
bool TestExtPdo::test_pdo_mysql() {
  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, null);
  }
  return Count(true);
}
bool TestExtProcess::test_proc_open() {
  Array descriptorspec =
    CREATE_MAP3(0, CREATE_VECTOR2("pipe", "r"),
                1, CREATE_VECTOR2("pipe", "w"),
                2, CREATE_VECTOR3("file", "/tmp/error-output.txt", "a"));
  String cwd = "/tmp";
  Array env = CREATE_MAP1("some_option", "aeiou");

  Variant pipes;
  Variant process = f_proc_open(php_path, descriptorspec, ref(pipes), cwd, env);
  VERIFY(!same(process, false));

  {
    File *f = pipes[0].toObject().getTyped<File>();
    VERIFY(f->valid());
    String s("<?php print(getenv('some_option')); ?>", AttachLiteral);
    f->write(s);
    f->close();
  }
  {
    File *f = pipes[1].toObject().getTyped<File>();
    VERIFY(f->valid());
    StringBuffer sbuf;
    sbuf.read(f);
    f->close();
    VS(sbuf.detach(), "aeiou");
  }

  VS(f_proc_close(process.toObject()), 0);
  return Count(true);
}
Exemple #6
0
bool TestExtFb::test_fb_compact_serialize() {
  fb_cs_test(null);
  fb_cs_test(true);
  fb_cs_test(false);
  fb_cs_test(1234.5678);
  fb_cs_test("");
  fb_cs_test("a");
  fb_cs_test("\0");
  fb_cs_test("\0 a");
  fb_cs_test("0123012301230123");
  fb_cs_test("0123012301230123a");
  fb_cs_test("012301230123012");
  fb_cs_test(Array());
  fb_cs_test(CREATE_VECTOR1(12345));
  fb_cs_test(CREATE_VECTOR3(12345,"abc",0.1234));
  fb_cs_test(CREATE_MAP1(1, 12345));
  fb_cs_test(CREATE_MAP3(1, 12345, "a", 123124, "sdf", 0.1234));
  fb_cs_test(CREATE_VECTOR1(CREATE_VECTOR1("a")));
  fb_cs_test(CREATE_VECTOR2(1, CREATE_VECTOR1("a")));
  fb_cs_test(CREATE_VECTOR2(CREATE_VECTOR1("a"), 1));
  fb_cs_test(CREATE_VECTOR2(CREATE_VECTOR1("a"), CREATE_VECTOR1(1)));

  // Test skips
  fb_cs_test(CREATE_MAP3(0, "a", 1, "b", 3, "c"));
  fb_cs_test(CREATE_MAP3(1, "a", 2, "b", 3, "c"));
  fb_cs_test(CREATE_MAP3(0, "a", 2, "b", 3, "c"));
  fb_cs_test(CREATE_MAP1(3, "a"));
  // Test for overflow
  fb_cs_test(CREATE_MAP1((int64)((1ULL << 63) - 1), "a"));

  // Test each power of two, +/- 1 and the negatives of them
  // Test a single number and packed inside an array
  for (int i = 0; i < 64; ++i) {
    int64 n = (1ULL << i);
    fb_cs_test(n);    fb_cs_test(CREATE_VECTOR1(n));
    fb_cs_test(n-1);  fb_cs_test(CREATE_VECTOR1(n-1));
    fb_cs_test(n+1);  fb_cs_test(CREATE_VECTOR1(n+1));
    fb_cs_test(-n);   fb_cs_test(CREATE_VECTOR1(-n));
    fb_cs_test(-n-1); fb_cs_test(CREATE_VECTOR1(-n-1));
    fb_cs_test(-n+1); fb_cs_test(CREATE_VECTOR1(-n+1));
  }
  return Count(true);
}
bool TestExtIconv::test_iconv_mime_decode_headers() {
  VS(f_iconv_mime_decode_headers
     ("Subject: =?UTF-8?B?UHLDnGZ1bmcgUHLDnGZ1bmc=?=\n"
      "Subject: =?UTF-8?B?UHLDnGZ1bmcgUHLDnGZ1bmc=?=\n",
      0, "ISO-8859-1"),
     CREATE_MAP1("Subject", CREATE_VECTOR2("Pr\xDC""fung Pr\xDC""fung",
                                           "Pr\xDC""fung Pr\xDC""fung")));

  return Count(true);
}
bool TestExtMemcached::test_Memcached_get_set() {
  CREATE_MEMCACHED();

  const char *key = "foo";
  Array value = CREATE_MAP1("foo", "bar");
  memc->t_set(key, value, EXPIRATION);
  VS(memc->t_get(key), value);

  return Count(true);
}
bool TestExtApc::test_apc_fetch() {
  // reproducing a memory leak (3/26/09)
  f_apc_add("apcdata", CREATE_MAP2("a", "test", "b", 1)); // MapVariant
  {
    Variant apcdata = f_apc_fetch("apcdata");
    Variant c = apcdata; // bump up ref count to make a MapVariant copy
    apcdata.set("b", 3); // problem
    VS(apcdata, CREATE_MAP2("a", "test", "b", 3));
  }
  {
    Variant apcdata = f_apc_fetch("apcdata");
    apcdata += CREATE_MAP1("b", 4); // problem
    VS(apcdata, CREATE_MAP2("a", "test", "b", 1));
  }
  {
    Variant apcdata = f_apc_fetch(CREATE_VECTOR2("apcdata", "nah"));
    VS(apcdata, CREATE_MAP1("apcdata", CREATE_MAP2("a", "test", "b", 1)));
  }
  return Count(true);
}
bool TestExtMath::test_max() {
  VS(f_max(4, 2, CREATE_VECTOR4(3, 1, 6, 7)), 7);
  VS(f_max(0, CREATE_VECTOR3(2, 4, 5)), 5);
  VS(f_max(1, 0, CREATE_VECTOR1("hello")), 0);
  VS(f_max(1, "hello", CREATE_VECTOR1(0)), "hello");
  VS(f_max(1, "hello", CREATE_VECTOR1(-1)), "hello");
  VS(f_max(1, CREATE_VECTOR3(2, 4, 8),
           CREATE_VECTOR1(CREATE_VECTOR3(2, 5, 1))),
     CREATE_VECTOR3(2, 5, 1));
  VS(f_max(1, "string", CREATE_VECTOR2(CREATE_VECTOR3(2, 5, 7), 42)),
     CREATE_VECTOR3(2, 5, 7));
  VS(f_max(1, CREATE_MAP1(1, "1236150163")), "1236150163");
  return Count(true);
}
Exemple #11
0
bool TestExtSoap::test_SoapServerWSDL() {
  m_server = p_SoapServer(NEW(c_SoapServer)());
  m_server->create("test/test.wsdl", CREATE_MAP1("uri", "http://testuri.org"));
  m_server->t_addfunction("Add");

  VSOAPNS("<ns1:Add xmlns:ns1=\"http://testuri.org\">"
          "<x xsi:type=\"xsd:int\">22</x>"
          "<y xsi:type=\"xsd:int\">33</y>"
          "</ns1:Add>",
          "<ns1:AddResponse><result xsi:type=\"xsd:double\">55"
          "</result></ns1:AddResponse>",
          "");

  return Count(true);
}
bool TestExtJson::test_json_encode() {
  VS(f_json_encode(CREATE_MAP3("a", 1, "b", 2.3, 3, "test")),
     "{\"a\":1,\"b\":2.3,\"3\":\"test\"}");
  VS(f_json_encode(CREATE_VECTOR5("a", 1, true, false, null)),
     "[\"a\",1,true,false,null]");

  VS(f_json_encode("a\xE0"), "null");
  VS(f_json_encode("a\xE0", k_JSON_FB_LOOSE), "\"a?\"");

  VS(f_json_encode(CREATE_MAP2("0", "apple", "1", "banana")),
     "[\"apple\",\"banana\"]");

  VS(f_json_encode(CREATE_VECTOR1(CREATE_MAP1("a", "apple"))),
     "[{\"a\":\"apple\"}]");

  return Count(true);
}
bool TestExtApc::test_apc_add() {
  f_apc_add("ts", "TestString");
  f_apc_add("ta", CREATE_MAP2("a", 1, "b", 2));
  f_apc_add("ts", "NewValue");
  f_apc_add("ta", CREATE_VECTOR1("newelement"));
  VS(f_apc_fetch("ts"), "TestString");
  VS(f_apc_fetch("ta"), CREATE_MAP2("a", 1, "b", 2));

  VS(f_apc_fetch("ts"), "TestString");
  VS(f_apc_fetch("ta"), CREATE_MAP2("a", 1, "b", 2));

  f_apc_add("texp", "TestString", 1);
  sleep(1);
  VS(f_apc_fetch("texp"), false);

  Variant ret = f_apc_store("foo", false);
  VS(ret, true);
  ret = f_apc_add("foo", false);
  VS(ret, false);
  Variant success;
  ret = f_apc_fetch("foo", ref(success));
  VS(ret, false);
  VS(success, true);
  ret = f_apc_fetch("bar", ref(success));
  VS(ret, false);
  VS(success, false);
  Variant map1= CREATE_MAP1("foo", false);
  ret = f_apc_fetch(CREATE_VECTOR1("foo"), ref(success));
  VS(ret, map1);
  ret = f_apc_fetch(CREATE_VECTOR1("bar"), ref(success));
  VS(ret, Array::Create());
  VS(success, false);
  ret = f_apc_fetch(CREATE_VECTOR2("foo", "bar"), ref(success));
  VS(ret, map1);
  VS(success, true);
  ret = f_apc_fetch(CREATE_VECTOR4("foo", "bar", "foo", "bar"), ref(success));
  VS(ret, map1);
  VS(success, true);
  return Count(true);
}
bool TestExtIpc::test_message_queue() {
  char filename[64];
  strcpy(filename, "/tmp/XXXXXX");
  close(mkstemp(filename));

  int64 token = f_ftok(filename, "a");
  Object queue = f_msg_get_queue(token);
  VERIFY(queue.get());

  int pid = fork();
  if (pid == 0) {
    Object q = f_msg_get_queue(token);
    assert(q.get());
    assert(f_msg_send(q, 2, "start"));
    Variant type, msg;
    assert(f_msg_receive(q, 1, ref(type), 100, ref(msg)));
    assert(f_msg_send(q, 2, msg)); // echo
    _exit(-1);
  }

  Variant type, msg;
  VERIFY(f_msg_receive(queue, 2, ref(type), 100, ref(msg)));
  VERIFY(same(msg, "start"));

  VERIFY(f_msg_send(queue, 1, "ok"));
  VERIFY(f_msg_receive(queue, 2, ref(type), 100, ref(msg)));
  VERIFY(same(msg, "ok"));

  Array ret = f_msg_stat_queue(queue);
  VS(ret["msg_qnum"], 0);
  f_msg_set_queue(queue, CREATE_MAP1("msg_perm.mode", 0666));

  f_msg_remove_queue(queue);
  int status = -1;
  wait(&status);
  return Count(true);
}
bool TestExtMailparse::test_mailparse_msg_get_part() {
  String msg =
    "Received: from mail pickup service by hotmail.com with Microsoft\n"
    "SMTPSVC;\n"
    "Sat, 18 Feb 2006 22:58:14 -0800\n"
    "Received: from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;\n"
    "Sun, 19 Feb 2006 06:58:13 +0000\n"
    "\n"
    "test";

  Variant mail = f_mailparse_msg_create();
  f_mailparse_msg_parse(mail, msg);
  Array arr = f_mailparse_msg_get_structure(mail);
  for (ArrayIter iter(arr); iter; ++iter) {
    Variant section = f_mailparse_msg_get_part(mail, iter.second());
    Variant info = f_mailparse_msg_get_part_data(section);
    Array received =
      CREATE_VECTOR2("from mail pickup service by hotmail.com with Microsoft",
                     "from 66.178.40.49 by BAY116-DAV8.phx.gbl with DAV;");
    VS(info,
       Array(ArrayInit(11).
             set(String("headers"), CREATE_MAP1("received", received)).
             set(String("starting-pos"), 0).
             set(String("starting-pos-body"), 200).
             set(String("ending-pos"), 200).
             set(String("ending-pos-body"), 200).
             set(String("line-count"), 6).
             set(String("body-line-count"), 0).
             set(String("charset"), "us-ascii").
             set(String("transfer-encoding"), "8bit").
             set(String("content-type"), "text/plain").
             set(String("content-base"), "/").
             create()));
  }

  return Count(true);
}
bool TestExtJson::test_json_decode() {
  Array arr = CREATE_MAP1("fbid", 101501853510151001LL);
  VS(f_json_decode(f_json_encode(arr), true), arr);

  VS(f_json_decode("{\"0\":{\"00\":0}}", true),
     CREATE_MAP1("0", CREATE_MAP1("00", 0)));

  VS(f_json_decode("{\"a\":1,\"b\":2.3,\"3\":\"test\"}", true),
     CREATE_MAP3("a", 1, "b", 2.3, 3, "test"));
  VS(f_json_decode("[\"a\",1,true,false,null]", true),
     CREATE_VECTOR5("a", 1, true, false, uninit_null()));

  Object obj = f_json_decode("{\"a\":1,\"b\":2.3,\"3\":\"test\"}");
  Object obj2(SystemLib::AllocStdClassObject());
  obj2->o_set("a", 1);
  obj2->o_set("b", 2.3);
  obj2->o_set("3", "test");
  VS(obj.toArray(), obj2.toArray());

  obj = f_json_decode("[\"a\",1,true,false,null]");
  VS(obj.toArray(), CREATE_VECTOR5("a", 1, true, false, uninit_null()));

  VS(f_json_decode("{z:1}",     true),       uninit_null());
  VS(f_json_decode("{z:1}",     true, k_JSON_FB_LOOSE), CREATE_MAP1("z", 1));
  VS(f_json_decode("{z:\"z\"}", true),       uninit_null());
  VS(f_json_decode("{z:\"z\"}", true, k_JSON_FB_LOOSE), CREATE_MAP1("z", "z"));
  VS(f_json_decode("{'x':1}",   true),       uninit_null());
  VS(f_json_decode("{'x':1}",   true, k_JSON_FB_LOOSE), CREATE_MAP1("x", 1));
  VS(f_json_decode("{y:1,}",    true),       uninit_null());
  VS(f_json_decode("{y:1,}",    true, k_JSON_FB_LOOSE), CREATE_MAP1("y", 1));
  VS(f_json_decode("{,}",       true),       uninit_null());
  VS(f_json_decode("{,}",       true, k_JSON_FB_LOOSE), uninit_null());
  VS(f_json_decode("[1,2,3,]",  true),       uninit_null());
  VS(f_json_decode("[1,2,3,]",  true, k_JSON_FB_LOOSE), CREATE_VECTOR3(1,2,3));
  VS(f_json_decode("[,]",       true),       uninit_null());
  VS(f_json_decode("[,]",       true, k_JSON_FB_LOOSE), uninit_null());
  VS(f_json_decode("[]",        true),       Array::Create());
  VS(f_json_decode("[]",        true, k_JSON_FB_LOOSE), Array::Create());
  VS(f_json_decode("{}",        true),       Array::Create());
  VS(f_json_decode("{}",        true, k_JSON_FB_LOOSE), Array::Create());

  VS(f_json_decode("[{\"a\":\"apple\"},{\"b\":\"banana\"}]", true),
     CREATE_VECTOR2(CREATE_MAP1("a", "apple"), CREATE_MAP1("b", "banana")));

  Variant a = "[{\"a\":[{\"n\":\"1st\"}]},{\"b\":[{\"n\":\"2nd\"}]}]";
  VS(f_json_decode(a, true),
     CREATE_VECTOR2
     (CREATE_MAP1("a", CREATE_VECTOR1(CREATE_MAP1("n", "1st"))),
      CREATE_MAP1("b", CREATE_VECTOR1(CREATE_MAP1("n", "2nd")))));

  return Count(true);
}
bool TestExtClass::test_get_class_constants() {
  Array constants = f_get_class_constants("test");
  VS(constants, CREATE_MAP1("const_foo", "f"));
  return Count(true);
}
bool TestExtProcess::test_pcntl_exec() {
  f_pcntl_exec("/bin/sh",
               CREATE_VECTOR1("test/test_pcntl_exec.sh"),
               CREATE_MAP1("name", "value"));
  return Count(true);
}
Exemple #19
0
bool TestExtClass::test_get_class_vars() {
  Array properties = f_get_class_vars("TEst");
  VS(properties, CREATE_MAP1("foo", null));
  return Count(true);
}
Exemple #20
0
bool TestExtPdo::test_pdo_sqlite() {
  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, 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, 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["id"], "1");

  } catch (Object &e) {
    VS(e, null);
  }

  CleanupSqliteTestTable();
  return Count(true);
}
Exemple #21
0
bool TestExtSoap::preTest() {
  m_server = p_SoapServer(NEW(c_SoapServer)());
  m_server->create(null, CREATE_MAP1("uri", "http://testuri.org"));
  return true;
}
bool TestCppBase::TestArray() {
  // Array::Create(), Array constructors and informational
  {
    Array arr;
    VERIFY(arr.empty()); VERIFY(arr.size() == 0); VERIFY(arr.length() == 0);
    VERIFY(arr.isNull());

    arr = Array::Create();
    VERIFY(arr.empty()); VERIFY(arr.size() == 0); VERIFY(arr.length() == 0);
    VERIFY(!arr.isNull());

    arr = Array::Create(0);
    VERIFY(!arr.empty()); VERIFY(arr.size() == 1); VERIFY(arr.length() == 1);
    VERIFY(!arr.isNull());
    VERIFY((int)arr[0] == 0);
    VS(arr, Array(ArrayInit(1).set(0).create()));

    arr = Array::Create("test");
    VERIFY(!arr.empty()); VERIFY(arr.size() == 1); VERIFY(arr.length() == 1);
    VERIFY(!arr.isNull());
    VERIFY(arr[0] == "test");
    VS(arr, Array(ArrayInit(1).set("test").create()));

    Array arrCopy = arr;
    arr = Array::Create(arr);
    VERIFY(!arr.empty()); VERIFY(arr.size() == 1); VERIFY(arr.length() == 1);
    VERIFY(!arr.isNull());
    VERIFY(arr[0].toArray().size() == 1);
    VS(arr[0], arrCopy);
    VS(arr, Array(ArrayInit(1).set(arrCopy).create()));

    arr = Array::Create("name", 1);
    VERIFY(!arr.empty()); VERIFY(arr.size() == 1); VERIFY(arr.length() == 1);
    VERIFY(!arr.isNull());
    VERIFY((int)arr[s_name] == 1);
    VS(arr, Array(ArrayInit(1).set(s_name, 1).create()));

    arr = Array::Create(s_name, "test");
    VERIFY(!arr.empty()); VERIFY(arr.size() == 1); VERIFY(arr.length() == 1);
    VERIFY(!arr.isNull());
    VERIFY(arr[s_name] == "test");
    VS(arr, Array(ArrayInit(1).set(s_name, "test").create()));

    arrCopy = arr;
    arr = Array::Create(s_name, arr);
    VERIFY(!arr.empty()); VERIFY(arr.size() == 1); VERIFY(arr.length() == 1);
    VERIFY(!arr.isNull());
    VS(arr[s_name], arrCopy);
    VERIFY(arr[s_name].toArray().size() == 1);
    VS(arr, Array(ArrayInit(1).set(s_name, arrCopy).create()));
  }

  // iteration
  {
    Array arr = CREATE_MAP2("n1", "v1", "n2", "v2");
    int i = 0;
    for (ArrayIter iter = arr.begin(); iter; ++iter, ++i) {
      if (i == 0) {
        VERIFY(iter.first() == "n1");
        VERIFY(iter.second() == "v1");
      } else {
        VERIFY(iter.first() == "n2");
        VERIFY(iter.second() == "v2");
      }
    }
    VERIFY(i == 2);
  }
  /* TODO: fix this
  {
    Variant arr = CREATE_MAP2("n1", "v1", "n2", "v2");
    arr.escalate();
    for (ArrayIterPtr iter = arr.begin(arr, true); !iter->end(); iter->next()){
      unset(arr.lvalAt(iter->first()));
    }
    VS(arr, Array::Create());
  }
  */

  // conversions
  {
    Array arr0;
    VERIFY(arr0.toBoolean() == false);
    VERIFY(arr0.toByte() == 0);
    VERIFY(arr0.toInt16() == 0);
    VERIFY(arr0.toInt32() == 0);
    VERIFY(arr0.toInt64() == 0);
    VERIFY(arr0.toDouble() == 0.0);
    VERIFY(arr0.toString() == "");

    Array arr1 = Array::Create("test");
    VERIFY(arr1.toBoolean() == true);
    VERIFY(arr1.toByte() == 1);
    VERIFY(arr1.toInt16() == 1);
    VERIFY(arr1.toInt32() == 1);
    VERIFY(arr1.toInt64() == 1);
    VERIFY(arr1.toDouble() == 1.0);
    VERIFY(arr1.toString() == "Array");
  }

  // offset
  {
    Array arr;
    arr.set(0, "v1");
    arr.set(1, "v2");
    VS(arr, CREATE_VECTOR2("v1", "v2"));
  }
  {
    Array arr;
    arr.set(s_n1, "v1");
    arr.set(s_n2, "v2");
    VS(arr, CREATE_MAP2("n1", "v1", "n2", "v2"));
  }
  {
    Array arr;
    arr.lvalAt(0) = String("v1");
    arr.lvalAt(1) = String("v2");
    VS(arr, CREATE_VECTOR2("v1", "v2"));
  }
  {
    Array arr;
    arr.lvalAt(s_n1) = String("v1");
    arr.lvalAt(s_n2) = String("v2");
    VS(arr, CREATE_MAP2("n1", "v1", "n2", "v2"));
  }
  {
    Array arr;
    Variant name = "name";
    arr.lvalAt(name) = String("value");
    VS(arr, CREATE_MAP1("name", "value"));
  }
  {
    Array arr;
    arr.lvalAt(s_A) = 10;
    arr.lvalAt(s_A)++;
    VS(arr[s_A], 11);
  }

  {
    Array arr;
    arr.lvalAt(1) = 10;
    VS(arr[1], 10);
    VS(arr[1.5], 10);
    VS(arr[Variant(1.5)], 10);
    VS(arr[s_1], 10);
    VS(arr[Variant("1")], 10);
  }
  {
    Array arr;
    arr.lvalAt(Variant(1.5)) = 10;
    VS(arr[1], 10);
    VS(arr[1.5], 10);
    VS(arr[Variant(1.5)], 10);
    VS(arr[s_1], 10);
    VS(arr[Variant("1")], 10);
  }
  {
    Array arr;
    arr.lvalAt(s_1) = 10;
    VS(arr[1], 10);
    VS(arr[1.5], 10);
    VS(arr[Variant(1.5)], 10);
    VS(arr[s_1], 10);
    VS(arr[Variant("1")], 10);
  }
  {
    Array arr;
    arr.lvalAt(Variant("1")) = 10;
    VS(arr[1], 10);
    VS(arr[1.5], 10);
    VS(arr[Variant(1.5)], 10);
    VS(arr[s_1], 10);
    VS(arr[Variant("1")], 10);
  }

  // membership
  {
    Array arr;
    arr.lvalAt(0) = String("v1");
    arr.lvalAt(1) = String("v2");
    VERIFY(arr.exists(0));
    arr.remove(0);
    VERIFY(!arr.exists(0));
    VS(arr, Array::Create(1, "v2"));
    arr.append("v3");
    VS(arr, CREATE_MAP2(1, "v2", 2, "v3"));
  }
  {
    static const StaticString s_0("0");
    Array arr;
    arr.lvalAt(0) = String("v1");
    VERIFY(arr.exists(0));
    arr.remove(String(s_0));
    VERIFY(!arr.exists(0));
  }
  {
    Array arr;
    arr.lvalAt(0) = String("v1");
    VERIFY(arr.exists(0));
    arr.remove(Variant("0"));
    VERIFY(!arr.exists(0));
  }
  {
    Array arr;
    arr.lvalAt(0) = String("v1");
    VERIFY(arr.exists(0));
    arr.remove(Variant(Variant("0")));
    VERIFY(!arr.exists(0));
  }
  {
    Array arr;
    arr.lvalAt(0) = String("v1");
    VERIFY(arr.exists(0));
    arr.remove(Variant(Variant(0.5)));
    VERIFY(!arr.exists(0));
  }
  {
    Array arr;
    arr.lvalAt(Variant()) = 123;
    VERIFY(arr.exists(empty_string));
    arr.remove(Variant());
    VERIFY(!arr.exists(empty_string));
  }
  {
    Array arr;
    arr.lvalAt(s_n1) = String("v1");
    arr.lvalAt(s_n2) = String("v2");
    VERIFY(arr.exists(s_n1));
    arr.remove(s_n1);
    VERIFY(!arr.exists(s_n1));
    VS(arr, Array::Create(s_n2, "v2"));
    arr.append("v3");
    VS(arr, CREATE_MAP2("n2", "v2", 0, "v3"));
  }
  {
    Array arr;
    arr.lvalAt() = String("test");
    VS(arr, CREATE_VECTOR1("test"));
  }
  {
    Array arr;
    arr.lvalAt(s_name) = String("value");
    VERIFY(arr.exists(s_name));
  }
  {
    Array arr;
    arr.lvalAt(1) = String("value");
    VERIFY(arr.exists(1));
    VERIFY(arr.exists(1.5));
    VERIFY(arr.exists(s_1));
    VERIFY(arr.exists(Variant("1")));
    VERIFY(arr.exists(Variant(1)));
    VERIFY(arr.exists(Variant(1.5)));
  }
  {
    Array arr;
    arr.lvalAt(s_1) = String("value");
    VERIFY(arr.exists(1));
    VERIFY(arr.exists(1.5));
    VERIFY(arr.exists(s_1));
    VERIFY(arr.exists(Variant("1")));
    VERIFY(arr.exists(Variant(1)));
    VERIFY(arr.exists(Variant(1.5)));
  }
  {
    Array arr;
    arr.lvalAt(1.5) = String("value");
    VERIFY(arr.exists(1));
    VERIFY(arr.exists(1.5));
    VERIFY(arr.exists(s_1));
    VERIFY(arr.exists(Variant("1")));
    VERIFY(arr.exists(Variant(1)));
    VERIFY(arr.exists(Variant(1.5)));
  }
  {
    Array arr;
    arr.lvalAt(Variant(1.5)) = String("value");
    VERIFY(arr.exists(1));
    VERIFY(arr.exists(1.5));
    VERIFY(arr.exists(s_1));
    VERIFY(arr.exists(Variant("1")));
    VERIFY(arr.exists(Variant(1)));
    VERIFY(arr.exists(Variant(1.5)));
  }
  {
    Array arr;
    arr.lvalAt(Variant("1")) = String("value");
    VERIFY(arr.exists(1));
    VERIFY(arr.exists(1.5));
    VERIFY(arr.exists(s_1));
    VERIFY(arr.exists(Variant("1")));
    VERIFY(arr.exists(Variant(1)));
    VERIFY(arr.exists(Variant(1.5)));
  }

  // merge
  {
    Array arr = Array::Create(0) + Array::Create(1);
    VS(arr, Array::Create(0));
    arr += CREATE_VECTOR2(0, 1);
    VS(arr, CREATE_VECTOR2(0, 1));

    arr = Array::Create(0).merge(Array::Create(1));
    VS(arr, CREATE_VECTOR2(0, 1));
    arr = arr.merge(CREATE_VECTOR2(0, 1));
    VS(arr, CREATE_VECTOR4(0, 1, 0, 1));

    arr = Array::Create("s0").merge(Array::Create("s1"));
    VS(arr, CREATE_VECTOR2("s0", "s1"));

    arr = Array::Create("n0", "s0") + Array::Create("n1", "s1");
    VS(arr, CREATE_MAP2("n0", "s0", "n1", "s1"));
    arr += CREATE_MAP2("n0", "s0", "n1", "s1");
    VS(arr, CREATE_MAP2("n0", "s0", "n1", "s1"));

    arr = Array::Create("n0", "s0").merge(Array::Create("n1", "s1"));
    VS(arr, CREATE_MAP2("n0", "s0", "n1", "s1"));
    Array arrX = CREATE_MAP2("n0", "s2", "n1", "s3");
    arr = arr.merge(arrX);
    VS(arr, CREATE_MAP2("n0", "s2", "n1", "s3"));
  }

  // slice
  {
    Array arr = CREATE_VECTOR2("test1", "test2");
    Array sub = arr.slice(1, 1, true);
    VS(sub, CREATE_MAP1(1, "test2"));
  }
  {
    Array arr = CREATE_VECTOR2("test1", "test2");
    Array sub = arr.slice(1, 1, false);
    VS(sub, CREATE_VECTOR1("test2"));
  }
  {
    Array arr = CREATE_MAP2("n1", "test1", "n2", "test2");
    Array sub = arr.slice(1, 1, true);
    VS(sub, CREATE_MAP1("n2", "test2"));
  }
  {
    Array arr = CREATE_MAP2("n1", "test1", "n2", "test2");
    Array sub = arr.slice(1, 1, false);
    VS(sub, CREATE_MAP1("n2", "test2"));
  }

  // escalation
  {
    Array arr;
    lval(arr.lvalAt(0)).lvalAt(0) = 1.2;
    VS(arr, CREATE_VECTOR1(CREATE_VECTOR1(1.2)));
  }
  {
    Array arr;
    lval(arr.lvalAt(s_name)).lvalAt(0) = 1.2;
    VS(arr, CREATE_MAP1(s_name, CREATE_VECTOR1(1.2)));
  }
  {
    Array arr = Array::Create();
    lval(arr.lvalAt(0)).lvalAt(0) = 1.2;
    VS(arr, CREATE_VECTOR1(CREATE_VECTOR1(1.2)));
  }
  {
    Array arr = Array::Create();
    lval(arr.lvalAt(s_name)).lvalAt(0) = 1.2;
    VS(arr, CREATE_MAP1(s_name, CREATE_VECTOR1(1.2)));
  }
  {
    Array arr = Array::Create("test");
    arr.lvalAt(0) = CREATE_VECTOR1(1.2);
    VS(arr, CREATE_VECTOR1(CREATE_VECTOR1(1.2)));
  }
  {
    Array arr = Array::Create("test");
    lval(arr.lvalAt(s_name)).lvalAt(0) = 1.2;
    VS(arr, CREATE_MAP2(0, "test", s_name, CREATE_VECTOR1(1.2)));
  }
  {
    Array arr = Array::Create();
    arr.append("apple");
    arr.set(2, "pear");
    VS(arr[2], "pear");
  }

  {
    Array arr = CREATE_MAP2(0, "a", 1, "b");
    VERIFY(arr->isVectorData());
  }
  {
    Array arr = CREATE_MAP2(1, "a", 0, "b");
    VERIFY(!arr->isVectorData());
  }
  {
    Array arr = CREATE_MAP2(1, "a", 2, "b");
    VERIFY(!arr->isVectorData());
  }
  {
    Array arr = CREATE_MAP1(1, "a");
    arr.set(0, "b");
    VERIFY(!arr->isVectorData());
  }

  return Count(true);
}
Exemple #23
0
bool TestExtSoap::preTest() {
  m_server = p_SoapServer(NEWOBJ(c_SoapServer)());
  m_server->t___construct(null, CREATE_MAP1("uri", "http://testuri.org"));
  return true;
}
bool TestExtSqlite3::test_sqlite3() {
  p_SQLite3 db(NEWOBJ(c_SQLite3)());
  db->t_open(":memory:test");
  db->t_exec("DROP TABLE foo");
  db->t_exec("CREATE TABLE foo (bar STRING)");

  db->t_exec("INSERT INTO foo VALUES ('ABC')");
  db->t_exec("INSERT INTO foo VALUES ('DEF')");
  VS(db->t_lastinsertrowid(), 2);
  VS(db->t_changes(), 1);
  VS(db->t_lasterrorcode(), 0);
  VS(db->t_lasterrormsg(), "not an error");

  VS(db->t_escapestring("'\""), "''\"");
  VS(db->t_querysingle("SELECT * FROM foo"), "ABC");
  VS(db->t_querysingle("SELECT * FROM foo", true), CREATE_MAP1("bar", "ABC"));

  // testing query() and SQLite3Result
  {
    Object objResult = db->t_query("SELECT * FROM foo").toObject();
    c_SQLite3Result *res = objResult.getTyped<c_SQLite3Result>();

    VS(res->t_fetcharray(), CREATE_MAP2(0, "ABC", "bar", "ABC"));
    VS(res->t_numcolumns(), 1);
    VS(res->t_columnname(0), "bar");
    VS(res->t_columntype(0), k_SQLITE3_TEXT);

    VS(res->t_fetcharray(k_SQLITE3_NUM), CREATE_VECTOR1("DEF"));
  }

  // testing prepare() and sqlite3stmt
  {
    Object objStmt = db->t_prepare("SELECT * FROM foo WHERE bar = :id");
    c_SQLite3Stmt *stmt = objStmt.getTyped<c_SQLite3Stmt>();
    VS(stmt->t_paramcount(), 1);

    Variant id = "DEF";
    VERIFY(stmt->t_bindvalue(":id", id, SQLITE3_TEXT));
    id = "ABC";
    {
      Object objResult = stmt->t_execute();
      c_SQLite3Result *res = objResult.getTyped<c_SQLite3Result>();
      VS(res->t_fetcharray(k_SQLITE3_NUM), CREATE_VECTOR1("DEF"));
    }

    VERIFY(stmt->t_clear());
    VERIFY(stmt->t_reset());
    id = "DEF";
    VERIFY(stmt->t_bindparam(":id", ref(id), SQLITE3_TEXT));
    id = "ABC";
    {
      Object objResult = stmt->t_execute();
      c_SQLite3Result *res = objResult.getTyped<c_SQLite3Result>();
      VS(res->t_fetcharray(k_SQLITE3_NUM), CREATE_VECTOR1("ABC"));
    }
  }

  // testing UDF
  {
    VERIFY(db->t_createfunction("tolower", "lower", 1));
    Object objResult = db->t_query("SELECT tolower(bar) FROM foo").toObject();
    c_SQLite3Result *res = objResult.getTyped<c_SQLite3Result>();
    VS(res->t_fetcharray(k_SQLITE3_NUM), CREATE_VECTOR1("abc"));
  }
  {
    VERIFY(db->t_createaggregate("sumlen", "sumlen_step", "sumlen_fini", 1));
    Object objResult = db->t_query("SELECT sumlen(bar) FROM foo").toObject();
    c_SQLite3Result *res = objResult.getTyped<c_SQLite3Result>();
    VS(res->t_fetcharray(k_SQLITE3_NUM), CREATE_VECTOR1(6));
  }

  db->t_close();

  VS(db->t_version(),
     CREATE_MAP2("versionString", "3.6.23.1", "versionNumber", 3006023));
  f_unlink(":memory:test");
  return Count(true);
}
bool TestCppBase::TestVariant() {
  // operators
  {
    Variant v(15);
    v += 20;
    VERIFY(v.isNumeric());
    VERIFY(v.is(KindOfInt64));
    VERIFY(v == Variant(35));
  }

  // conversions
  {
    Variant v("123");
    VERIFY(v.toInt32() == 123);
  }

  // offset
  {
    Variant v = "test";
    VS(v.rvalAt(0), "t");
  }
  {
    Variant v;
    v.lvalAt(0) = String("v0");
    v.lvalAt(1) = String("v1");
    VERIFY(v[0] == "v0");
    VERIFY(v[1] == "v1");
  }
  {
    Variant v;
    v.lvalAt() = String("test");
    VS(v, CREATE_VECTOR1("test"));
  }
  {
    Variant v;
    v.lvalAt(1) = String("test");
    VS(v[1], "test");
    VS(v[1.5], "test");
    VS(v[Variant(1.5)], "test");
    VS(v[s_1], "test");
    VS(v[Variant("1")], "test");
  }
  {
    Variant v;
    v.lvalAt(Variant(1.5)) = String("test");
    VS(v[1], "test");
    VS(v[1.5], "test");
    VS(v[Variant(1.5)], "test");
    VS(v[s_1], "test");
    VS(v[Variant("1")], "test");
  }
  {
    Variant v;
    v.lvalAt(s_1) = String("test");
    VS(v[1], "test");
    VS(v[1.5], "test");
    VS(v[Variant(1.5)], "test");
    VS(v[s_1], "test");
    VS(v[Variant("1")], "test");
  }
  {
    Variant v;
    v.lvalAt(Variant("1")) = String("test");
    VS(v[1], "test");
    VS(v[1.5], "test");
    VS(v[Variant(1.5)], "test");
    VS(v[s_1], "test");
    VS(v[Variant("1")], "test");
  }

  // membership
  {
    Variant v;
    v.lvalAt(s_n0) = String("v0");
    v.lvalAt(s_n1) = String("v1");
    v.remove(s_n1);
    VS(v, CREATE_MAP1(s_n0, "v0"));
    v.append("v2");
    VS(v, CREATE_MAP2(s_n0, "v0", 0, "v2"));
  }
  {
    Variant v;
    v.lvalAt(s_n0) = String("v0");
    v.lvalAt(1) = String("v1");
    v.remove(Variant(1.5));
    VS(v, CREATE_MAP1("n0", "v0"));
  }
  {
    Variant v;
    v.lvalAt(s_n0) = String("v0");
    v.lvalAt(1) = String("v1");
    v.remove(Variant("1"));
    VS(v, CREATE_MAP1("n0", "v0"));
  }
  {
    Variant v;
    v.lvalAt(s_n0) = String("v0");
    v.lvalAt(1) = String("v1");
    v.remove(String("1"));
    VS(v, CREATE_MAP1("n0", "v0"));
  }
  {
    Variant v;
    v.lvalAt(s_n0) = String("v0");
    v.lvalAt(empty_string) = String("v1");
    v.remove(Variant());
    VS(v, CREATE_MAP1("n0", "v0"));
  }

  // references
  {
    Variant v1("original");
    Variant v2 = v1;
    v2 = String("changed");
    VERIFY(v1 == "original");
  }
  {
    Variant v1("original");
    Variant v2 = strongBind(v1);
    v2 = String("changed");
    VERIFY(v1 == "changed");
  }
  {
    Variant v1 = 10;
    Variant v2 = Array(ArrayInit(1).setRef(v1).create());
    v1 = 20;
    VS(v2[0], 20);
  }
  {
    Variant v1 = 10;
    Variant v2;
    v2.lvalAt() = ref(v1);
    v1 = 20;
    VS(v2[0], 20);
  }
  {
    Variant v1 = 10;
    Variant v2 = CREATE_VECTOR1(5);
    v2.lvalAt() = ref(v1);
    v1 = 20;
    VS(v2[1], 20);
  }
  {
    Variant v1 = 10;
    Variant v2 = strongBind(v1);
    v2++;
    VS(v2, 11);
    VS(v1, 11);
  }
  {
    Variant arr = CREATE_VECTOR2(1, 2);
    Variant v;
    for (MutableArrayIter iter = arr.begin(nullptr, v); iter.advance();) {
      v++;
    }
    VS(arr, CREATE_VECTOR2(2, 3));
  }

  // array escalation
  {
    Variant arr;
    lval(arr.lvalAt(0)).lvalAt(0) = 1.2;
    VS(arr, CREATE_VECTOR1(CREATE_VECTOR1(1.2)));
  }
  {
    Variant arr;
    lval(arr.lvalAt(s_name)).lvalAt(0) = 1.2;
    VS(arr, CREATE_MAP1(s_name, CREATE_VECTOR1(1.2)));
  }
  {
    Variant arr = Array::Create();
    lval(arr.lvalAt(0)).lvalAt(0) = 1.2;
    VS(arr, CREATE_VECTOR1(CREATE_VECTOR1(1.2)));
  }
  {
    Variant arr = Array::Create();
    lval(arr.lvalAt(s_name)).lvalAt(0) = 1.2;
    VS(arr, CREATE_MAP1(s_name, CREATE_VECTOR1(1.2)));
  }
  {
    Variant arr = Array::Create("test");
    arr.lvalAt(0) = CREATE_VECTOR1(1.2);
    VS(arr, CREATE_VECTOR1(CREATE_VECTOR1(1.2)));
  }
  {
    Variant arr = Array::Create("test");
    lval(arr.lvalAt(s_name)).lvalAt(0) = 1.2;
    VS(arr, CREATE_MAP2(0, "test", s_name, CREATE_VECTOR1(1.2)));
  }

  return Count(true);
}