summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorvalmat <ufabiz@gmail.com>2014-03-28 02:34:51 +0600
committervalmat <ufabiz@gmail.com>2014-03-28 02:34:51 +0600
commit7bc500847e3027bb785c5525a21078ff72acc2ab (patch)
tree06e29578cbaf38e328b662c6e4130ad07b055f39 /tests
parent28152192b85cbe9bf5ea08ebe8c706d12e2e13e5 (diff)
add test: 004-static-funct.phpt (Test class with static function)
Diffstat (limited to 'tests')
-rw-r--r--tests/cpp/include/Classes_and_objects.h81
-rw-r--r--tests/cpp/main.cpp18
-rw-r--r--tests/php/dbg.php35
-rw-r--r--tests/php/phpt/class_obj/003-comparable.phpt2
-rw-r--r--tests/php/phpt/class_obj/004-static-funct.phpt26
5 files changed, 124 insertions, 38 deletions
diff --git a/tests/cpp/include/Classes_and_objects.h b/tests/cpp/include/Classes_and_objects.h
index e9b7434..0977da9 100644
--- a/tests/cpp/include/Classes_and_objects.h
+++ b/tests/cpp/include/Classes_and_objects.h
@@ -49,7 +49,7 @@ namespace TestBaseClass {
// check number of parameters
//if (params.size() != 1) throw Php::Exception("Invalid number of parameters supplied");
- std::cout << "myMethod is called for object " << _x << std::endl;
+ Php::out << "myMethod is called for object " << _x << std::endl;
return 5;
@@ -58,7 +58,7 @@ namespace TestBaseClass {
/**
- * custom comparison operator
+ * Test custom comparison operator
*/
class Comparable : public Php::Base
{
@@ -110,6 +110,83 @@ namespace TestBaseClass {
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 &params)
+ {
+ 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 &params)
+ {
+ 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 &params)
+ {
+ Php::out << "testStaticPubClass::staticMethod()"<< std::endl;
+ }
+ };
+
+
+ /**
+ * End test static functions
+ */
+
+
diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp
index 7569a0b..b892b41 100644
--- a/tests/cpp/main.cpp
+++ b/tests/cpp/main.cpp
@@ -56,6 +56,24 @@ extern "C"
extension.add( Php::Class<TestBaseClass::Comparable>("TestBaseClass\\Comparable") );
+ // test static functions
+ //
+ // description of the class so that PHP knows which methods are accessible
+ Php::Class<TestBaseClass::testStaticPubClass> ClassWithStatic("TestBaseClass\\ClassWithStatic");
+ // register the testStaticPubClass::staticMethod to be a static method callable from PHP
+ ClassWithStatic.method("static1", &TestBaseClass::testStaticPubClass::staticMethod);
+ // regular functions have the same signatures as static methods. So nothing forbids you to register a normal function as static method too
+ ClassWithStatic.method("static2", TestBaseClass::testStaticRegFunc);
+ // and even static methods from completely different classes have the same function signature and can thus be registered
+ ClassWithStatic.method("static3", &TestBaseClass::testStaticPrivClass::staticMethod);
+ // add the class to the extension
+ extension.add(std::move(ClassWithStatic));
+ // In fact, because a static method has the same signature
+ // as a regular function, you can also register static
+ // C++ methods as regular global PHP functions
+ extension.add("TestBaseClass\\staticFun1", &TestBaseClass::testStaticPrivClass::staticMethod);
+
+
/**
diff --git a/tests/php/dbg.php b/tests/php/dbg.php
index b9841c7..9a28fc6 100644
--- a/tests/php/dbg.php
+++ b/tests/php/dbg.php
@@ -11,38 +11,3 @@
-
-//-
-
-// initialize a couple of objects
-$object1 = new TestBaseClass\Comparable();
-$object2 = new TestBaseClass\Comparable();
-$object3 = new TestBaseClass\Comparable();
-
-// compare the objects
-if ($object1 < $object2)
-{
- echo("$object1 is smaller than $object2\n");
-}
-else
-{
- echo("$object1 is bigger than $object2\n");
-}
-
-if ($object1 == $object3)
-{
- echo("$object1 is equal to $object3\n");
-}
-else
-{
- echo("$object1 is not equal to $object3\n");
-}
-
-if ($object1 != $object2)
-{
- echo("$object1 is not equal to $object2\n");
-}
-else
-{
- echo("$object1 is equal to $object2\n");
-} \ No newline at end of file
diff --git a/tests/php/phpt/class_obj/003-comparable.phpt b/tests/php/phpt/class_obj/003-comparable.phpt
index 8ffc076..3e0b1f6 100644
--- a/tests/php/phpt/class_obj/003-comparable.phpt
+++ b/tests/php/phpt/class_obj/003-comparable.phpt
@@ -1,5 +1,5 @@
--TEST--
-Test constructor & destructor
+Test comparable objects
--SKIPIF--
<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?>
--FILEEOF--
diff --git a/tests/php/phpt/class_obj/004-static-funct.phpt b/tests/php/phpt/class_obj/004-static-funct.phpt
new file mode 100644
index 0000000..8c78210
--- /dev/null
+++ b/tests/php/phpt/class_obj/004-static-funct.phpt
@@ -0,0 +1,26 @@
+--TEST--
+Test class with static function
+--SKIPIF--
+<?php if (!extension_loaded("extension_for_tests")) print "skip"; ?>
+--FILEEOF--
+<?php
+
+// this will call PublicClass::staticMethod()
+TestBaseClass\ClassWithStatic::static1();
+
+// this will call PrivateClass::staticMethod()
+TestBaseClass\ClassWithStatic::static2();
+
+// this will call regularFunction()
+TestBaseClass\ClassWithStatic::static3();
+
+// this will also call PrivateClass::staticMethod()
+TestBaseClass\staticFun1();
+
+
+//echo PHP_EOL;
+--EXPECT--
+testStaticPubClass::staticMethod()
+testStatic regular function
+testStaticPrivClass::staticMethod()
+testStaticPrivClass::staticMethod() \ No newline at end of file