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

ReflectionException: Function get_option() does not exist when using Patchwork in test bootstrap #153

Open
Mario453-tech opened this issue Dec 5, 2024 · 0 comments

Comments

@Mario453-tech
Copy link

Mario453-tech commented Dec 5, 2024

Description

When trying to set up a test environment for a WordPress plugin using Brain\Monkey, I'm encountering a ReflectionException stating that the get_option() function does not exist. This occurs during the bootstrap process, specifically when Patchwork tries to patch WordPress functions.

Environment

  • PHP Version: [Your PHP Version]
  • Brain\Monkey Version: 2.6.2
  • PHPUnit Version: 9.6.22
  • Operating System: Windows
  • WordPress Plugin Type: WooCommerce Integration

Dependencies (from composer.json)

{
    "require-dev": {
        "phpunit/phpunit": "^9.0",
        "brain/monkey": "^2.6",
        "mockery/mockery": "^1.5"
    }
}

Bootstrap :

<?php

// First require Patchwork
require_once dirname(__DIR__) . '/vendor/antecedent/patchwork/Patchwork.php';

// Then require the Composer autoloader
require_once dirname(__DIR__) . '/vendor/autoload.php';

// Now require Brain Monkey
require_once dirname(__DIR__) . '/vendor/brain/monkey/inc/api.php';

// Define WordPress constants
if (!defined('ABSPATH')) {
    define('ABSPATH', __DIR__ . '/wordpress/');
}

if (!defined('WP_DEBUG')) {
    define('WP_DEBUG', true);
}

if (!defined('DAY_IN_SECONDS')) {
    define('DAY_IN_SECONDS', 86400);
}

// Initialize Brain Monkey
Brain\Monkey\setUp();

// Mock WordPress functions
Brain\Monkey\Functions\stubs([
    'get_option' => [],
    'update_option' => true,
    'wp_verify_nonce' => true,
    'current_user_can' => true,
    'plugin_basename' => 'plugin/plugin.php',
    'plugin_dir_path' => __DIR__ . '/',
    'plugins_url' => 'http://example.com/wp-content/plugins/',
    '__' => function($text) { return $text; },
    'esc_html' => function($text) { return $text; },
    'esc_attr' => function($text) { return $text; },
    'esc_url' => function($text) { return $text; },
    'wp_kses_post' => function($text) { return $text; },
    'sanitize_text_field' => function($text) { return $text; },
    'add_action' => true,
    'add_filter' => true,
    'do_action' => true,
    'apply_filters' => function($hook, $value) { return $value; },
]);

// Clean up Brain\Monkey on shutdown
register_shutdown_function(function() {
    Brain\Monkey\tearDown();
});

Error Log

Error in bootstrap script: ReflectionException:
Function get_option() does not exist
#0 C:\xampp\htdocs\wordpress\wp-content\plugins\zarzadzanie-dzielnicami\vendor\antecedent\patchwork\src\CallRerouting.php(416): ReflectionFunction->__construct('get_option')
#1 C:\xampp\htdocs\wordpress\wp-content\plugins\zarzadzanie-dzielnicami\vendor\antecedent\patchwork\Patchwork.php(131): Patchwork\CallRerouting\createStubsForInternals()
#2 C:\xampp\htdocs\wordpress\wp-content\plugins\zarzadzanie-dzielnicami\tests\bootstrap.php(4): require_once('C:\\xampp\\htdocs...')`

What I've Tried
Loading Patchwork before any function definitions
Using different namespace approaches (global namespace, namespaced code)
Manually defining WordPress functions before Brain\Monkey setup
Using Brain\Monkey's Functions\stubs() instead of manual function definitions
Following the solution from this StackOverflow post
Expected Behavior
The test bootstrap should successfully mock WordPress functions using Brain\Monkey without throwing the ReflectionException.

Question
Is there a specific order or approach required to properly set up Brain\Monkey with Patchwork for WordPress function mocking? The documentation suggests this should work, but I'm encountering this error consistently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant