I need to merge them. And past my projects inside for testing and development.
The WordPress shows me this?page. I have installed everything regarding points with Docker. However the command “npm run test:php” returns the errors connecting to the database. Files?wp-config.php?and?wp-config-tests.php?are correct.
Please give me a guide about how to make it work.
Thanks
]]>wp-env start
starts up a new environment, I can go to the instance in a browser and everything works.
Now I want to run PHPUnit tests for my plugin in this environment, but can’t find any way to do so.
According to the doc – https://developer.www.ads-software.com/block-editor/reference-guides/packages/packages-env/, PHPUnit should be available in all containers, but wp-env run tests-cli phpunit
fails saying "phpunit": executable file not found in $PATH: unknown
I thought I may need to run composer install
, but composer install also fails due to missing composer.json
.
Can anyone please help?
]]>I’ve stumbled upon a big hurdle in phpunit for wordpress, where I can’t call _handleAjax more than once in a test. I have to call it at most once in a test otherwise I encounter issues.
My test file is as follows:
class AjaxTest extends WP_Ajax_UnitTestCase {
public function test_submission_verification() {
global $_POST;
$_POST['directory_id'] = 'ded88';
$_POST['page'] = '1';
$_POST['sorting'] = 'random';
$_POST['gmt_offset'] = '0';
$_POST['post_refferer'] = '53';
$_POST['nonce'] = wp_create_nonce( 'um-frontend-nonce' );
$_POST['action'] = 'um_get_members';
try {
$this->_handleAjax( 'um_get_members' );
} catch ( WPAjaxDieStopException | WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
}
//so far so good
try {
$this->_handleAjax( 'um_get_members' );
} catch ( WPAjaxDieStopException | WPAjaxDieContinueException $e ) {
// We expected this, do nothing.
}
//mayhem
}
}
I’ve looked at the code inside the WP_Ajax_UnitTestCase class and it seems _handleAjax is built to be called once in a test and thats it:
protected function _handleAjax( $action ) {
// Start output buffering.
ini_set( 'implicit_flush', false );
ob_start();
// Build the request.
$_POST['action'] = $action;
$_GET['action'] = $action;
$_REQUEST = array_merge( $_POST, $_GET );
// Call the hooks.
do_action( 'admin_init' );
do_action( 'wp_ajax_' . $_REQUEST['action'], null );
// Save the output.
$buffer = ob_get_clean();
if ( ! empty( $buffer ) ) {
$this->_last_response = $buffer;
}
}
So I’ve tried removing do_action( 'admin_init' );
from the function but unfortunately there’s still problems.
Issue is that for these phpunit tests wordpress is bootstrapped and setup for each test and I cant re-boostrap it in the middle of a test so changes made to classes and global functions remain throughout the test. I’ve tried doing $this->tear_down() ; $this->set_up() ;
but those functions can only do so much.
Is there any way to get around this, how can I make multiple ajax calls to in one test function?
Thank you.
UPDATE: _handleAjax cant be called more than once in different tests from different files even.
]]>kindly note that when i run php vendor/phpunit/phpunit/phpunit only it returned
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
Usage: phpunit [options] UnitTest [UnitTest.php]
phpunit [options] <directory>
My plugin uses custom tables, which are installed upon plugin activation if they aren’t present, and also some custom constants located in wp-config.php
The general testing setup creates a new database/install for running the tests, however it also appears that the plugin is pre-activated, so the activation scripts never run and the tables aren’t installed. Also it doesn’t include any of the custom constants. Because of this obviously fatal errors get triggered.
Is there a way to run activation scripts right after the test database gets installed, in order to ensure the tables are there? Also how can I include the custom constants (likely with testing values) when using PHPUnit?
thanks!
]]>How I can test the html-content of my plugin in wp-admin with PHPUnit tests.
I want something like this:
set_current_screen('admin_page_myplugin');
$html = $this->getOriginalHTML();
$this->assertContains('My plugin unique content', $html);
]]>The problem I have is that I wrote the test that will check if the custom post types are registered, and if they are the test will pass
public function test_custom_post_types_are_registered() {
$post_types = get_post_types();
$documentation = \My_Plugin\Custom_Post_Type\Documentation::POST_TYPE_SLUG;
$email = \My_Plugin\Custom_Post_Type\Email_Templates::POST_TYPE_SLUG;
$faq = \My_Plugin\Custom_Post_Type\Faq::POST_TYPE_SLUG;
$this->assertTrue( isset( $post_types[ $documentation ] ) );
$this->assertTrue( isset( $post_types[ $email ] ) );
$this->assertTrue( isset( $post_types[ $faq ] ) );
}
I even put the print_r
in the code that is run which registers the CPT, and during the test run it’s actually shown.
But when I generate code coverage, this code isn’t covered.
Any guesses why?
]]>Following the directions on https://make.www.ads-software.com/cli/handbook/plugin-unit-tests/
Everything seems ok, the bin
and tests
dirs are created in the targeted plugin.
As are the yml and dist files.
But when I run phpunit, the result is always No tests executed!
I’m new to phpunit, so I’m hoping it is something simple.
Here is the log for an attempt: https://pastebin.com/9Tc1JhZr
]]>