From 708a22fae15b13db871ca5ffc73004c70f0584fe Mon Sep 17 00:00:00 2001 From: valmat Date: Fri, 28 Mar 2014 19:00:17 +0600 Subject: Changed the structure of the test file. With the increasing number of tests the old structure became uncomfortable. --- tests/cpp/include/Classes_and_objects.h | 199 ----------- tests/cpp/include/ValueIterator.h | 54 --- tests/cpp/include/bool2str.h | 12 + tests/cpp/include/class_obj/001-002.h | 66 ++++ tests/cpp/include/class_obj/003-comparable.h | 74 +++++ tests/cpp/include/class_obj/004-static-funct.h | 90 +++++ tests/cpp/include/class_obj/tpl.h | 25 ++ tests/cpp/include/valueiterator/001-006.h | 27 ++ tests/cpp/include/valueiterator/007.h | 44 +++ tests/cpp/include/variables.h | 366 --------------------- tests/cpp/include/variables/001-process_globals.h | 57 ++++ .../cpp/include/variables/002-get_complex_array.h | 35 ++ tests/cpp/include/variables/003-value-types.h | 39 +++ .../include/variables/004-store-scalar-variables.h | 48 +++ .../variables/005-cast-objects-to-scalars.h | 75 +++++ tests/cpp/include/variables/006-casting-obj2str.h | 30 ++ .../include/variables/007-overloaded-operators.h | 46 +++ tests/cpp/include/variables/008-value-arrays.h | 57 ++++ tests/cpp/include/variables/009-010-value-object.h | 68 ++++ .../variables/011-012-value-casting-operators.h | 52 +++ tests/cpp/include/variables/tpl.h | 30 ++ 21 files changed, 875 insertions(+), 619 deletions(-) delete mode 100644 tests/cpp/include/Classes_and_objects.h delete mode 100644 tests/cpp/include/ValueIterator.h create mode 100644 tests/cpp/include/bool2str.h create mode 100644 tests/cpp/include/class_obj/001-002.h create mode 100644 tests/cpp/include/class_obj/003-comparable.h create mode 100644 tests/cpp/include/class_obj/004-static-funct.h create mode 100644 tests/cpp/include/class_obj/tpl.h create mode 100644 tests/cpp/include/valueiterator/001-006.h create mode 100644 tests/cpp/include/valueiterator/007.h delete mode 100644 tests/cpp/include/variables.h create mode 100644 tests/cpp/include/variables/001-process_globals.h create mode 100644 tests/cpp/include/variables/002-get_complex_array.h create mode 100644 tests/cpp/include/variables/003-value-types.h create mode 100644 tests/cpp/include/variables/004-store-scalar-variables.h create mode 100644 tests/cpp/include/variables/005-cast-objects-to-scalars.h create mode 100644 tests/cpp/include/variables/006-casting-obj2str.h create mode 100644 tests/cpp/include/variables/007-overloaded-operators.h create mode 100644 tests/cpp/include/variables/008-value-arrays.h create mode 100644 tests/cpp/include/variables/009-010-value-object.h create mode 100644 tests/cpp/include/variables/011-012-value-casting-operators.h create mode 100644 tests/cpp/include/variables/tpl.h (limited to 'tests/cpp/include') diff --git a/tests/cpp/include/Classes_and_objects.h b/tests/cpp/include/Classes_and_objects.h deleted file mode 100644 index 0977da9..0000000 --- a/tests/cpp/include/Classes_and_objects.h +++ /dev/null @@ -1,199 +0,0 @@ -/** - * - * - * Classes_and_objects.h - * - */ - - -/** - * Set up namespace - */ -namespace TestBaseClass { - - - - class MyCustomClass : public Php::Base, public Php::Countable - { - private: - int _x = 3; - - public: - MyCustomClass() - { - std::cerr << "MyCustomClass::MyCustomClass()" << std::endl; - } - - MyCustomClass(int value) : _x(value) - { - std::cerr << "MyCustomClass::MyCustomClass(" << value << ")" << std::endl; - } - - MyCustomClass(const MyCustomClass &that) - { - //std::cerr << "MyCustomClass::MyCustomClass copy constructor" << std::endl; - } - - virtual ~MyCustomClass() - { - std::cerr << "MyCustomClass::~MyCustomClass" << std::endl; - } - - virtual long int count() override - { - return 33; - } - - Php::Value myMethod(Php::Parameters ¶ms) - { - // check number of parameters - //if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied"); - - Php::out << "myMethod is called for object " << _x << std::endl; - - return 5; - - } - }; - - - /** - * Test custom comparison operator - */ - class Comparable : public Php::Base - { - private: - /** - * Internal value of the class - * @var int - */ - static int count; - int _nom; - int _value; - - public: - /** - * C++ constructor - */ - Comparable() - { - // start with random value - //_value = rand(); - _nom = ++count; - _value = _nom%2+1; - } - - /** - * C++ destructor - */ - virtual ~Comparable() {} - - /** - * Cast the object to a string - * @return std::string - */ - std::string __toString() - { - return "Obj#" + std::to_string(_nom) + "(" + std::to_string(_value) + ")"; - } - - /** - * Compare with a different object - * @param that - * @return int - */ - int __compare(const Comparable &that) const - { - return _value - that._value; - } - }; - int Comparable::count = 0; - - - /** - * Begin test static functions - */ - - - /** - * Regular function - * - * Because a regular function does not have a 'this' pointer, - * it has the same signature as static methods - * - * @param params Parameters passed to the function - */ - void testStaticRegFunc(Php::Parameters ¶ms) - { - Php::out << "testStatic regular function"<< std::endl; - } - - /** - * A very simple class that will not be exported to PHP - */ - class testStaticPrivClass - { - public: - /** - * C++ constructor and destructor - */ - testStaticPrivClass() {} - virtual ~testStaticPrivClass() {} - - /** - * Static method - * - * A static method also has no 'this' pointer and has - * therefore a signature identical to regular functions - * - * @param params Parameters passed to the method - */ - static void staticMethod(Php::Parameters ¶ms) - { - Php::out << "testStaticPrivClass::staticMethod()"<< std::endl; - } - }; - - /** - * A very simple class that will be exported to PHP - */ - class testStaticPubClass : public Php::Base - { - public: - /** - * C++ constructor and destructor - */ - testStaticPubClass() {} - virtual ~testStaticPubClass() {} - - /** - * Another static method - * - * This static has exactly the same signature as the - * regular function and static method that were mentioned - * before - * - * @param params Parameters passed to the method - */ - static void staticMethod(Php::Parameters ¶ms) - { - Php::out << "testStaticPubClass::staticMethod()"<< std::endl; - } - }; - - - /** - * End test static functions - */ - - - - - - -/** - * End of namespace - */ -} - - \ No newline at end of file diff --git a/tests/cpp/include/ValueIterator.h b/tests/cpp/include/ValueIterator.h deleted file mode 100644 index 689797a..0000000 --- a/tests/cpp/include/ValueIterator.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * - * TestValueIterator - * - */ - -/** - * Set up namespace - */ -namespace TestValueIterator { - - - void loopValue(Php::Parameters ¶ms) - { - std::cout << "Array/Object contains " << params[0].size() << " items" << std::endl; - for (auto it=params[0].begin(), itend = params[0].end(); it != itend; ++it) { - std::cout << "["<< it->first << "]="<< it->second << std::endl; - //std::cout << "["<< it->key() << "]="<< it->value() << std::endl; - } - return; - } - - void loopArray(void) - { - - Php::Value value; - /* - If we fill the array in this form, we get the following: - *** Error in `/usr/bin/php': double free or corruption (fasttop): 0x0000000001956d60 *** - value[0] = "val0"; - value[1] = "val1"; - value["third"] = "val3"; - value["fourth"] = "val3"; - */ - value.set(0 , "val0"); - value.set(1 , "val1"); - value.set("third" , "val3"); - value.set("fourth", "val3"); - - std::cout << "Array/Object contains " << value.size() << " items" << std::endl; - // assum the value variable holds an array or object, it then - // is possible to iterator over the values or properties - for (auto &iter : value) - { - // output key and value - Php::out << "["<< iter.first << "]="<< iter.second << std::endl; - } - } - -/** - * End of namespace - */ -} - diff --git a/tests/cpp/include/bool2str.h b/tests/cpp/include/bool2str.h new file mode 100644 index 0000000..26aac80 --- /dev/null +++ b/tests/cpp/include/bool2str.h @@ -0,0 +1,12 @@ +/** + * + * bool -> string + * + */ + + + +std::string bool2str(bool b) +{ + return b ? "Yes" : "No"; +} diff --git a/tests/cpp/include/class_obj/001-002.h b/tests/cpp/include/class_obj/001-002.h new file mode 100644 index 0000000..2a3b4df --- /dev/null +++ b/tests/cpp/include/class_obj/001-002.h @@ -0,0 +1,66 @@ +/** + * + * Test Classes and objects + * 001.phpt + * 002.phpt + * + */ + + + + +/** + * Set up namespace + */ +namespace TestBaseClass { + + + class MyCustomClass : public Php::Base, public Php::Countable + { + private: + int _x = 3; + + public: + MyCustomClass() + { + std::cerr << "MyCustomClass::MyCustomClass()" << std::endl; + } + + MyCustomClass(int value) : _x(value) + { + std::cerr << "MyCustomClass::MyCustomClass(" << value << ")" << std::endl; + } + + MyCustomClass(const MyCustomClass &that) + { + //std::cerr << "MyCustomClass::MyCustomClass copy constructor" << std::endl; + } + + virtual ~MyCustomClass() + { + std::cerr << "MyCustomClass::~MyCustomClass" << std::endl; + } + + virtual long int count() override + { + return 33; + } + + Php::Value myMethod(Php::Parameters ¶ms) + { + // check number of parameters + //if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied"); + + Php::out << "myMethod is called for object " << _x << std::endl; + + return 5; + + } + }; + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/class_obj/003-comparable.h b/tests/cpp/include/class_obj/003-comparable.h new file mode 100644 index 0000000..8a384dc --- /dev/null +++ b/tests/cpp/include/class_obj/003-comparable.h @@ -0,0 +1,74 @@ +/** + * + * Test Classes and objects + * 003-comparable.phpt + * + */ + + + + +/** + * Set up namespace + */ +namespace TestBaseClass { + + + /** + * Test custom comparison operator + */ + class Comparable : public Php::Base + { + private: + /** + * Internal value of the class + * @var int + */ + static int count; + int _nom; + int _value; + + public: + /** + * C++ constructor + */ + Comparable() + { + // start with random value + //_value = rand(); + _nom = ++count; + _value = _nom%2+1; + } + + /** + * C++ destructor + */ + virtual ~Comparable() {} + + /** + * Cast the object to a string + * @return std::string + */ + std::string __toString() + { + return "Obj#" + std::to_string(_nom) + "(" + std::to_string(_value) + ")"; + } + + /** + * Compare with a different object + * @param that + * @return int + */ + int __compare(const Comparable &that) const + { + return _value - that._value; + } + }; + int Comparable::count = 0; + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/class_obj/004-static-funct.h b/tests/cpp/include/class_obj/004-static-funct.h new file mode 100644 index 0000000..d6816ab --- /dev/null +++ b/tests/cpp/include/class_obj/004-static-funct.h @@ -0,0 +1,90 @@ +/** + * + * Test Classes and objects + * 004-static-funct.phpt + * test static functions + * + */ + + + + +/** + * Set up namespace + */ +namespace TestBaseClass { + + + /** + * Regular function + * + * Because a regular function does not have a 'this' pointer, + * it has the same signature as static methods + * + * @param params Parameters passed to the function + */ + void testStaticRegFunc(Php::Parameters ¶ms) + { + Php::out << "testStatic regular function"<< std::endl; + } + + /** + * A very simple class that will not be exported to PHP + */ + class testStaticPrivClass + { + public: + /** + * C++ constructor and destructor + */ + testStaticPrivClass() {} + virtual ~testStaticPrivClass() {} + + /** + * Static method + * + * A static method also has no 'this' pointer and has + * therefore a signature identical to regular functions + * + * @param params Parameters passed to the method + */ + static void staticMethod(Php::Parameters ¶ms) + { + Php::out << "testStaticPrivClass::staticMethod()"<< std::endl; + } + }; + + /** + * A very simple class that will be exported to PHP + */ + class testStaticPubClass : public Php::Base + { + public: + /** + * C++ constructor and destructor + */ + testStaticPubClass() {} + virtual ~testStaticPubClass() {} + + /** + * Another static method + * + * This static has exactly the same signature as the + * regular function and static method that were mentioned + * before + * + * @param params Parameters passed to the method + */ + static void staticMethod(Php::Parameters ¶ms) + { + Php::out << "testStaticPubClass::staticMethod()"<< std::endl; + } + }; + + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/class_obj/tpl.h b/tests/cpp/include/class_obj/tpl.h new file mode 100644 index 0000000..7dfdcfc --- /dev/null +++ b/tests/cpp/include/class_obj/tpl.h @@ -0,0 +1,25 @@ +/** + * + * Test Classes and objects + * phptname.phpt + * + */ + + + + +/** + * Set up namespace + */ +namespace TestBaseClass { + + + + + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/valueiterator/001-006.h b/tests/cpp/include/valueiterator/001-006.h new file mode 100644 index 0000000..0dc6778 --- /dev/null +++ b/tests/cpp/include/valueiterator/001-006.h @@ -0,0 +1,27 @@ +/** + * + * TestValueIterator + * test valueiterator/001.phpt-valueiterator/006.phpt + * + */ + +/** + * Set up namespace + */ +namespace TestValueIterator { + + + void loopValue(Php::Parameters ¶ms) + { + std::cout << "Array/Object contains " << params[0].size() << " items" << std::endl; + for (auto it=params[0].begin(), itend = params[0].end(); it != itend; ++it) { + std::cout << "["<< it->first << "]="<< it->second << std::endl; + //std::cout << "["<< it->key() << "]="<< it->value() << std::endl; + } + return; + } +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/valueiterator/007.h b/tests/cpp/include/valueiterator/007.h new file mode 100644 index 0000000..3cfcb59 --- /dev/null +++ b/tests/cpp/include/valueiterator/007.h @@ -0,0 +1,44 @@ +/** + * + * TestValueIterator + * test valueiterator/007.phpt + * + */ + +/** + * Set up namespace + */ +namespace TestValueIterator { + + void loopArray(void) + { + + Php::Value value; + /* + If we fill the array in this form, we get the following: + *** Error in `/usr/bin/php': double free or corruption (fasttop): 0x0000000001956d60 *** + value[0] = "val0"; + value[1] = "val1"; + value["third"] = "val3"; + value["fourth"] = "val3"; + */ + value.set(0 , "val0"); + value.set(1 , "val1"); + value.set("third" , "val3"); + value.set("fourth", "val3"); + + std::cout << "Array/Object contains " << value.size() << " items" << std::endl; + // assum the value variable holds an array or object, it then + // is possible to iterator over the values or properties + for (auto &iter : value) + { + // output key and value + Php::out << "["<< iter.first << "]="<< iter.second << std::endl; + } + } + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables.h b/tests/cpp/include/variables.h deleted file mode 100644 index e8cb526..0000000 --- a/tests/cpp/include/variables.h +++ /dev/null @@ -1,366 +0,0 @@ -/** - * - * Test variables - * - */ - - #include "doubl2str.h" - -/** - * Set up namespace - */ -namespace TestVariables { - - - /** - * process_globals() - * - * This function reads and modifies global variables - */ - Php::Value process_globals() - { - // all global variables can be accessed via the Php::GLOBALS variable, - // which is more or less the same as the PHP $_GLOBALS variable - - // set a global variable - Php::GLOBALS["a"] = 1; - - // increment a global variable - Php::GLOBALS["b"] += 1; - - // set a global variable to be an array - Php::GLOBALS["c"] = Php::Array(); - - // add a member to an array - Php::GLOBALS["c"]["member"] = 123; - - // and increment it - Php::GLOBALS["c"]["member"] += 77; - - // change value e - Php::GLOBALS["e"] = Php::GLOBALS["e"][0]("hello"); - - // if a global variable holds a function, we can call it - return Php::GLOBALS["d"](1,2,3); - } - - /** - * This function returns complex array - */ - Php::Value get_complex_array() - { - Php::Value r; - r["a"] = 123; - r["b"] = 456; - r["c"][0] = "nested value"; - r["c"][1] = "example"; - r["c"][2] = 7; - return r; - } - - - std::string bool2str(bool b) - { - return b ? "Yes" : "No"; - } - - /* - * Check type of value - * @param array - */ - void value_types(Php::Parameters ¶ms) { - if (params.size() == 0) { - return; - } - - Php::Value arr = params[0]; - - Php::out << "Null: " << bool2str( arr.get("Null").type() == Php::Type::Null ) << std::endl; - Php::out << "Numeric: " << bool2str( arr.get("Numeric").type() == Php::Type::Numeric ) << std::endl; - Php::out << "Float: " << bool2str( arr.get("Float").type() == Php::Type::Float ) << std::endl; - Php::out << "Bool: " << bool2str( arr.get("Bool").type() == Php::Type::Bool ) << std::endl; - Php::out << "Array: " << bool2str( arr.get("Array").type() == Php::Type::Array ) << std::endl; - Php::out << "Object: " << bool2str( arr.get("Object").type() == Php::Type::Object ) << std::endl; - Php::out << "String: " << bool2str( arr.get("String").type() == Php::Type::String ) << std::endl; - Php::out << "Resource: " << bool2str( arr.get("Resource").type() == Php::Type::Resource ) << std::endl; - Php::out << "Constant: " << bool2str( arr.get("Constant").type() == Php::Type::Constant ) << std::endl; - Php::out << "ConstantArray: " << bool2str( arr.get("ConstantArray").type() == Php::Type::ConstantArray ) << std::endl; - Php::out << "Callable1: " << bool2str( arr.get("Callable1").type() == Php::Type::Callable ) << std::endl; - Php::out << "Callable2: " << bool2str( arr.get("Callable2").type() == Php::Type::Callable ) << std::endl; - Php::out << "Callable3: " << bool2str( arr.get("Callable3").type() == Php::Type::Callable ) << std::endl; - Php::out << "Callable4: " << bool2str( arr.get("Callable4").type() == Php::Type::Callable ) << std::endl; - } - - /* - * Test variables defined in PHP-CPP - */ - Php::Value scalar_store(void) { - - Php::Value value1 = 1234; - Php::Value value2 = "this is a string"; - Php::Value value3 = std::string("another string"); - Php::Value value4 = nullptr; - Php::Value value5 = 123.45; - Php::Value value6 = true; - - Php::Value r; - r[0] = value1; - r[1] = value2; - r[2] = value3; - r[3] = value4; - r[4] = value5; - r[5] = value6; - - r[6] = 1234; - r[7] = "this is a string"; - r[8] = std::string("another string"); - r[9] = nullptr; - r[10] = Php::Value(); - r[11] = 123.45; - r[12] = false; - - return r; - } - - - /* - * Test Php::Value casting operators - */ - void value_casting(Php::Parameters ¶ms) - { - Php::Value value = params[0]; - - long value1 = value; - std::string value2 = value; - bool value4 = value; - - Php::out << " long:" << value1 << "\n string:" << value2 << "\n bool:" << bool2str(value4) << std::endl; - } - - - /* - * Test Php::Value casting operators - */ - void value_cast2double(Php::Parameters ¶ms) - { - Php::Value value = params[0]; - double value3 = value; - - /* - * The remark (from valmat). - * Somehow std::to_string truncates the tail of numbers of type `double` when converting it to a string. - * So I wrote my own function `double2str()`, which does not have this drawback. - */ - Php::out << double2str(value3) << std::endl; - } - - /* - * Test Php::Value casting operators - */ - void value_cast2str(Php::Parameters ¶ms) - { - std::string value = params[0]; - Php::out << value << std::endl; - } - - /* - * Test Php::Value overloaded operators - */ - void overloaded_op(Php::Parameters ¶ms) - { - Php::Value value = params[0]; - if (value == "some string") - { - Php::out << "value == 'some string'" << std::endl; - } - - if (value == 12) - { - Php::out << "value == 12" << std::endl; - } - else if (value > 100) - { - Php::out << "value > 100" << std::endl; - } - - value += 10; - Php::out << value << std::endl; - - int r1 = value - 8; - Php::out << r1 << std::endl; - - double r2 = value*123.45; - Php::out << r2 << std::endl; - - double r3 = value/123.45; - Php::out << r3 << std::endl; - - - - } - - /* - * Test Php::Value arrays - */ - Php::Value value_arrays(void) - { - // create a regular array - Php::Value array; - array[0] = "apple"; - array[1] = "banana"; - array[2] = "tomato"; - - // an initializer list can be used to create a filled array - Php::Value filled({ "a", "b", "c", "d"}); - - // create an associative array - Php::Value assoc; - assoc["apple"] = "green"; - assoc["banana"] = "yellow"; - assoc["tomato"] = "green"; - - // the variables in an array do not all have to be of the same type - Php::Value assoc2; - assoc2["x"] = "info@example.com"; - assoc2["y"] = nullptr; - assoc2["z"] = 123; - - // nested arrays are possible too - Php::Value assoc3; - assoc3["x"] = "info@example.com"; - assoc3["y"] = nullptr; - assoc3["z"][0] = "a"; - assoc3["z"][1] = "b"; - assoc3["z"][2] = "c"; - - Php::Value r; - r["array"] = array; - r["filled"] = filled; - r["assoc"] = assoc; - r["assoc2"] = assoc2; - r["assoc3"] = assoc3; - return r; - } - - /* - * Test Php::Value object - */ - Php::Value value_object1(void) - { - - // create empty object of type stdClass - Php::Object object; - - // object properties can be accessed with square brackets - object["property1"] = "value1"; - object["property2"] = "value2"; - - // Php::Value is the base class, so you can assign Php::Object objects - //Php::Value value = object; - - return object; - } - - /* - * Test Php::Value object - */ - Php::Value value_object2(void) - { - - // create empty object of type stdClass - Php::Object object; - - // to create an object of a different type, pass in the class name - // to the constructor with optional constructor parameters - //object = Php::Object("DateTime", "2014-03-27 00:37:15.638276"); - - auto timeZone = Php::Object("DateTimeZone", "Europe/Amsterdam"); - object = Php::Object("DateTime", "2014-03-27 00:37:15", timeZone); - - - // methods can be called with the call() method - Php::out << object.call("format", "Y-m-d H:i:s") << std::endl; - - // all these methods can be called on a Php::Value object too - Php::Value value = Php::Object("DateTime", "2016-03-31 15:48:00", timeZone); - Php::out << value.call("format", "Y-m-d H:i:s") << std::endl; - Php::out << value.call("getOffset") << std::endl; - - return object; - } - - /** - * A sample class, with methods to cast objects to scalars - */ - class Obj2Scalar : public Php::Base - { - public: - /** - * C++ constructor and C++ destructpr - */ - Obj2Scalar() {} - virtual ~Obj2Scalar() {} - - /** - * Cast to a string - * - * Note that now we use const char* as return value, and not Php::Value. - * The __toString function is detected at compile time, and it does - * not have a fixed signature. You can return any value that can be picked - * up by a Php::Value object. - * - * @return const char * - */ - const char *__toString() - { - return "Mount Meru, also called Sumeru (Sanskrit)"; - } - - /** - * Cast to a integer - * @return long - */ - long __toInteger() - { - return 27032014; - } - - /** - * Cast to a floating point number - * @return double - */ - double __toFloat() - { - return 3.14159265359; - } - - /** - * Cast to a boolean - * @return bool - */ - bool __toBool() - { - return true; - } - }; - - /* - * Test Php::Value resize - */ - void ghfghfhf2(Php::Parameters ¶ms) - { - - - } - - - - - - - -/** - * End of namespace - */ -} - diff --git a/tests/cpp/include/variables/001-process_globals.h b/tests/cpp/include/variables/001-process_globals.h new file mode 100644 index 0000000..ae930d2 --- /dev/null +++ b/tests/cpp/include/variables/001-process_globals.h @@ -0,0 +1,57 @@ +/** + * + * Test variables + * 001-process_globals.phpt + * Global variables in PHP-CPP + * + */ + + + + +/** + * Set up namespace + */ +namespace TestVariables { + + + + /** + * process_globals() + * + * This function reads and modifies global variables + */ + Php::Value process_globals() + { + // all global variables can be accessed via the Php::GLOBALS variable, + // which is more or less the same as the PHP $_GLOBALS variable + + // set a global variable + Php::GLOBALS["a"] = 1; + + // increment a global variable + Php::GLOBALS["b"] += 1; + + // set a global variable to be an array + Php::GLOBALS["c"] = Php::Array(); + + // add a member to an array + Php::GLOBALS["c"]["member"] = 123; + + // and increment it + Php::GLOBALS["c"]["member"] += 77; + + // change value e + Php::GLOBALS["e"] = Php::GLOBALS["e"][0]("hello"); + + // if a global variable holds a function, we can call it + return Php::GLOBALS["d"](1,2,3); + } + + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/002-get_complex_array.h b/tests/cpp/include/variables/002-get_complex_array.h new file mode 100644 index 0000000..b12e5ee --- /dev/null +++ b/tests/cpp/include/variables/002-get_complex_array.h @@ -0,0 +1,35 @@ +/** + * + * Test variables + * 002-get_complex_array.phpt + * + */ + + + + +/** + * Set up namespace + */ +namespace TestVariables { + + /** + * This function returns complex array + */ + Php::Value get_complex_array() + { + Php::Value r; + r["a"] = 123; + r["b"] = 456; + r["c"][0] = "nested value"; + r["c"][1] = "example"; + r["c"][2] = 7; + return r; + } + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/003-value-types.h b/tests/cpp/include/variables/003-value-types.h new file mode 100644 index 0000000..e1d31b9 --- /dev/null +++ b/tests/cpp/include/variables/003-value-types.h @@ -0,0 +1,39 @@ +/** + * + * Test variables + * 003-value-types.phpt + * + */ + + +namespace TestVariables { + + /* + * Check type of value + * @param array + */ + void value_types(Php::Parameters ¶ms) { + if (params.size() == 0) { + return; + } + + Php::Value arr = params[0]; + + Php::out << "Null: " << bool2str( arr.get("Null").type() == Php::Type::Null ) << std::endl; + Php::out << "Numeric: " << bool2str( arr.get("Numeric").type() == Php::Type::Numeric ) << std::endl; + Php::out << "Float: " << bool2str( arr.get("Float").type() == Php::Type::Float ) << std::endl; + Php::out << "Bool: " << bool2str( arr.get("Bool").type() == Php::Type::Bool ) << std::endl; + Php::out << "Array: " << bool2str( arr.get("Array").type() == Php::Type::Array ) << std::endl; + Php::out << "Object: " << bool2str( arr.get("Object").type() == Php::Type::Object ) << std::endl; + Php::out << "String: " << bool2str( arr.get("String").type() == Php::Type::String ) << std::endl; + Php::out << "Resource: " << bool2str( arr.get("Resource").type() == Php::Type::Resource ) << std::endl; + Php::out << "Constant: " << bool2str( arr.get("Constant").type() == Php::Type::Constant ) << std::endl; + Php::out << "ConstantArray: " << bool2str( arr.get("ConstantArray").type() == Php::Type::ConstantArray ) << std::endl; + Php::out << "Callable1: " << bool2str( arr.get("Callable1").type() == Php::Type::Callable ) << std::endl; + Php::out << "Callable2: " << bool2str( arr.get("Callable2").type() == Php::Type::Callable ) << std::endl; + Php::out << "Callable3: " << bool2str( arr.get("Callable3").type() == Php::Type::Callable ) << std::endl; + Php::out << "Callable4: " << bool2str( arr.get("Callable4").type() == Php::Type::Callable ) << std::endl; + } + +} + diff --git a/tests/cpp/include/variables/004-store-scalar-variables.h b/tests/cpp/include/variables/004-store-scalar-variables.h new file mode 100644 index 0000000..9ce82e5 --- /dev/null +++ b/tests/cpp/include/variables/004-store-scalar-variables.h @@ -0,0 +1,48 @@ +/** + * + * Test variables + * 004-store-scalar-variables.phpt + * + */ + +/** + * Set up namespace + */ +namespace TestVariables { + + /* + * Test variables defined in PHP-CPP + */ + Php::Value scalar_store(void) { + + Php::Value value1 = 1234; + Php::Value value2 = "this is a string"; + Php::Value value3 = std::string("another string"); + Php::Value value4 = nullptr; + Php::Value value5 = 123.45; + Php::Value value6 = true; + + Php::Value r; + r[0] = value1; + r[1] = value2; + r[2] = value3; + r[3] = value4; + r[4] = value5; + r[5] = value6; + + r[6] = 1234; + r[7] = "this is a string"; + r[8] = std::string("another string"); + r[9] = nullptr; + r[10] = Php::Value(); + r[11] = 123.45; + r[12] = false; + + return r; + } + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/005-cast-objects-to-scalars.h b/tests/cpp/include/variables/005-cast-objects-to-scalars.h new file mode 100644 index 0000000..f053c4d --- /dev/null +++ b/tests/cpp/include/variables/005-cast-objects-to-scalars.h @@ -0,0 +1,75 @@ +/** + * + * Test variables + * 005-cast-objects-to-scalars.phpt + * + */ + + + +/** + * Set up namespace + */ +namespace TestVariables { + + /** + * A sample class, with methods to cast objects to scalars + */ + class Obj2Scalar : public Php::Base + { + public: + /** + * C++ constructor and C++ destructpr + */ + Obj2Scalar() {} + virtual ~Obj2Scalar() {} + + /** + * Cast to a string + * + * Note that now we use const char* as return value, and not Php::Value. + * The __toString function is detected at compile time, and it does + * not have a fixed signature. You can return any value that can be picked + * up by a Php::Value object. + * + * @return const char * + */ + const char *__toString() + { + return "Mount Meru, also called Sumeru (Sanskrit)"; + } + + /** + * Cast to a integer + * @return long + */ + long __toInteger() + { + return 27032014; + } + + /** + * Cast to a floating point number + * @return double + */ + double __toFloat() + { + return 3.14159265359; + } + + /** + * Cast to a boolean + * @return bool + */ + bool __toBool() + { + return true; + } + }; + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/006-casting-obj2str.h b/tests/cpp/include/variables/006-casting-obj2str.h new file mode 100644 index 0000000..879683d --- /dev/null +++ b/tests/cpp/include/variables/006-casting-obj2str.h @@ -0,0 +1,30 @@ +/** + * + * Test variables + * 006-casting-obj2str.phpt + * + */ + + + +/** + * Set up namespace + */ +namespace TestVariables { + + + /* + * Test Php::Value casting operators + */ + void value_cast2str(Php::Parameters ¶ms) + { + std::string value = params[0]; + Php::out << value << std::endl; + } + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/007-overloaded-operators.h b/tests/cpp/include/variables/007-overloaded-operators.h new file mode 100644 index 0000000..88b3117 --- /dev/null +++ b/tests/cpp/include/variables/007-overloaded-operators.h @@ -0,0 +1,46 @@ +/** + * + * Test variables + * 007-overloaded-operators.phpt + * + */ + + + +namespace TestVariables { + + /* + * Test Php::Value overloaded operators + */ + void overloaded_op(Php::Parameters ¶ms) + { + Php::Value value = params[0]; + if (value == "some string") + { + Php::out << "value == 'some string'" << std::endl; + } + + if (value == 12) + { + Php::out << "value == 12" << std::endl; + } + else if (value > 100) + { + Php::out << "value > 100" << std::endl; + } + + value += 10; + Php::out << value << std::endl; + + int r1 = value - 8; + Php::out << r1 << std::endl; + + double r2 = value*123.45; + Php::out << r2 << std::endl; + + double r3 = value/123.45; + Php::out << r3 << std::endl; + } + +} + diff --git a/tests/cpp/include/variables/008-value-arrays.h b/tests/cpp/include/variables/008-value-arrays.h new file mode 100644 index 0000000..937f79a --- /dev/null +++ b/tests/cpp/include/variables/008-value-arrays.h @@ -0,0 +1,57 @@ +/** + * + * Test variables + * 008-value-arrays.phpt + * + */ + + +namespace TestVariables { + + + /* + * Test Php::Value arrays + */ + Php::Value value_arrays(void) + { + // create a regular array + Php::Value array; + array[0] = "apple"; + array[1] = "banana"; + array[2] = "tomato"; + + // an initializer list can be used to create a filled array + Php::Value filled({ "a", "b", "c", "d"}); + + // create an associative array + Php::Value assoc; + assoc["apple"] = "green"; + assoc["banana"] = "yellow"; + assoc["tomato"] = "green"; + + // the variables in an array do not all have to be of the same type + Php::Value assoc2; + assoc2["x"] = "info@example.com"; + assoc2["y"] = nullptr; + assoc2["z"] = 123; + + // nested arrays are possible too + Php::Value assoc3; + assoc3["x"] = "info@example.com"; + assoc3["y"] = nullptr; + assoc3["z"][0] = "a"; + assoc3["z"][1] = "b"; + assoc3["z"][2] = "c"; + + Php::Value r; + r["array"] = array; + r["filled"] = filled; + r["assoc"] = assoc; + r["assoc2"] = assoc2; + r["assoc3"] = assoc3; + return r; + } + + +} + diff --git a/tests/cpp/include/variables/009-010-value-object.h b/tests/cpp/include/variables/009-010-value-object.h new file mode 100644 index 0000000..e3eba85 --- /dev/null +++ b/tests/cpp/include/variables/009-010-value-object.h @@ -0,0 +1,68 @@ +/** + * + * Test variables + * 009-value-object.phpt + * 010-value-object2.phpt + * + */ + + +/** + * Set up namespace + */ +namespace TestVariables { + + + /* + * Test Php::Value object + */ + Php::Value value_object1(void) + { + + // create empty object of type stdClass + Php::Object object; + + // object properties can be accessed with square brackets + object["property1"] = "value1"; + object["property2"] = "value2"; + + // Php::Value is the base class, so you can assign Php::Object objects + //Php::Value value = object; + + return object; + } + + /* + * Test Php::Value object + */ + Php::Value value_object2(void) + { + + // create empty object of type stdClass + Php::Object object; + + // to create an object of a different type, pass in the class name + // to the constructor with optional constructor parameters + //object = Php::Object("DateTime", "2014-03-27 00:37:15.638276"); + + auto timeZone = Php::Object("DateTimeZone", "Europe/Amsterdam"); + object = Php::Object("DateTime", "2014-03-27 00:37:15", timeZone); + + + // methods can be called with the call() method + Php::out << object.call("format", "Y-m-d H:i:s") << std::endl; + + // all these methods can be called on a Php::Value object too + Php::Value value = Php::Object("DateTime", "2016-03-31 15:48:00", timeZone); + Php::out << value.call("format", "Y-m-d H:i:s") << std::endl; + Php::out << value.call("getOffset") << std::endl; + + return object; + } + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/011-012-value-casting-operators.h b/tests/cpp/include/variables/011-012-value-casting-operators.h new file mode 100644 index 0000000..182054b --- /dev/null +++ b/tests/cpp/include/variables/011-012-value-casting-operators.h @@ -0,0 +1,52 @@ +/** + * + * Test variables + * 011-value-casting-operators.phpt + * 012-value-casting-operators-double.phpt + * + */ + + + +/** + * Set up namespace + */ +namespace TestVariables { + + /* + * Test Php::Value casting operators + */ + void value_casting(Php::Parameters ¶ms) + { + Php::Value value = params[0]; + + long value1 = value; + std::string value2 = value; + bool value4 = value; + + Php::out << " long:" << value1 << "\n string:" << value2 << "\n bool:" << bool2str(value4) << std::endl; + } + + + /* + * Test Php::Value casting operators + */ + void value_cast2double(Php::Parameters ¶ms) + { + Php::Value value = params[0]; + double value3 = value; + + /* + * The remark (from valmat). + * Somehow std::to_string truncates the tail of numbers of type `double` when converting it to a string. + * So I wrote my own function `double2str()`, which does not have this drawback. + */ + Php::out << double2str(value3) << std::endl; + } + + +/** + * End of namespace + */ +} + diff --git a/tests/cpp/include/variables/tpl.h b/tests/cpp/include/variables/tpl.h new file mode 100644 index 0000000..3c4ed7c --- /dev/null +++ b/tests/cpp/include/variables/tpl.h @@ -0,0 +1,30 @@ +/** + * + * Test variables + * phptname.phpt + * + */ + + + + +/** + * Set up namespace + */ +namespace TestVariables { + + + /* + * Test + */ + void fnname(Php::Parameters ¶ms) + { + + + } + +/** + * End of namespace + */ +} + -- cgit v1.2.3