Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libphpbridge phpt tests #32

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion prod/native/libphpbridge/code/PhpBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void PhpBridge::compileAndExecuteFile(std::string_view fileName) const {
#endif
std::string msg = "Unable to open file for compilation '"s;
msg.append(fileName);
msg.append("'"s);
throw std::runtime_error(msg);
}

Expand Down Expand Up @@ -367,7 +368,7 @@ void getFunctionReturnValue(zval *zv, zval *retval) {
void getCurrentException(zval *zv, zend_object *exception) {
if (exception && zend_is_unwind_exit(exception)) {
ZVAL_NULL(zv);
} else if (UNEXPECTED(exception)) {
} else if (UNEXPECTED(exception) && instanceof_function(exception->ce, zend_ce_throwable)) {
ZVAL_OBJ_COPY(zv, exception);
} else {
ZVAL_NULL(zv);
Expand Down
239 changes: 227 additions & 12 deletions prod/native/phpbridge_extension/code/BridgeModuleFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,40 @@ PHP_FUNCTION(isOpcacheEnabled) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_info, "isOpcacheEnabled: %d", BRIDGE_G(globals)->bridge.isOpcacheEnabled());
}

PHP_FUNCTION(getExtensionList) {
auto extensions = BRIDGE_G(globals)->bridge.getExtensionList();
for (auto const &extension : extensions) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_info, "name: '%s' version: '%s'", extension.name.c_str(), extension.version.c_str());
}
}

PHP_FUNCTION(getPhpInfo) {
auto info = BRIDGE_G(globals)->bridge.getPhpInfo();
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_info, "PHP-INFO: %s", info.c_str());
}

PHP_FUNCTION(getPhpSapiName) {
auto info = BRIDGE_G(globals)->bridge.getPhpSapiName();
RETURN_STRINGL(info.data(), info.length());
}


PHP_FUNCTION(compileAndExecuteFile) {
char *fileName = nullptr;
size_t fileNameLen = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STRING(fileName, fileNameLen)
ZEND_PARSE_PARAMETERS_END();

try {
BRIDGE_G(globals)->bridge.compileAndExecuteFile(std::string_view(fileName, fileNameLen));
} catch (std::exception const &msg) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "Native exception caught: '%s'", msg.what());
}
}


PHP_FUNCTION(findClassEntry) {
char *className = nullptr;
size_t classNameLen = 0;
Expand Down Expand Up @@ -120,29 +154,210 @@ PHP_FUNCTION(callMethod) {
RETURN_COPY(&returnValue);
}

PHP_FUNCTION(isObjectOfClass) {
char *className = nullptr;
size_t classNameLen = 0;
zval *object = nullptr;

ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(className, classNameLen)
Z_PARAM_ZVAL(object)
ZEND_PARSE_PARAMETERS_END();

RETURN_BOOL(elasticapm::php::isObjectOfClass(object, std::string_view(className, classNameLen)));
}

PHP_FUNCTION(getFunctionName) {
elasticapm::php::AutoZval zv;
long framesBack = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(framesBack)
ZEND_PARSE_PARAMETERS_END();

auto execData = EG(current_execute_data);
for (auto frame = 0; frame < framesBack; ++frame) {
execData = execData->prev_execute_data;
if (!execData) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "test failure, can't go %d frames back", framesBack);
RETURN_NULL();
return;
}
}

elasticapm::php::getFunctionName(zv.get(), execData);

RETURN_COPY(zv.get());
}


PHP_FUNCTION(getFunctionDeclaringScope) {
elasticapm::php::AutoZval zv;
long framesBack = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(framesBack)
ZEND_PARSE_PARAMETERS_END();

auto execData = EG(current_execute_data);
for (auto frame = 0; frame < framesBack; ++frame) {
execData = execData->prev_execute_data;
if (!execData) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "test failure, can't go %d frames back", framesBack);
RETURN_NULL();
return;
}
}

elasticapm::php::getFunctionDeclaringScope(zv.get(), execData);

RETURN_COPY(zv.get());
}

PHP_FUNCTION(getFunctionDeclarationFileName) {
elasticapm::php::AutoZval zv;
long framesBack = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(framesBack)
ZEND_PARSE_PARAMETERS_END();

auto execData = EG(current_execute_data);
for (auto frame = 0; frame < framesBack; ++frame) {
execData = execData->prev_execute_data;
if (!execData) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "test failure, can't go %d frames back", framesBack);
RETURN_NULL();
return;
}
}

elasticapm::php::getFunctionDeclarationFileName(zv.get(), execData);

RETURN_COPY(zv.get());
}

PHP_FUNCTION(getFunctionDeclarationLineNo) {
elasticapm::php::AutoZval zv;
long framesBack = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(framesBack)
ZEND_PARSE_PARAMETERS_END();

auto execData = EG(current_execute_data);
for (auto frame = 0; frame < framesBack; ++frame) {
execData = execData->prev_execute_data;
if (!execData) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "test failure, can't go %d frames back", framesBack);
RETURN_NULL();
return;
}
}

elasticapm::php::getFunctionDeclarationLineNo(zv.get(), execData);

RETURN_COPY(zv.get());
}

PHP_FUNCTION(getCallArguments) {
elasticapm::php::AutoZval zv;
long framesBack = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(framesBack)
ZEND_PARSE_PARAMETERS_END();

auto execData = EG(current_execute_data);
for (auto frame = 0; frame < framesBack; ++frame) {
execData = execData->prev_execute_data;
if (!execData) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "test failure, can't go %d frames back", framesBack);
RETURN_NULL();
return;
}
}

elasticapm::php::getCallArguments(zv.get(), execData);

RETURN_COPY(zv.get());
}


