summaryrefslogtreecommitdiff
path: root/tests/cpp
diff options
context:
space:
mode:
authorvalmat <ufabiz@gmail.com>2014-03-18 15:18:11 +0600
committervalmat <ufabiz@gmail.com>2014-03-18 15:18:11 +0600
commite8334afd7f2920b706a3d28250a3e2b6dbd49538 (patch)
treea92e2b1b67c22eaad7eb84a2138ede2dae12e873 /tests/cpp
parentbfaed88493de0a3ebd7f2619cb11291cd09252b1 (diff)
Unit tests. For description see https://github.com/CopernicaMarketingSoftware/PHP-CPP/pull/25#issuecomment-37882236
Diffstat (limited to 'tests/cpp')
-rw-r--r--tests/cpp/Makefile141
-rw-r--r--tests/cpp/include/MyCustomClass.h70
-rw-r--r--tests/cpp/include/testValueIterator.h27
-rw-r--r--tests/cpp/main.cpp54
-rw-r--r--tests/cpp/readme2
5 files changed, 294 insertions, 0 deletions
diff --git a/tests/cpp/Makefile b/tests/cpp/Makefile
new file mode 100644
index 0000000..2a3cbaf
--- /dev/null
+++ b/tests/cpp/Makefile
@@ -0,0 +1,141 @@
+#
+# Makefile template
+#
+# This is an example Makefile that can be used by anyone who is building
+# his or her own PHP extensions using the PHP-CPP library.
+#
+# In the top part of this file we have included variables that can be
+# altered to fit your configuration, near the bottom the instructions and
+# dependencies for the compiler are defined. The deeper you get into this
+# file, the less likely it is that you will have to change anything in it.
+#
+
+#
+# Name of your extension
+#
+# This is the name of your extension. Based on this extension name, the
+# name of the library file (name.so) and the name of the config file (name.ini)
+# are automatically generated
+#
+
+NAME = extfortest
+
+
+#
+# Php.ini directories
+#
+# In the past, PHP used a single php.ini configuration file. Today, most
+# PHP installations use a conf.d directory that holds a set of config files,
+# one for each extension. Use this variable to specify this directory.
+#
+
+# in our case it is not required
+#INI_DIR = /etc/php5/conf.d
+
+
+#
+# The extension dirs
+#
+# This is normally a directory like /usr/lib/php5/20121221 (based on the
+# PHP version that you use. We make use of the command line 'php-config'
+# instruction to find out what the extension directory is, you can override
+# this with a different fixed directory
+#
+
+#EXTENSION_DIR = $(shell php-config --extension-dir)
+EXTENSION_DIR = ../..
+
+
+#
+# The name of the extension and the name of the .ini file
+#
+# These two variables are based on the name of the extension. We simply add
+# a certain extension to them (.so or .ini)
+#
+
+EXTENSION = ${NAME}.so
+# in our case it is not required
+#INI = ${NAME}.ini
+
+
+#
+# Compiler
+#
+# By default, the GNU C++ compiler is used. If you want to use a different
+# compiler, you can change that here. You can change this for both the
+# compiler (the program that turns the c++ files into object files) and for
+# the linker (the program that links all object files into the single .so
+# library file. By default, g++ (the GNU C++ compiler) is used for both.
+#
+
+COMPILER = g++
+LINKER = g++
+
+
+#
+# Compiler and linker flags
+#
+# This variable holds the flags that are passed to the compiler. By default,
+# we include the -O2 flag. This flag tells the compiler to optimize the code,
+# but it makes debugging more difficult. So if you're debugging your application,
+# you probably want to remove this -O2 flag. At the same time, you can then
+# add the -g flag to instruct the compiler to include debug information in
+# the library (but this will make the final libphpcpp.so file much bigger, so
+# you want to leave that flag out on production servers).
+#
+# If your extension depends on other libraries (and it does at least depend on
+# one: the PHP-CPP library), you should update the LINKER_DEPENDENCIES variable
+# with a list of all flags that should be passed to the linker.
+#
+
+#COMPILER_FLAGS = -Wall -c -O2 -std=c++11 -fpic -o
+#COMPILER_FLAGS = -Wall -c -g -std=c++11 -fpic -o
+COMPILER_FLAGS = -Wall -c -g -std=c++11 -fpic -I. -I"../../include" -o
+LINKER_FLAGS = -shared
+LINKER_DEPENDENCIES = -lphpcpp
+
+
+#
+# Command to remove files, copy files and create directories.
+#
+# I've never encountered a *nix environment in which these commands do not work.
+# So you can probably leave this as it is
+#
+
+RM = rm -f
+CP = cp -f
+MKDIR = mkdir -p
+
+
+#
+# All source files are simply all *.cpp files found in the current directory
+#
+# A builtin Makefile macro is used to scan the current directory and find
+# all source files. The object files are all compiled versions of the source
+# file, with the .cpp extension being replaced by .o.
+#
+
+SOURCES = $(wildcard *.cpp)
+OBJECTS = $(SOURCES:%.cpp=%.o)
+
+
+#
+# From here the build instructions start
+#
+
+all: ${OBJECTS} ${EXTENSION}
+
+${EXTENSION}: ${OBJECTS}
+ ${LINKER} ${LINKER_FLAGS} -o $@ ${OBJECTS} ${LINKER_DEPENDENCIES}
+
+${OBJECTS}:
+ ${COMPILER} ${COMPILER_FLAGS} $@ ${@:%.o=%.cpp}
+
+# Do not install this extention
+#install:
+# ${CP} ${EXTENSION} ${EXTENSION_DIR}
+# ${CP} ${INI} ${INI_DIR}
+
+clean:
+ ${RM} ${EXTENSION} ${OBJECTS}
+
diff --git a/tests/cpp/include/MyCustomClass.h b/tests/cpp/include/MyCustomClass.h
new file mode 100644
index 0000000..02ff44b
--- /dev/null
+++ b/tests/cpp/include/MyCustomClass.h
@@ -0,0 +1,70 @@
+/**
+ *
+ *
+ *
+ *
+ */
+
+
+/**
+ * Set up namespace
+ */
+namespace TestBaseClass {
+
+
+/**
+ * Namespace to use
+ */
+ using namespace std;
+
+ 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 &params)
+ {
+ // 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;
+
+ return 5;
+
+ }
+ };
+
+
+
+/**
+ * End of namespace
+ */
+}
+
+ \ No newline at end of file
diff --git a/tests/cpp/include/testValueIterator.h b/tests/cpp/include/testValueIterator.h
new file mode 100644
index 0000000..a9329c9
--- /dev/null
+++ b/tests/cpp/include/testValueIterator.h
@@ -0,0 +1,27 @@
+/**
+ *
+ * TestValueIterator
+ *
+ */
+
+/**
+ * Set up namespace
+ */
+namespace TestValueIterator {
+
+
+ void loopValue(Php::Parameters &params)
+ {
+ 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/main.cpp b/tests/cpp/main.cpp
new file mode 100644
index 0000000..7a4c15d
--- /dev/null
+++ b/tests/cpp/main.cpp
@@ -0,0 +1,54 @@
+/**
+ *
+ * An example file to show the working of using a C++ class in PHP.
+ */
+
+#include <string>
+#include <iostream>
+#include <phpcpp.h>
+
+// Test includes
+#include "include/testValueIterator.h"
+#include "include/MyCustomClass.h"
+
+
+
+// Symbols are exported according to the "C" language
+extern "C"
+{
+ // export the "get_module" function that will be called by the Zend engine
+ PHPCPP_EXPORT void *get_module()
+ {
+ // create extension
+ static Php::Extension extension("extention_for_tests","0.1");
+
+ // build an interface
+ //Php::Interface interface("MyInterface");
+
+ // add methods to the interface
+ //interface.method("method1");
+ //interface.method("method2");
+
+ // add the interface to the extension
+ //extension.add(interface);
+
+ // we are going to define a class
+ Php::Class<TestBaseClass::MyCustomClass> customClass("TestBaseClass\\MyClass");
+
+ // add methods to it
+ customClass.method("myMethod", &TestBaseClass::MyCustomClass::myMethod, Php::Final, {});
+ customClass.property("property1", "prop1");
+ customClass.property("property2", "prop2", Php::Protected);
+
+ // add the class to the extension
+ extension.add(customClass);
+
+
+ // add function to extension
+ //extension.add("TestValueIterator\\loopValue", TestValueIterator::loopValue/*, {
+ extension.add("TestValueIterator\\loopValue", TestValueIterator::loopValue);
+
+ // return the extension module
+ return extension;
+ }
+}
diff --git a/tests/cpp/readme b/tests/cpp/readme
new file mode 100644
index 0000000..c3743ce
--- /dev/null
+++ b/tests/cpp/readme
@@ -0,0 +1,2 @@
+This extension is written with a single purpose - all tests will be conducted through it.
+No installation required!