diff options
author | valmat <ufabiz@gmail.com> | 2014-03-28 00:05:14 +0600 |
---|---|---|
committer | valmat <ufabiz@gmail.com> | 2014-03-28 00:05:14 +0600 |
commit | f56cff9e4f67a949e6f0ae35ca63aaaea16d9a41 (patch) | |
tree | f23abcc134dc189d695152057528365f412d9be2 /tests | |
parent | b2e2aa93754a12fac360e8de5196e45cd2f0f2aa (diff) |
add test variables/005-cast-objects-to-scalars.phpt
Diffstat (limited to 'tests')
-rw-r--r-- | tests/cpp/include/variables.h | 55 | ||||
-rw-r--r-- | tests/cpp/main.cpp | 4 | ||||
-rw-r--r-- | tests/php/dbg.php | 2 | ||||
-rw-r--r-- | tests/php/phpt/variables/005-cast-objects-to-scalars.phpt | 22 | ||||
-rw-r--r-- | tests/php/phpt/variables/readme | 4 |
5 files changed, 85 insertions, 2 deletions
diff --git a/tests/cpp/include/variables.h b/tests/cpp/include/variables.h index b7063ef..e8cb526 100644 --- a/tests/cpp/include/variables.h +++ b/tests/cpp/include/variables.h @@ -289,6 +289,61 @@ namespace TestVariables { 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 */ diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index 4e781d2..e5150ed 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -71,6 +71,10 @@ extern "C" extension.add("TestVariables\\value_object1", TestVariables::value_object1); extension.add("TestVariables\\value_object2", TestVariables::value_object2); + // A sample class, with methods to cast objects to scalars + Php::Class<TestVariables::Obj2Scalar> cObj2Scalar("Obj2Scalar"); + extension.add(std::move(cObj2Scalar)); + diff --git a/tests/php/dbg.php b/tests/php/dbg.php index 4728700..2040f01 100644 --- a/tests/php/dbg.php +++ b/tests/php/dbg.php @@ -12,5 +12,3 @@ -//--------- -TestValueIterator\loopArray();
\ No newline at end of file diff --git a/tests/php/phpt/variables/005-cast-objects-to-scalars.phpt b/tests/php/phpt/variables/005-cast-objects-to-scalars.phpt new file mode 100644 index 0000000..335acb7 --- /dev/null +++ b/tests/php/phpt/variables/005-cast-objects-to-scalars.phpt @@ -0,0 +1,22 @@ +--TEST-- +cast objects to scalars +--DESCRIPTION-- +http://www.phpcpp.com/documentation/special-features#casting-functions +--SKIPIF-- +<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?> +--FILEEOF-- +<?php + + +function bool2str($b) { + return $b ? "Yes" : "No"; +} +$obj = new Obj2Scalar(); +echo " long: ", (int)$obj, "\n string: ", (string)$obj, "\n double: ", (double)$obj, "\n bool: ", bool2str((bool)$obj); + + +--EXPECT-- + long: 27032014 + string: Mount Meru, also called Sumeru (Sanskrit) + double: 3.14159265359 + bool: Yes
\ No newline at end of file diff --git a/tests/php/phpt/variables/readme b/tests/php/phpt/variables/readme index 8162526..a47cb89 100644 --- a/tests/php/phpt/variables/readme +++ b/tests/php/phpt/variables/readme @@ -1 +1,5 @@ tests variables + +TODO: more tests for globals variables +including separate test for $_GET, $_POST, $_COOKIE, $_FILES, $_SERVER, $_REQUEST and $_ENV (Php::GET, Php::POST, Php::COOKIE, Php::FILES, Php::SERVER, Php::REQUEST and Php::ENV) variables. + |