WordPress setup: Installed WordPress using VVV.

PHPUnit setup:
I have setup the testing suite as mentioned in Pippin’s blog. And accessing PHPUnit after logging into the box machine by using vagrant ssh.

Test case: I’m writing my test cases inside the /tests/ folder in the test-{my-plugin-filename}.php file.

function test_my_plugin_method() {
    $links = plugin_method();
}

Where I’m stuck?

When I run PHPUnit, I’m getting the following error.

Error: Call to undefined function plugin_method()

I have added the following line.

require_once( dirname( dirname( __FILE__ ) ) . '/plugin-file.php' );

But I believe I’m missing something. Any help would be much appreciated.

Edit 1:

I executed wp scaffold plugin-tests and then the following line as mentioned in Pippin’s blog (refer above for link).

bash bin/install-wp-tests.sh wordpress_test root '' localhost latest

Finally I executed phpunit.

bootstrap.php

I haven’t changed anything in the bootstrap.php file.

<?php

$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
    $_tests_dir="/tmp/wordpress-tests-lib";
}

require_once $_tests_dir . '/includes/functions.php';

function _manually_load_plugin() {
    require dirname( dirname( __FILE__ ) ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

require $_tests_dir . '/includes/bootstrap.php';

2 Answers
2

Is the plugin_method() publicly available? Check whether it is a class method or belongs to a namespace.

If it belongs to a class, then you have to create an object of the class before you can call the method. Similarly if it is a namespace method, then you have to call the function with the full namespace.

Leave a Reply

Your email address will not be published. Required fields are marked *