admin管理员组

文章数量:1313598

I am writing tests that execute some of WordPress hooks / actions

My hook example

add_filter("asdasd", static function() {
    echo 'asdf';
});

It's complaining:

This test executed code that is not listed as code to be covered or used:
- /var/www/html/wp-content/themes/theme-1/init.php:123

Where line 123 is echo

I can get around this by using a named function like this

function my_asdasd() {
    echo 'asdf';
}
add_filter("asdasd", 'my_asdasd');

And then add my_asdasd to CoversFunction tag in the test suite

My question, how do I use closure function from example 1 while pleasing phpunit that I intended to cover the code?

I am writing tests that execute some of WordPress hooks / actions

My hook example

add_filter("asdasd", static function() {
    echo 'asdf';
});

It's complaining:

This test executed code that is not listed as code to be covered or used:
- /var/www/html/wp-content/themes/theme-1/init.php:123

Where line 123 is echo

I can get around this by using a named function like this

function my_asdasd() {
    echo 'asdf';
}
add_filter("asdasd", 'my_asdasd');

And then add my_asdasd to CoversFunction tag in the test suite

My question, how do I use closure function from example 1 while pleasing phpunit that I intended to cover the code?

Share Improve this question asked Jan 31 at 2:00 RuslanRuslan 826 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can try wrapping the function, put all of the filters / actions there For example

function my_init() 
{
    add_filter("asdasd", static function() {
        echo 'asdf';
    });
}
my_init();

Then on phpunit you can declare to collect coverage from my_init

本文标签: unit testingPHPunit risky test with Unintentionally Covered Code in wordpress callbacksStack Overflow