PHP_FUNCTION(getScopeNameOrThis) {
elasticapm::php::AutoZval zv;
long framesBack = 0;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_LONG(framesBack)
ZEND_PARSE_PARAMETERS_END();

auto execData = EG(current_execute_data);
for (auto frame = 0; frame < framesBack; ++frame) {
execData = execData->prev_execute_data;
if (!execData) {
BRIDGE_G(globals)->logger->printf(LogLevel::logLevel_error, "test failure, can't go %d frames back", framesBack);
RETURN_NULL();
return;
}
}

elasticapm::php::getScopeNameOrThis(zv.get(), execData);

RETURN_COPY(zv.get());
}

PHP_FUNCTION(getExceptionName) {
zval *object = nullptr;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(object)
ZEND_PARSE_PARAMETERS_END();

auto name = elasticapm::php::getExceptionName(Z_OBJ_P(object));
RETURN_STRINGL(name.data(), name.length());
}

PHP_FUNCTION(getCurrentException) {
zend_object *object = nullptr;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_OBJ(object)
ZEND_PARSE_PARAMETERS_END();

elasticapm::php::AutoZval zv;

elasticapm::php::getCurrentException(zv.get(), object);
RETURN_COPY(zv.get());
}

ZEND_BEGIN_ARG_INFO(no_paramters_arginfo, 0)
ZEND_END_ARG_INFO()

// clang-format off
const zend_function_entry phpbridge_functions[] = {
PHP_FE( detectOpcachePreload, no_paramters_arginfo )
PHP_FE( isOpcacheEnabled, no_paramters_arginfo )

PHP_FE( getExtensionList, no_paramters_arginfo )
PHP_FE( getPhpInfo, no_paramters_arginfo )
PHP_FE( getPhpSapiName, no_paramters_arginfo )
PHP_FE( compileAndExecuteFile, no_paramters_arginfo )

PHP_FE( findClassEntry, no_paramters_arginfo )
PHP_FE( getClassStaticPropertyValue, no_paramters_arginfo )
PHP_FE( getClassPropertyValue, no_paramters_arginfo )
PHP_FE( callMethod, no_paramters_arginfo )
PHP_FE( isObjectOfClass, no_paramters_arginfo )
PHP_FE( getFunctionName, no_paramters_arginfo )
PHP_FE( getFunctionDeclaringScope, no_paramters_arginfo )
PHP_FE( getFunctionDeclarationFileName, no_paramters_arginfo )
PHP_FE( getFunctionDeclarationLineNo, no_paramters_arginfo )
PHP_FE( getCallArguments, no_paramters_arginfo )
PHP_FE( getScopeNameOrThis, no_paramters_arginfo )
PHP_FE( getExceptionName, no_paramters_arginfo )
PHP_FE( getCurrentException, no_paramters_arginfo )

PHP_FE_END
};
// clang-format on

// std::string_view getExceptionName(zend_object *exception);
// bool isObjectOfClass(zval *object, std::string_view className);

// void getCallArguments(zval *zv, zend_execute_data *ex);
// void getScopeNameOrThis(zval *zv, zend_execute_data *execute_data);
// void getFunctionName(zval *zv, zend_execute_data *ex);
// void getFunctionDeclaringScope(zval *zv, zend_execute_data *ex);
// void getFunctionDeclarationFileName(zval *zv, zend_execute_data *ex);
// void getFunctionDeclarationLineNo(zval *zv, zend_execute_data *ex);
// void getFunctionReturnValue(zval *zv, zval *retval);
// void getCurrentException(zval *zv, zend_object *exception);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

echo "This is the result of executing the compiled script".PHP_EOL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
compileAndExecuteFile
--INI--
extension=/elastic/phpbridge.so
--FILE--
<?php
declare(strict_types=1);

compileAndExecuteFile("compileAndExecuteFile.inc");

echo 'Test completed';
?>
--EXPECTF--
This is the result of executing the compiled script
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

compilerrrrror;

echo "This is the result of executing the compiled script".PHP_EOL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
compileAndExecuteFile - error in file
--INI--
extension=/elastic/phpbridge.so
--FILE--
<?php
declare(strict_types=1);

compileAndExecuteFile("compileAndExecuteFile_errorInFile.inc");

echo 'Test completed';
?>
--EXPECTF--
%aNative exception caught: 'Error during execution of file 'compileAndExecuteFile_errorInFile.inc'. Error thrown with message 'Undefined constant "compilerrrrror"' in /phpt-tests/tests/compileAndExecuteFile_errorInFile.inc:3'
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
compileAndExecuteFile - file not found
--INI--
extension=/elastic/phpbridge.so
--FILE--
<?php
declare(strict_types=1);

compileAndExecuteFile("fileNotFound");

echo 'Test completed';
?>
--EXPECTF--
%aNative exception caught: 'Unable to open file for compilation 'fileNotFound''
Test completed
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
getCallArguments - reflaction of user class
--INI--
extension=/elastic/phpbridge.so
--FILE--
<?php
declare(strict_types=1);

class TestClass {
public function test() {
var_dump(getCallArguments(0)); // getCallArguments - internal - array(1) - int(0)
var_dump(getCallArguments(1)); // TestClass - array(1) - string
var_dump(getCallArguments(2)); // ReflectionMethod - internal - array(object, string)
var_dump(getCallArguments(3)); // main scope - array(0)
}
}

$obj = new ReflectionClass('TestClass');

$obj->getMethod('test')->invoke(new TestClass, "test function arg");


echo 'Test completed';
?>
--EXPECTF--
array(1) {
[0]=>
int(0)
}
array(1) {
[0]=>
string(17) "test function arg"
}
array(2) {
[0]=>
object(TestClass)#3 (0) {
}
[1]=>
string(17) "test function arg"
}
array(0) {
}
Test completed
Loading
Loading