Issue with mocking my functions (Wordpress)
FredoNook opened this issue · 4 comments
Hello comrades! I have one issue with mocking my functions writen in wordpress theme. For generate base test files i utilize the wp scraffold
after i add download BrainMonkey by Composer and add to my bootstrap.php
import Composer's autoload:
// Require Brain\Monkey
require_once __DIR__ . '/libs/vendor/autoload.php';
So then i include namespaces in my tests file:
use PHPUnit\Framework\TestCase;
use Brain\Monkey\Functions;
use Brain\Monkey;
Then i add setUp()
and tearDown()
methods:
function setUp() {
parent::setUp();
Monkey\setUp();
}
function tearDown() {
Monkey\tearDown();
parent::tearDown();
}
Then i add Mockery trait adapter to test class:
use \Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
Write test for my function mocking another my function:
function test_func_is_user_banned_ban_empty() {
Functions\expect( 'dirt_get_bans_by_player' )->once()->with( 'user id' )->andReturn( null );
$no_ban_fields_found = dirt_is_user_banned( 'user id' );
$this->assertSame( null, $no_ban_fields_found );
}
And after i run PHPUnit tests i have that issue:
1) Ban_Tests::test_func_is_user_banned_ban_empty
Patchwork\Exceptions\DefinedTooEarly: The file that defines dirt_get_bans_by_player() was included earlier than Patchwork. This is likely a result of an improper setup; see readme for details.
Help me please, how fix it. Im little confused because i import autoload.php
with BrainMonkey by firtst instruction in bootstrap.php
. Thanks a lot.
Hi, @FredoNook
Brain Monkey is a tool designed to mock functions when those are not available. Because you're using wp scaffold
I am assuming that you're loading WordPress and this causes issues with Brain Monkey.
More specifically, the function dirt_get_bans_by_player
is not a WordPress core function, and it looks like a custom function of yours. Custom functions can still be mocked, but they should not be loaded before Composer autoload is required.
My guess is that when your bootstrap.php
file is loaded, your theme, including the function you want to mock, is already available and so Brain Monkey can't mock it.
Hi @gmazzap
There is my bootstarp.php
, it autogenerated by wp scaffold
i just add require_once __DIR__ . '/libs/vendor/autoload.php';
, so after i run phpunit
if i understand right first code is runned by phpunit
is bootstrap.php
. Where i made mistake?
<?php
/**
* PHPUnit bootstrap file
*
* @package Dirtcraftru
*/
// Require Brain\Monkey
require_once __DIR__ . '/libs/vendor/autoload.php';
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL;
exit( 1 );
}
// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';
/**
* Registers theme
*/
function _register_theme() {
$theme_dir = dirname( __DIR__ );
$current_theme = basename( $theme_dir );
$theme_root = dirname( $theme_dir );
add_filter( 'theme_root', function() use ( $theme_root ) {
return $theme_root;
} );
register_theme_directory( $theme_root );
add_filter( 'pre_option_template', function() use ( $current_theme ) {
return $current_theme;
});
add_filter( 'pre_option_stylesheet', function() use ( $current_theme ) {
return $current_theme;
});
}
tests_add_filter( 'muplugins_loaded', '_register_theme' );
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
About function, yest them are custom functions and written in theme files.
Closing this issue because there's nothing to do here.
Once again, Brain Monkey is a tool to test WordPress code without loading WordPress. If one wants to run integration/e2e tests loading WordPress (which si something that makes ton of sense), then Brain Monkey should not be loaded for those tests.
I just have to say thank you for this library and for being clear about what it does. I was trying to mock a core wp function using BrainMonkey while including the WordPress library (using the official WordPress PHPUnit testing environment) and now I get what this library is about.
Unfortunately, I can't use it since my tests already depend on WordPress but I'll sure consider it in the future for testing WordPress in complete isolation.