Skip to content

Commit

Permalink
add tests to cover nr_php_call_user_func
Browse files Browse the repository at this point in the history
  • Loading branch information
lavarou committed Dec 29, 2023
1 parent 74ff85d commit 3bb1a78
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/agent/php_call/00.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
Test nr_php_call_user_func: user function exists and executes without an error.
*/

/*SKIPIF*/

/*INI
newrelic.framework = wordpress
newrelic.framework.wordpress.hooks = true
newrelic.framework.wordpress.plugins = true
newrelic.framework.wordpress.core = true
;newrelic.loglevel=verbosedebug
newrelic.special=show_executes, show_execute_params, show_execute_returns
*/

/*ENVIRONMENT
REQUEST_METHOD=GET
*/

/*EXPECT_METRICS_EXIST*/
/*
Supportability/InstrumentedFunction/apply_filters
Supportability/InstrumentedFunction/do_action
WebTransaction/Action/template
Framework/WordPress/Hook/template_include
Framework/WordPress/Hook/init
Framework/WordPress/Plugin/mock-theme
*/

/*EXPECT_ERROR_EVENTS null */

//require_once __DIR__.'/wp-content/themes/mock-theme.php';
//require_once __DIR__.'/wp-includes/functions.php';

// Mock WordPress hooks; only a single callback for a given hook can be defined
$wp_filters = [];

function add_filter($hook_name, $callback) {
global $wp_filters;
$wp_filters[$hook_name] = $callback;
}

function apply_filters($hook_name, $value, ...$args) {
global $wp_filters;
return call_user_func_array($wp_filters[$hook_name], array($value, $args));
}

// Called in fw_wordpress via nr_php_call_user_func
function get_theme_roots() {
return "wp-mock/wp-content/themes";
}

function identity_filter($value) {
return $value;
}

// Register custom plugin filter
add_filter("template_include", "identity_filter");

// Emulate WordPress loading a template to render a page:
$template = apply_filters("template_include", "./path/to/templates/template.php");
69 changes: 69 additions & 0 deletions tests/agent/php_call/01.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
Test nr_php_call_user_func: user function does not exists.
*/

/*SKIPIF*/

/*INI
newrelic.framework = wordpress
newrelic.framework.wordpress.hooks = true
newrelic.framework.wordpress.plugins = true
newrelic.framework.wordpress.core = true
;newrelic.loglevel=verbosedebug
newrelic.special=show_executes, show_execute_params, show_execute_returns
*/

/*ENVIRONMENT
REQUEST_METHOD=GET
*/

/*EXPECT_METRICS_EXIST*/
/*
Supportability/InstrumentedFunction/apply_filters
Supportability/InstrumentedFunction/do_action
WebTransaction/Action/template
Framework/WordPress/Hook/template_include
Framework/WordPress/Hook/init
Framework/WordPress/Plugin/mock-theme
*/

/*EXPECT_ERROR_EVENTS null */

//require_once __DIR__.'/wp-content/themes/mock-theme.php';
//require_once __DIR__.'/wp-includes/functions.php';

// Mock WordPress hooks; only a single callback for a given hook can be defined
$wp_filters = [];

function add_filter($hook_name, $callback) {
global $wp_filters;
$wp_filters[$hook_name] = $callback;
}

function apply_filters($hook_name, $value, ...$args) {
global $wp_filters;
return call_user_func_array($wp_filters[$hook_name], array($value, $args));
}

// Called in fw_wordpress via nr_php_call_user_func
/*
function get_theme_roots() {
return "wp-mock/wp-content/themes";
}
*/

function identity_filter($value) {
return $value;
}

// Register custom plugin filter
add_filter("template_include", "identity_filter");

// Emulate WordPress loading a template to render a page:
$template = apply_filters("template_include", "./path/to/templates/template.php");
69 changes: 69 additions & 0 deletions tests/agent/php_call/02.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
Test nr_php_call_user_func: user function does not exists.
*/

/*SKIPIF*/

/*INI
newrelic.framework = wordpress
newrelic.framework.wordpress.hooks = true
newrelic.framework.wordpress.plugins = true
newrelic.framework.wordpress.core = true
;newrelic.loglevel=verbosedebug
newrelic.special=show_executes, show_execute_params, show_execute_returns
*/

/*ENVIRONMENT
REQUEST_METHOD=GET
*/

/*EXPECT_METRICS_EXIST*/
/*
Supportability/InstrumentedFunction/apply_filters
Supportability/InstrumentedFunction/do_action
WebTransaction/Action/template
Framework/WordPress/Hook/template_include
Framework/WordPress/Hook/init
Framework/WordPress/Plugin/mock-theme
*/

/*EXPECT_ERROR_EVENTS null */

//require_once __DIR__.'/wp-content/themes/mock-theme.php';
//require_once __DIR__.'/wp-includes/functions.php';

// Mock WordPress hooks; only a single callback for a given hook can be defined
$wp_filters = [];

function add_filter($hook_name, $callback) {
global $wp_filters;
$wp_filters[$hook_name] = $callback;
}

function apply_filters($hook_name, $value, ...$args) {
global $wp_filters;
return call_user_func_array($wp_filters[$hook_name], array($value, $args));
}

// Called in fw_wordpress via nr_php_call_user_func
function get_theme_roots() {
throw new RuntimeException("throwing exception");
}

function identity_filter($value) {
return $value;
}

// Register custom plugin filter
add_filter("template_include", "identity_filter");

// Emulate WordPress loading a template to render a page:
$template = apply_filters("template_include", "./path/to/templates/template.php");

echo "this should not print";
69 changes: 69 additions & 0 deletions tests/agent/php_call/03.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*DESCRIPTION
Test nr_php_call_user_func: user function does not exists.
*/

/*SKIPIF*/

/*INI
newrelic.framework = wordpress
newrelic.framework.wordpress.hooks = true
newrelic.framework.wordpress.plugins = true
newrelic.framework.wordpress.core = true
;newrelic.loglevel=verbosedebug
newrelic.special=show_executes, show_execute_params, show_execute_returns
*/

/*ENVIRONMENT
REQUEST_METHOD=GET
*/

/*EXPECT_METRICS_EXIST*/
/*
Supportability/InstrumentedFunction/apply_filters
Supportability/InstrumentedFunction/do_action
WebTransaction/Action/template
Framework/WordPress/Hook/template_include
Framework/WordPress/Hook/init
Framework/WordPress/Plugin/mock-theme
*/

/*EXPECT_ERROR_EVENTS null */

//require_once __DIR__.'/wp-content/themes/mock-theme.php';
//require_once __DIR__.'/wp-includes/functions.php';

// Mock WordPress hooks; only a single callback for a given hook can be defined
$wp_filters = [];

function add_filter($hook_name, $callback) {
global $wp_filters;
$wp_filters[$hook_name] = $callback;
}

function apply_filters($hook_name, $value, ...$args) {
global $wp_filters;
return call_user_func_array($wp_filters[$hook_name], array($value, $args));
}

// Called in fw_wordpress via nr_php_call_user_func
function get_theme_roots() {
trigger_error("Cannot get theme roots error");
}

function identity_filter($value) {
return $value;
}

// Register custom plugin filter
add_filter("template_include", "identity_filter");

// Emulate WordPress loading a template to render a page:
$template = apply_filters("template_include", "./path/to/templates/template.php");

echo "this should not print";

0 comments on commit 3bb1a78

Please sign in to comment.